main.rs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #![feature(convert, fs_mode)]
  2. #![feature(slice_splits, vec_resize)]
  3. extern crate ansi_term;
  4. extern crate datetime;
  5. extern crate getopts;
  6. extern crate libc;
  7. extern crate locale;
  8. extern crate natord;
  9. extern crate num_cpus;
  10. extern crate number_prefix;
  11. extern crate pad;
  12. extern crate term_grid;
  13. extern crate threadpool;
  14. extern crate unicode_width;
  15. extern crate users;
  16. #[cfg(feature="git")]
  17. extern crate git2;
  18. use std::env;
  19. use std::fs;
  20. use std::path::{Component, Path, PathBuf};
  21. use std::process;
  22. use std::sync::mpsc::channel;
  23. use threadpool::ThreadPool;
  24. use dir::Dir;
  25. use file::File;
  26. use options::{Options, View};
  27. mod colours;
  28. mod column;
  29. mod dir;
  30. mod feature;
  31. mod file;
  32. mod filetype;
  33. mod options;
  34. mod output;
  35. mod term;
  36. #[cfg(not(test))]
  37. struct Exa<'dir> {
  38. count: usize,
  39. options: Options,
  40. dirs: Vec<PathBuf>,
  41. files: Vec<File<'dir>>,
  42. }
  43. #[cfg(not(test))]
  44. impl<'dir> Exa<'dir> {
  45. fn new(options: Options) -> Exa<'dir> {
  46. Exa {
  47. count: 0,
  48. options: options,
  49. dirs: Vec::new(),
  50. files: Vec::new(),
  51. }
  52. }
  53. fn load(&mut self, files: &[String]) {
  54. // Separate the user-supplied paths into directories and files.
  55. // Files are shown first, and then each directory is expanded
  56. // and listed second.
  57. let is_tree = self.options.dir_action.is_tree() || self.options.dir_action.is_as_file();
  58. let total_files = files.len();
  59. // Communication between consumer thread and producer threads
  60. enum StatResult<'dir> {
  61. File(File<'dir>),
  62. Dir(PathBuf),
  63. Error
  64. }
  65. let pool = ThreadPool::new(8 * num_cpus::get());
  66. let (tx, rx) = channel();
  67. for file in files.iter() {
  68. let tx = tx.clone();
  69. let file = file.clone();
  70. // Spawn producer thread
  71. pool.execute(move || {
  72. let path = Path::new(&*file);
  73. let _ = tx.send(match fs::metadata(&path) {
  74. Ok(metadata) => {
  75. if !metadata.is_dir() {
  76. StatResult::File(File::with_metadata(metadata, &path, None, false))
  77. }
  78. else if is_tree {
  79. StatResult::File(File::with_metadata(metadata, &path, None, true))
  80. }
  81. else {
  82. StatResult::Dir(path.to_path_buf())
  83. }
  84. }
  85. Err(e) => {
  86. println!("{}: {}", file, e);
  87. StatResult::Error
  88. }
  89. });
  90. });
  91. }
  92. // Spawn consumer thread
  93. for result in rx.iter().take(total_files) {
  94. match result {
  95. StatResult::File(file) => self.files.push(file),
  96. StatResult::Dir(path) => self.dirs.push(path),
  97. StatResult::Error => ()
  98. }
  99. self.count += 1;
  100. }
  101. }
  102. fn print_files(&self) {
  103. if !self.files.is_empty() {
  104. self.print(None, &self.files[..]);
  105. }
  106. }
  107. fn print_dirs(&mut self) {
  108. let mut first = self.files.is_empty();
  109. // Directories are put on a stack rather than just being iterated through,
  110. // as the vector can change as more directories are added.
  111. loop {
  112. let dir_path = match self.dirs.pop() {
  113. None => break,
  114. Some(f) => f,
  115. };
  116. // Put a gap between directories, or between the list of files and the
  117. // first directory.
  118. if first {
  119. first = false;
  120. }
  121. else {
  122. print!("\n");
  123. }
  124. match Dir::readdir(&dir_path) {
  125. Ok(ref dir) => {
  126. let mut files = dir.files(false);
  127. self.options.transform_files(&mut files);
  128. // When recursing, add any directories to the dirs stack
  129. // backwards: the *last* element of the stack is used each
  130. // time, so by inserting them backwards, they get displayed in
  131. // the correct sort order.
  132. if let Some(recurse_opts) = self.options.dir_action.recurse_options() {
  133. let depth = dir_path.components().filter(|&c| c != Component::CurDir).count() + 1;
  134. if !recurse_opts.tree && !recurse_opts.is_too_deep(depth) {
  135. for dir in files.iter().filter(|f| f.is_directory()).rev() {
  136. self.dirs.push(dir.path.clone());
  137. }
  138. }
  139. }
  140. if self.count > 1 {
  141. println!("{}:", dir_path.display());
  142. }
  143. self.count += 1;
  144. self.print(Some(dir), &files[..]);
  145. }
  146. Err(e) => {
  147. println!("{}: {}", dir_path.display(), e);
  148. return;
  149. }
  150. };
  151. }
  152. }
  153. fn print(&self, dir: Option<&Dir>, files: &[File]) {
  154. match self.options.view {
  155. View::Grid(g) => g.view(files),
  156. View::Details(d) => d.view(dir, files),
  157. View::GridDetails(gd) => gd.view(dir, files),
  158. View::Lines(l) => l.view(files),
  159. }
  160. }
  161. }
  162. #[cfg(not(test))]
  163. fn main() {
  164. let args: Vec<String> = env::args().skip(1).collect();
  165. match Options::getopts(&args) {
  166. Ok((options, paths)) => {
  167. let mut exa = Exa::new(options);
  168. exa.load(&paths);
  169. exa.print_files();
  170. exa.print_dirs();
  171. },
  172. Err(e) => {
  173. println!("{}", e);
  174. process::exit(e.error_code());
  175. },
  176. };
  177. }