1
0

options.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. use file::File;
  2. use std::cmp::lexical_ordering;
  3. pub enum SortField {
  4. Name, Extension, Size
  5. }
  6. pub struct Options {
  7. pub showInvisibles: bool,
  8. pub sortField: SortField,
  9. }
  10. impl SortField {
  11. pub fn from_word(word: StrBuf) -> SortField {
  12. match word.as_slice() {
  13. "name" => Name,
  14. "size" => Size,
  15. "ext" => Extension,
  16. _ => fail!("Invalid sorting order"),
  17. }
  18. }
  19. fn sort(&self, files: &mut Vec<File>) {
  20. match *self {
  21. Name => files.sort_by(|a, b| a.name.cmp(&b.name)),
  22. Size => files.sort_by(|a, b| a.stat.size.cmp(&b.stat.size)),
  23. Extension => files.sort_by(|a, b| {
  24. let exts = a.ext().cmp(&b.ext());
  25. let names = a.name.cmp(&b.name);
  26. lexical_ordering(exts, names)
  27. }),
  28. }
  29. }
  30. }
  31. impl Options {
  32. pub fn sort(&self, files: &mut Vec<File>) {
  33. self.sortField.sort(files);
  34. }
  35. pub fn show(&self, f: &File) -> bool {
  36. if self.showInvisibles {
  37. true
  38. } else {
  39. !f.name.starts_with(".")
  40. }
  41. }
  42. }