column.rs 1.9 KB

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