options.rs 3.6 KB

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