main.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #![allow(unstable)]
  2. extern crate ansi_term;
  3. extern crate number_prefix;
  4. extern crate unicode;
  5. extern crate users;
  6. #[cfg(feature="git")]
  7. extern crate git2;
  8. use std::old_io::{fs, FileType};
  9. use std::os::{args, set_exit_status};
  10. use dir::Dir;
  11. use file::File;
  12. use options::Options;
  13. pub mod column;
  14. pub mod dir;
  15. pub mod file;
  16. pub mod filetype;
  17. pub mod options;
  18. pub mod output;
  19. pub mod term;
  20. fn exa(options: &Options) {
  21. let mut dirs: Vec<String> = vec![];
  22. let mut files: Vec<File> = vec![];
  23. // It's only worth printing out directory names if the user supplied
  24. // more than one of them.
  25. let mut count = 0;
  26. // Separate the user-supplied paths into directories and files.
  27. // Files are shown first, and then each directory is expanded
  28. // and listed second.
  29. for file in options.path_strings() {
  30. let path = Path::new(file);
  31. match fs::stat(&path) {
  32. Ok(stat) => {
  33. if !options.list_dirs && stat.kind == FileType::Directory {
  34. dirs.push(file.clone());
  35. }
  36. else {
  37. // May as well reuse the stat result from earlier
  38. // instead of just using File::from_path().
  39. files.push(File::with_stat(stat, &path, None));
  40. }
  41. }
  42. Err(e) => println!("{}: {}", file, e),
  43. }
  44. count += 1;
  45. }
  46. let mut first = files.is_empty();
  47. if !files.is_empty() {
  48. options.view(None, files);
  49. }
  50. for dir_name in dirs.iter() {
  51. if first {
  52. first = false;
  53. }
  54. else {
  55. print!("\n");
  56. }
  57. match Dir::readdir(Path::new(dir_name.clone())) {
  58. Ok(ref dir) => {
  59. let unsorted_files = dir.files();
  60. let files: Vec<File> = options.transform_files(unsorted_files);
  61. if count > 1 {
  62. println!("{}:", dir_name);
  63. }
  64. options.view(Some(dir), files);
  65. }
  66. Err(e) => {
  67. println!("{}: {}", dir_name, e);
  68. return;
  69. }
  70. };
  71. }
  72. }
  73. fn main() {
  74. let args: Vec<String> = args();
  75. match Options::getopts(args.tail()) {
  76. Ok(options) => exa(&options),
  77. Err(e) => {
  78. println!("{}", e);
  79. set_exit_status(e.error_code());
  80. },
  81. };
  82. }