column.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. The length of the string is
  41. // passed in specifically because it needs to be the *unformatted*
  42. // length, rather than just the number of characters.
  43. impl Alignment {
  44. pub fn pad_string(&self, string: &String, string_length: uint, width: uint) -> String {
  45. let mut str = String::new();
  46. match *self {
  47. Left => {
  48. str.push_str(string.as_slice());
  49. for _ in range(string_length, width) {
  50. str.push_char(' ');
  51. }
  52. }
  53. Right => {
  54. for _ in range(string_length, width) {
  55. str.push_char(' ');
  56. }
  57. str.push_str(string.as_slice());
  58. },
  59. }
  60. return str;
  61. }
  62. }