exa.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #![feature(phase)]
  2. extern crate regex;
  3. #[phase(plugin)] extern crate regex_macros;
  4. use std::os;
  5. use file::File;
  6. use dir::Dir;
  7. use options::Options;
  8. use unix::Unix;
  9. pub mod colours;
  10. pub mod column;
  11. pub mod dir;
  12. pub mod format;
  13. pub mod file;
  14. pub mod filetype;
  15. pub mod unix;
  16. pub mod options;
  17. pub mod sort;
  18. fn main() {
  19. let args = os::args();
  20. match Options::getopts(args) {
  21. Err(err) => println!("Invalid options:\n{}", err),
  22. Ok(opts) => {
  23. // Default to listing the current directory when a target
  24. // isn't specified (mimic the behaviour of ls)
  25. let strs = if opts.dirs.is_empty() {
  26. vec!(".".to_string())
  27. }
  28. else {
  29. opts.dirs.clone()
  30. };
  31. for dir in strs.move_iter() {
  32. exa(&opts, dir)
  33. }
  34. }
  35. };
  36. }
  37. fn exa(options: &Options, string: String) {
  38. let path = Path::new(string.clone());
  39. let dir = match Dir::readdir(path) {
  40. Ok(dir) => dir,
  41. Err(e) => {
  42. println!("{}: {}", string, e);
  43. return;
  44. }
  45. };
  46. let unsorted_files = dir.files();
  47. let files: Vec<&File> = options.transform_files(&unsorted_files);
  48. // The output gets formatted into columns, which looks nicer. To
  49. // do this, we have to write the results into a table, instead of
  50. // displaying each file immediately, then calculating the maximum
  51. // width of each column based on the length of the results and
  52. // padding the fields during output.
  53. let mut cache = Unix::empty_cache();
  54. let table: Vec<Vec<String>> = files.iter()
  55. .map(|f| options.columns.iter().map(|c| f.display(c, &mut cache)).collect())
  56. .collect();
  57. // Each column needs to have its invisible colour-formatting
  58. // characters stripped before it has its width calculated, or the
  59. // width will be incorrect and the columns won't line up properly.
  60. // This is fairly expensive to do (it uses a regex), so the
  61. // results are cached.
  62. let lengths: Vec<Vec<uint>> = table.iter()
  63. .map(|row| row.iter().map(|col| colours::strip_formatting(col).len()).collect())
  64. .collect();
  65. let column_widths: Vec<uint> = range(0, options.columns.len())
  66. .map(|n| lengths.iter().map(|row| *row.get(n)).max().unwrap())
  67. .collect();
  68. for (field_lengths, row) in lengths.iter().zip(table.iter()) {
  69. for (((column_length, cell), field_length), (num, column)) in column_widths.iter().zip(row.iter()).zip(field_lengths.iter()).zip(options.columns.iter().enumerate()) { // this is getting messy
  70. if num != 0 {
  71. print!(" ");
  72. }
  73. if num == options.columns.len() - 1 {
  74. print!("{}", cell);
  75. }
  76. else {
  77. print!("{}", column.alignment().pad_string(cell, *field_length, *column_length));
  78. }
  79. }
  80. print!("\n");
  81. }
  82. }