column.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. pub enum Column {
  2. Permissions,
  3. FileName,
  4. FileSize(bool),
  5. User(u64),
  6. Group,
  7. HardLinks,
  8. }
  9. // Each column can pick its own alignment. Usually, numbers are
  10. // right-aligned, and text is left-aligned.
  11. pub enum Alignment {
  12. Left, Right,
  13. }
  14. impl Column {
  15. pub fn alignment(&self) -> Alignment {
  16. match *self {
  17. FileSize(_) => Right,
  18. HardLinks => Right,
  19. _ => Left,
  20. }
  21. }
  22. }
  23. // An Alignment is used to pad a string to a certain length, letting
  24. // it pick which end it puts the text on. The length of the string is
  25. // passed in specifically because it needs to be the *unformatted*
  26. // length, rather than just the number of characters.
  27. impl Alignment {
  28. pub fn pad_string(&self, string: &String, string_length: uint, width: uint) -> String {
  29. let mut str = String::new();
  30. match *self {
  31. Left => {
  32. str.push_str(string.as_slice());
  33. for _ in range(string_length, width) {
  34. str.push_char(' ');
  35. }
  36. }
  37. Right => {
  38. for _ in range(string_length, width) {
  39. str.push_char(' ');
  40. }
  41. str.push_str(string.as_slice());
  42. },
  43. }
  44. return str;
  45. }
  46. }