exa.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. if opts.dirs.is_empty() {
  24. exa(&opts, false, ".".to_string())
  25. }
  26. else {
  27. let mut first = true;
  28. let print_header = opts.dirs.len() > 1;
  29. for dir in opts.dirs.clone().move_iter() {
  30. if first {
  31. first = false;
  32. }
  33. else {
  34. print!("\n");
  35. }
  36. exa(&opts, print_header, dir)
  37. }
  38. }
  39. }
  40. };
  41. }
  42. fn exa(options: &Options, print_header: bool, string: String) {
  43. let path = Path::new(string.clone());
  44. let dir = match Dir::readdir(path) {
  45. Ok(dir) => dir,
  46. Err(e) => {
  47. println!("{}: {}", string, e);
  48. return;
  49. }
  50. };
  51. // Print header *after* readdir must have succeeded
  52. if print_header {
  53. println!("{}:", string);
  54. }
  55. let unsorted_files = dir.files();
  56. let files: Vec<&File> = options.transform_files(&unsorted_files);
  57. // The output gets formatted into columns, which looks nicer. To
  58. // do this, we have to write the results into a table, instead of
  59. // displaying each file immediately, then calculating the maximum
  60. // width of each column based on the length of the results and
  61. // padding the fields during output.
  62. let mut cache = Unix::empty_cache();
  63. let table: Vec<Vec<String>> = files.iter()
  64. .map(|f| options.columns.iter().map(|c| f.display(c, &mut cache)).collect())
  65. .collect();
  66. // Each column needs to have its invisible colour-formatting
  67. // characters stripped before it has its width calculated, or the
  68. // width will be incorrect and the columns won't line up properly.
  69. // This is fairly expensive to do (it uses a regex), so the
  70. // results are cached.
  71. let lengths: Vec<Vec<uint>> = table.iter()
  72. .map(|row| row.iter().map(|col| colours::strip_formatting(col).len()).collect())
  73. .collect();
  74. let column_widths: Vec<uint> = range(0, options.columns.len())
  75. .map(|n| lengths.iter().map(|row| *row.get(n)).max().unwrap())
  76. .collect();
  77. for (field_lengths, row) in lengths.iter().zip(table.iter()) {
  78. 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
  79. if num != 0 {
  80. print!(" ");
  81. }
  82. if num == options.columns.len() - 1 {
  83. print!("{}", cell);
  84. }
  85. else {
  86. print!("{}", column.alignment().pad_string(cell, *field_length, *column_length));
  87. }
  88. }
  89. print!("\n");
  90. }
  91. }