exa.rs 3.3 KB

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