exa.rs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. extern crate getopts;
  2. use std::os;
  3. use std::io::fs;
  4. use file::File;
  5. use column::defaultColumns;
  6. pub mod colours;
  7. pub mod column;
  8. pub mod format;
  9. pub mod file;
  10. struct Options {
  11. showInvisibles: bool,
  12. }
  13. fn main() {
  14. let args = os::args();
  15. let program = args[0].as_slice();
  16. let opts = ~[
  17. getopts::optflag("a", "all", "show dot-files")
  18. ];
  19. let matches = match getopts::getopts(args.tail(), opts) {
  20. Ok(m) => m,
  21. Err(f) => fail!("Invalid options\n{}", f.to_err_msg()),
  22. };
  23. let opts = Options {
  24. showInvisibles: matches.opt_present("all")
  25. };
  26. let strs = if matches.free.is_empty() {
  27. vec!("./".to_owned())
  28. }
  29. else {
  30. matches.free.clone()
  31. };
  32. for dir in strs.move_iter() {
  33. list(opts, Path::new(dir))
  34. }
  35. }
  36. fn list(opts: Options, path: Path) {
  37. let mut files = match fs::readdir(&path) {
  38. Ok(files) => files,
  39. Err(e) => fail!("readdir: {}", e),
  40. };
  41. files.sort_by(|a, b| a.filename_str().cmp(&b.filename_str()));
  42. for subpath in files.iter() {
  43. let file = File::from_path(subpath);
  44. if file.is_dotfile() && !opts.showInvisibles {
  45. continue;
  46. }
  47. let columns = defaultColumns();
  48. let mut cells = columns.iter().map(|c| file.display(c));
  49. let mut first = true;
  50. for cell in cells {
  51. if first {
  52. first = false;
  53. } else {
  54. print!(" ");
  55. }
  56. print!("{}", cell);
  57. }
  58. print!("\n");
  59. }
  60. }