options.rs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. extern crate getopts;
  2. extern crate natord;
  3. use file::File;
  4. use column::Column;
  5. use column::Column::*;
  6. use term::dimensions;
  7. use std::ascii::AsciiExt;
  8. pub enum SortField {
  9. Unsorted, Name, Extension, Size, FileInode
  10. }
  11. impl Copy for SortField { }
  12. impl SortField {
  13. fn from_word(word: String) -> SortField {
  14. match word.as_slice() {
  15. "name" => SortField::Name,
  16. "size" => SortField::Size,
  17. "ext" => SortField::Extension,
  18. "none" => SortField::Unsorted,
  19. "inode" => SortField::FileInode,
  20. _ => panic!("Invalid sorting order"),
  21. }
  22. }
  23. }
  24. pub enum View {
  25. Details(Vec<Column>),
  26. Lines,
  27. Grid(bool, uint),
  28. }
  29. pub struct Options {
  30. pub header: bool,
  31. pub list_dirs: bool,
  32. pub path_strs: Vec<String>,
  33. pub reverse: bool,
  34. pub show_invisibles: bool,
  35. pub sort_field: SortField,
  36. pub view: View,
  37. }
  38. impl Options {
  39. pub fn getopts(args: Vec<String>) -> Result<Options, int> {
  40. let opts = [
  41. getopts::optflag("1", "oneline", "display one entry per line"),
  42. getopts::optflag("a", "all", "show dot-files"),
  43. getopts::optflag("b", "binary", "use binary prefixes in file sizes"),
  44. getopts::optflag("d", "list-dirs", "list directories as regular files"),
  45. getopts::optflag("g", "group", "show group as well as user"),
  46. getopts::optflag("h", "header", "show a header row at the top"),
  47. getopts::optflag("H", "links", "show number of hard links"),
  48. getopts::optflag("l", "long", "display extended details and attributes"),
  49. getopts::optflag("i", "inode", "show each file's inode number"),
  50. getopts::optflag("r", "reverse", "reverse order of files"),
  51. getopts::optopt ("s", "sort", "field to sort by", "WORD"),
  52. getopts::optflag("S", "blocks", "show number of file system blocks"),
  53. getopts::optflag("x", "across", "sort multi-column view entries across"),
  54. getopts::optflag("?", "help", "show list of command-line options"),
  55. ];
  56. let matches = match getopts::getopts(args.tail(), &opts) {
  57. Ok(m) => m,
  58. Err(e) => {
  59. println!("Invalid options: {}", e);
  60. return Err(1);
  61. }
  62. };
  63. if matches.opt_present("help") {
  64. println!("exa - ls with more features\n\n{}", getopts::usage("Usage:\n exa [options] [files...]", &opts))
  65. return Err(2);
  66. }
  67. Ok(Options {
  68. header: matches.opt_present("header"),
  69. list_dirs: matches.opt_present("list-dirs"),
  70. path_strs: if matches.free.is_empty() { vec![ ".".to_string() ] } else { matches.free.clone() },
  71. reverse: matches.opt_present("reverse"),
  72. show_invisibles: matches.opt_present("all"),
  73. sort_field: matches.opt_str("sort").map(|word| SortField::from_word(word)).unwrap_or(SortField::Name),
  74. view: Options::view(&matches),
  75. })
  76. }
  77. fn view(matches: &getopts::Matches) -> View {
  78. if matches.opt_present("long") {
  79. View::Details(Options::columns(matches))
  80. }
  81. else if matches.opt_present("oneline") {
  82. View::Lines
  83. }
  84. else {
  85. match dimensions() {
  86. None => View::Lines,
  87. Some((width, _)) => View::Grid(matches.opt_present("across"), width),
  88. }
  89. }
  90. }
  91. fn columns(matches: &getopts::Matches) -> Vec<Column> {
  92. let mut columns = vec![];
  93. if matches.opt_present("inode") {
  94. columns.push(Inode);
  95. }
  96. columns.push(Permissions);
  97. if matches.opt_present("links") {
  98. columns.push(HardLinks);
  99. }
  100. columns.push(FileSize(matches.opt_present("binary")));
  101. if matches.opt_present("blocks") {
  102. columns.push(Blocks);
  103. }
  104. columns.push(User);
  105. if matches.opt_present("group") {
  106. columns.push(Group);
  107. }
  108. columns.push(FileName);
  109. columns
  110. }
  111. fn should_display(&self, f: &File) -> bool {
  112. if self.show_invisibles {
  113. true
  114. }
  115. else {
  116. !f.name.as_slice().starts_with(".")
  117. }
  118. }
  119. pub fn transform_files<'a>(&self, unordered_files: Vec<File<'a>>) -> Vec<File<'a>> {
  120. let mut files: Vec<File<'a>> = unordered_files.into_iter()
  121. .filter(|f| self.should_display(f))
  122. .collect();
  123. match self.sort_field {
  124. SortField::Unsorted => {},
  125. SortField::Name => files.sort_by(|a, b| natord::compare(a.name.as_slice(), b.name.as_slice())),
  126. SortField::Size => files.sort_by(|a, b| a.stat.size.cmp(&b.stat.size)),
  127. SortField::FileInode => files.sort_by(|a, b| a.stat.unstable.inode.cmp(&b.stat.unstable.inode)),
  128. SortField::Extension => files.sort_by(|a, b| {
  129. let exts = a.ext.clone().map(|e| e.to_ascii_lower()).cmp(&b.ext.clone().map(|e| e.to_ascii_lower()));
  130. let names = a.name.to_ascii_lower().cmp(&b.name.to_ascii_lower());
  131. exts.cmp(&names)
  132. }),
  133. }
  134. if self.reverse {
  135. files.reverse();
  136. }
  137. files
  138. }
  139. }