column.rs 1.4 KB

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