exa.rs 1.6 KB

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