options.rs 1.1 KB

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