options.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, Blocks};
  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. getopts::optflag("S", "blocks", "show number of file system blocks"),
  38. ];
  39. match getopts::getopts(args.tail(), opts) {
  40. Err(f) => Err(f),
  41. Ok(matches) => Ok(Options {
  42. showInvisibles: matches.opt_present("all"),
  43. reverse: matches.opt_present("reverse"),
  44. sortField: matches.opt_str("sort").map(|word| SortField::from_word(word)).unwrap_or(Name),
  45. dirs: matches.free.clone(),
  46. columns: Options::columns(matches),
  47. })
  48. }
  49. }
  50. fn columns(matches: getopts::Matches) -> Vec<Column> {
  51. let mut columns = vec![];
  52. if matches.opt_present("inode") {
  53. columns.push(Inode);
  54. }
  55. columns.push(Permissions);
  56. if matches.opt_present("links") {
  57. columns.push(HardLinks);
  58. }
  59. columns.push(FileSize(matches.opt_present("binary")));
  60. if matches.opt_present("blocks") {
  61. columns.push(Blocks);
  62. }
  63. columns.push(User(get_current_user_id()));
  64. if matches.opt_present("group") {
  65. columns.push(Group);
  66. }
  67. columns.push(FileName);
  68. return columns;
  69. }
  70. fn should_display(&self, f: &File) -> bool {
  71. if self.showInvisibles {
  72. true
  73. } else {
  74. !f.name.starts_with(".")
  75. }
  76. }
  77. pub fn transform_files<'a>(&self, unordered_files: &'a Vec<File<'a>>) -> Vec<&'a File<'a>> {
  78. let mut files: Vec<&'a File<'a>> = unordered_files.iter()
  79. .filter(|&f| self.should_display(f))
  80. .collect();
  81. match self.sortField {
  82. Name => files.sort_by(|a, b| a.parts.cmp(&b.parts)),
  83. Size => files.sort_by(|a, b| a.stat.size.cmp(&b.stat.size)),
  84. Extension => files.sort_by(|a, b| {
  85. let exts = a.ext.map(|e| e.to_ascii_lower()).cmp(&b.ext.map(|e| e.to_ascii_lower()));
  86. let names = a.name.to_ascii_lower().cmp(&b.name.to_ascii_lower());
  87. lexical_ordering(exts, names)
  88. }),
  89. }
  90. if self.reverse {
  91. files.reverse();
  92. }
  93. return files;
  94. }
  95. }