column.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. pub enum Column {
  2. Permissions,
  3. FileName,
  4. FileSize(bool),
  5. Blocks,
  6. User(u64),
  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. }
  27. // An Alignment is used to pad a string to a certain length, letting
  28. // it pick which end it puts the text on. The length of the string is
  29. // passed in specifically because it needs to be the *unformatted*
  30. // length, rather than just the number of characters.
  31. impl Alignment {
  32. pub fn pad_string(&self, string: &String, string_length: uint, width: uint) -> String {
  33. let mut str = String::new();
  34. match *self {
  35. Left => {
  36. str.push_str(string.as_slice());
  37. for _ in range(string_length, width) {
  38. str.push_char(' ');
  39. }
  40. }
  41. Right => {
  42. for _ in range(string_length, width) {
  43. str.push_char(' ');
  44. }
  45. str.push_str(string.as_slice());
  46. },
  47. }
  48. return str;
  49. }
  50. }