grid_details.rs 11 KB

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