grid_details.rs 12 KB

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