1
0

options.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. extern crate getopts;
  2. use file::File;
  3. use std::cmp::lexical_ordering;
  4. use column::{Column, Permissions, FileName, FileSize, User, Group, HardLinks, Inode};
  5. use unix::get_current_user_id;
  6. use std::ascii::StrAsciiExt;
  7. pub enum SortField {
  8. Name, Extension, Size
  9. }
  10. pub struct Options {
  11. pub showInvisibles: bool,
  12. pub sortField: SortField,
  13. pub reverse: bool,
  14. pub dirs: Vec<String>,
  15. pub columns: Vec<Column>,
  16. }
  17. impl SortField {
  18. fn from_word(word: String) -> SortField {
  19. match word.as_slice() {
  20. "name" => Name,
  21. "size" => Size,
  22. "ext" => Extension,
  23. _ => fail!("Invalid sorting order"),
  24. }
  25. }
  26. }
  27. impl Options {
  28. pub fn getopts(args: Vec<String>) -> Result<Options, getopts::Fail_> {
  29. let opts = [
  30. getopts::optflag("a", "all", "show dot-files"),
  31. getopts::optflag("b", "binary", "use binary prefixes in file sizes"),
  32. getopts::optflag("g", "group", "show group as well as user"),
  33. getopts::optflag("i", "inode", "show each file's inode number"),
  34. getopts::optflag("l", "links", "show number of hard links"),
  35. getopts::optflag("r", "reverse", "reverse order of files"),
  36. getopts::optopt("s", "sort", "field to sort by", "WORD"),
  37. ];
  38. match getopts::getopts(args.tail(), opts) {
  39. Err(f) => Err(f),
  40. Ok(matches) => Ok(Options {
  41. showInvisibles: matches.opt_present("all"),
  42. reverse: matches.opt_present("reverse"),
  43. sortField: matches.opt_str("sort").map(|word| SortField::from_word(word)).unwrap_or(Name),
  44. dirs: matches.free.clone(),
  45. columns: Options::columns(matches),
  46. })
  47. }
  48. }
  49. fn columns(matches: getopts::Matches) -> Vec<Column> {
  50. let mut columns = vec![];
  51. if matches.opt_present("inode") {
  52. columns.push(Inode);
  53. }
  54. columns.push(Permissions);
  55. if matches.opt_present("links") {
  56. columns.push(HardLinks);
  57. }
  58. columns.push(FileSize(matches.opt_present("binary")));
  59. columns.push(User(get_current_user_id()));
  60. if matches.opt_present("group") {
  61. columns.push(Group);
  62. }
  63. columns.push(FileName);
  64. return columns;
  65. }
  66. fn should_display(&self, f: &File) -> bool {
  67. if self.showInvisibles {
  68. true
  69. } else {
  70. !f.name.starts_with(".")
  71. }
  72. }
  73. pub fn transform_files<'a>(&self, unordered_files: &'a Vec<File<'a>>) -> Vec<&'a File<'a>> {
  74. let mut files: Vec<&'a File<'a>> = unordered_files.iter()
  75. .filter(|&f| self.should_display(f))
  76. .collect();
  77. match self.sortField {
  78. Name => files.sort_by(|a, b| a.parts.cmp(&b.parts)),
  79. Size => files.sort_by(|a, b| a.stat.size.cmp(&b.stat.size)),
  80. Extension => files.sort_by(|a, b| {
  81. let exts = a.ext.map(|e| e.to_ascii_lower()).cmp(&b.ext.map(|e| e.to_ascii_lower()));
  82. let names = a.name.to_ascii_lower().cmp(&b.name.to_ascii_lower());
  83. lexical_ordering(exts, names)
  84. }),
  85. }
  86. if self.reverse {
  87. files.reverse();
  88. }
  89. return files;
  90. }
  91. }