1
0

column.rs 1.2 KB

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