exa.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. extern crate getopts;
  2. use std::os;
  3. use std::io;
  4. use std::io::fs;
  5. use file::File;
  6. use column::{Column, defaultColumns};
  7. pub mod colours;
  8. pub mod column;
  9. pub mod format;
  10. pub mod file;
  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) => {
  23. fail!("Invalid options\n{}", f.to_err_msg());
  24. return
  25. }
  26. };
  27. let opts = Options {
  28. showInvisibles: matches.opt_present("all")
  29. };
  30. let strs = if matches.free.is_empty() {vec!(~"./")} else {matches.free.clone()};
  31. for dir in strs.move_iter() {
  32. list(opts, Path::new(dir))
  33. }
  34. }
  35. fn list(opts: Options, path: Path) {
  36. let mut files = match fs::readdir(&path) {
  37. Ok(files) => files,
  38. Err(e) => fail!("readdir: {}", e),
  39. };
  40. files.sort_by(|a, b| a.filename_str().cmp(&b.filename_str()));
  41. for subpath in files.iter() {
  42. let file = File::from_path(subpath);
  43. if file.name.starts_with(".") && !opts.showInvisibles {
  44. continue;
  45. }
  46. let columns = defaultColumns();
  47. let mut cells = columns.iter().map(|c| file.display(c));
  48. let mut first = true;
  49. for cell in cells {
  50. if first {
  51. first = false;
  52. } else {
  53. print!(" ");
  54. }
  55. print!("{}", cell);
  56. }
  57. print!("\n");
  58. }
  59. }