grid_details.rs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //! The grid-details view lists several details views side-by-side.
  2. use std::io::{Write, Result as IOResult};
  3. use ansi_term::ANSIStrings;
  4. use term_grid as grid;
  5. use fs::{Dir, File};
  6. use fs::feature::git::GitCache;
  7. use fs::feature::xattr::FileAttributes;
  8. use fs::filter::FileFilter;
  9. use style::Colours;
  10. use output::cell::TextCell;
  11. use output::details::{Options as DetailsOptions, Row as DetailsRow, Render as DetailsRender};
  12. use output::grid::Options as GridOptions;
  13. use output::file_name::FileStyle;
  14. use output::table::{Table, Row as TableRow, Options as TableOptions};
  15. use output::tree::{TreeParams, TreeDepth};
  16. #[derive(Debug)]
  17. pub struct Options {
  18. pub grid: GridOptions,
  19. pub details: DetailsOptions,
  20. pub row_threshold: RowThreshold,
  21. }
  22. /// The grid-details view can be configured to revert to just a details view
  23. /// (with one column) if it wouldn’t produce enough rows of output.
  24. ///
  25. /// Doing this makes the resulting output look a bit better: when listing a
  26. /// small directory of four files in four columns, the files just look spaced
  27. /// out and it’s harder to see what’s going on. So it can be enabled just for
  28. /// larger directory listings.
  29. #[derive(Copy, Clone, Debug, PartialEq)]
  30. pub enum RowThreshold {
  31. /// Only use grid-details view if it would result in at least this many
  32. /// rows of output.
  33. MinimumRows(usize),
  34. /// Use the grid-details view no matter what.
  35. AlwaysGrid,
  36. }
  37. pub struct Render<'a> {
  38. /// The directory that’s being rendered here.
  39. /// We need this to know which columns to put in the output.
  40. pub dir: Option<&'a Dir>,
  41. /// The files that have been read from the directory. They should all
  42. /// hold a reference to it.
  43. pub files: Vec<File<'a>>,
  44. /// How to colour various pieces of text.
  45. pub colours: &'a Colours,
  46. /// How to format filenames.
  47. pub style: &'a FileStyle,
  48. /// The grid part of the grid-details view.
  49. pub grid: &'a GridOptions,
  50. /// The details part of the grid-details view.
  51. pub details: &'a DetailsOptions,
  52. /// How to filter files after listing a directory. The files in this
  53. /// render will already have been filtered and sorted, but any directories
  54. /// that we recurse into will have to have this applied.
  55. pub filter: &'a FileFilter,
  56. /// The minimum number of rows that there need to be before grid-details
  57. /// mode is activated.
  58. pub row_threshold: RowThreshold,
  59. }
  60. impl<'a> Render<'a> {
  61. /// Create a temporary Details render that gets used for the columns of
  62. /// the grid-details render that's being generated.
  63. ///
  64. /// This includes an empty files vector because the files get added to
  65. /// the table in *this* file, not in details: we only want to insert every
  66. /// *n* files into each column’s table, not all of them.
  67. pub fn details(&self) -> DetailsRender<'a> {
  68. DetailsRender {
  69. dir: self.dir.clone(),
  70. files: Vec::new(),
  71. colours: self.colours,
  72. style: self.style,
  73. opts: self.details,
  74. recurse: None,
  75. filter: self.filter,
  76. }
  77. }
  78. /// Create a Details render for when this grid-details render doesn’t fit
  79. /// in the terminal (or something has gone wrong) and we have given up.
  80. pub fn give_up(self) -> DetailsRender<'a> {
  81. DetailsRender {
  82. dir: self.dir,
  83. files: self.files,
  84. colours: self.colours,
  85. style: self.style,
  86. opts: self.details,
  87. recurse: None,
  88. filter: &self.filter,
  89. }
  90. }
  91. pub fn render<W: Write>(self, git: Option<&GitCache>, w: &mut W) -> IOResult<()> {
  92. if let Some((grid, width)) = self.find_fitting_grid(git) {
  93. write!(w, "{}", grid.fit_into_columns(width))
  94. }
  95. else {
  96. self.give_up().render(git, w)
  97. }
  98. }
  99. pub fn find_fitting_grid(&self, git: Option<&GitCache>) -> Option<(grid::Grid, grid::Width)> {
  100. let options = self.details.table.as_ref().expect("Details table options not given!");
  101. let drender = self.details();
  102. let (first_table, _) = self.make_table(options, git, &drender);
  103. let rows = self.files.iter()
  104. .map(|file| first_table.row_for_file(file, file_has_xattrs(file)))
  105. .collect::<Vec<TableRow>>();
  106. let file_names = self.files.iter()
  107. .map(|file| self.style.for_file(file, self.colours).paint().promote())
  108. .collect::<Vec<TextCell>>();
  109. let mut last_working_table = self.make_grid(1, options, git, &file_names, rows.clone(), &drender);
  110. // If we can’t fit everything in a grid 100 columns wide, then
  111. // something has gone seriously awry
  112. for column_count in 2..100 {
  113. let grid = self.make_grid(column_count, options, git, &file_names, rows.clone(), &drender);
  114. let the_grid_fits = {
  115. let d = grid.fit_into_columns(column_count);
  116. d.is_complete() && d.width() <= self.grid.console_width
  117. };
  118. if the_grid_fits {
  119. last_working_table = grid;
  120. }
  121. else {
  122. // If we’ve figured out how many columns can fit in the user’s
  123. // terminal, and it turns out there aren’t enough rows to
  124. // make it worthwhile, then just resort to the lines view.
  125. if let RowThreshold::MinimumRows(thresh) = self.row_threshold {
  126. if last_working_table.fit_into_columns(column_count - 1).row_count() < thresh {
  127. return None;
  128. }
  129. }
  130. return Some((last_working_table, column_count - 1));
  131. }
  132. }
  133. None
  134. }
  135. fn make_table<'t>(&'a self, options: &'a TableOptions, mut git: Option<&'a GitCache>, drender: &DetailsRender) -> (Table<'a>, Vec<DetailsRow>) {
  136. if self.dir.is_none() { git = None }
  137. if let (Some(g), Some(d)) = (git, self.dir) { if !g.has_anything_for(&d.path) { git = None } }
  138. let mut table = Table::new(options, git, self.colours);
  139. let mut rows = Vec::new();
  140. if self.details.header {
  141. let row = table.header_row();
  142. table.add_widths(&row);
  143. rows.push(drender.render_header(row));
  144. }
  145. (table, rows)
  146. }
  147. fn make_grid(&'a self, column_count: usize, options: &'a TableOptions, git: Option<&GitCache>, file_names: &[TextCell], rows: Vec<TableRow>, drender: &DetailsRender) -> grid::Grid {
  148. let mut tables = Vec::new();
  149. for _ in 0 .. column_count {
  150. tables.push(self.make_table(options, git, drender));
  151. }
  152. let mut num_cells = rows.len();
  153. if self.details.header {
  154. num_cells += column_count;
  155. }
  156. let original_height = divide_rounding_up(rows.len(), column_count);
  157. let height = divide_rounding_up(num_cells, column_count);
  158. for (i, (file_name, row)) in file_names.iter().zip(rows.into_iter()).enumerate() {
  159. let index = if self.grid.across {
  160. i % column_count
  161. }
  162. else {
  163. i / original_height
  164. };
  165. let (ref mut table, ref mut rows) = tables[index];
  166. table.add_widths(&row);
  167. let details_row = drender.render_file(row, file_name.clone(), TreeParams::new(TreeDepth::root(), false));
  168. rows.push(details_row);
  169. }
  170. let columns: Vec<_> = tables.into_iter().map(|(table, details_rows)| {
  171. drender.iterate_with_table(table, details_rows).collect::<Vec<_>>()
  172. }).collect();
  173. let direction = if self.grid.across { grid::Direction::LeftToRight }
  174. else { grid::Direction::TopToBottom };
  175. let mut grid = grid::Grid::new(grid::GridOptions {
  176. direction: direction,
  177. filling: grid::Filling::Spaces(4),
  178. });
  179. if self.grid.across {
  180. for row in 0 .. height {
  181. for column in &columns {
  182. if row < column.len() {
  183. let cell = grid::Cell {
  184. contents: ANSIStrings(&column[row].contents).to_string(),
  185. width: *column[row].width,
  186. };
  187. grid.add(cell);
  188. }
  189. }
  190. }
  191. }
  192. else {
  193. for column in &columns {
  194. for cell in column.iter() {
  195. let cell = grid::Cell {
  196. contents: ANSIStrings(&cell.contents).to_string(),
  197. width: *cell.width,
  198. };
  199. grid.add(cell);
  200. }
  201. }
  202. }
  203. grid
  204. }
  205. }
  206. fn divide_rounding_up(a: usize, b: usize) -> usize {
  207. let mut result = a / b;
  208. if a % b != 0 { result += 1; }
  209. result
  210. }
  211. fn file_has_xattrs(file: &File) -> bool {
  212. match file.path.attributes() {
  213. Ok(attrs) => !attrs.is_empty(),
  214. Err(_) => false,
  215. }
  216. }