exa.rs 1.5 KB

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