grid_details.rs 10 KB

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