options.rs 3.5 KB

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