options.rs 5.1 KB

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