column.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. pub enum Column {
  2. Permissions,
  3. FileName,
  4. FileSize(bool),
  5. Blocks,
  6. User,
  7. Group,
  8. HardLinks,
  9. Inode,
  10. }
  11. impl Copy for Column { }
  12. // Each column can pick its own alignment. Usually, numbers are
  13. // right-aligned, and text is left-aligned.
  14. pub enum Alignment {
  15. Left, Right,
  16. }
  17. impl Copy for Alignment { }
  18. impl Column {
  19. pub fn alignment(&self) -> Alignment {
  20. match *self {
  21. Column::FileSize(_) => Alignment::Right,
  22. Column::HardLinks => Alignment::Right,
  23. Column::Inode => Alignment::Right,
  24. Column::Blocks => Alignment::Right,
  25. _ => Alignment::Left,
  26. }
  27. }
  28. pub fn header(&self) -> &'static str {
  29. match *self {
  30. Column::Permissions => "Permissions",
  31. Column::FileName => "Name",
  32. Column::FileSize(_) => "Size",
  33. Column::Blocks => "Blocks",
  34. Column::User => "User",
  35. Column::Group => "Group",
  36. Column::HardLinks => "Links",
  37. Column::Inode => "inode",
  38. }
  39. }
  40. }
  41. // An Alignment is used to pad a string to a certain length, letting
  42. // it pick which end it puts the text on. It takes the amount of
  43. // padding to apply, rather than the width the text should end up,
  44. // because these strings are usually full of control characters.
  45. impl Alignment {
  46. pub fn pad_string(&self, string: &String, padding: uint) -> String {
  47. match *self {
  48. Alignment::Left => format!("{}{}", string, " ".repeat(padding).as_slice()),
  49. Alignment::Right => format!("{}{}", " ".repeat(padding), string.as_slice()),
  50. }
  51. }
  52. }