use std::io::{self, Write};
use nu_ansi_term::AnsiStrings;
use term_grid as tg;
use crate::fs::{Dir, File};
use crate::fs::feature::VcsCache;
use crate::fs::feature::xattr::FileAttributes;
use crate::fs::filter::FileFilter;
use crate::output::cell::TextCell;
use crate::output::details::{Options as DetailsOptions, Row as DetailsRow, Render as DetailsRender};
use crate::output::file_name::Options as FileStyle;
use crate::output::grid::Options as GridOptions;
use crate::output::table::{Table, Row as TableRow, Options as TableOptions};
use crate::output::tree::{TreeParams, TreeDepth};
use crate::theme::Theme;
#[derive(PartialEq, Eq, Debug)]
pub struct Options {
pub grid: GridOptions,
pub details: DetailsOptions,
pub row_threshold: RowThreshold,
}
impl Options {
pub fn to_details_options(&self) -> &DetailsOptions {
&self.details
}
}
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub enum RowThreshold {
MinimumRows(usize),
AlwaysGrid,
}
pub struct Render<'a> {
pub dir: Option<&'a Dir>,
pub files: Vec<File<'a>>,
pub theme: &'a Theme,
pub file_style: &'a FileStyle,
pub grid: &'a GridOptions,
pub details: &'a DetailsOptions,
pub filter: &'a FileFilter,
pub row_threshold: RowThreshold,
pub vcs_ignoring: bool,
pub vcs: Option<&'a dyn VcsCache>,
pub console_width: usize,
}
struct RenderedGrid {
cells: Vec<String>,
column_count: usize,
row_count: usize,
}
impl<'a> Render<'a> {
fn details_for_column(&self) -> DetailsRender<'a> {
DetailsRender {
dir: self.dir,
files: Vec::new(),
theme: self.theme,
file_style: self.file_style,
opts: self.details,
recurse: None,
filter: self.filter,
vcs_ignoring: self.vcs_ignoring,
vcs: self.vcs,
}
}
pub fn give_up(self) -> DetailsRender<'a> {
DetailsRender {
dir: self.dir,
files: self.files,
theme: self.theme,
file_style: self.file_style,
opts: self.details,
recurse: None,
filter: self.filter,
vcs_ignoring: self.vcs_ignoring,
vcs: self.vcs,
}
}
pub fn render<W: Write>(mut self, w: &mut W) -> io::Result<()> {
if let Some(rendered) = self.find_fitting_grid() {
let direction = if self.grid.across { tg::Direction::LeftToRight }
else { tg::Direction::TopToBottom };
let grid = tg::Grid::new(rendered.cells, tg::GridOptions {
direction,
filling: tg::Filling::Spaces(4),
width: self.console_width,
});
write!(w, "{grid}")
}
else {
self.give_up().render(w).map(|_| ())
}
}
fn find_fitting_grid(&mut self) -> Option<RenderedGrid> {
let options = self.details.table.as_ref().expect("Details table options not given!");
let drender = self.details_for_column();
let (first_table, _) = self.make_table(options, &drender);
let rows = self.files.iter()
.map(|file| first_table.row_for_file(file, file_has_xattrs(file)))
.collect::<Vec<_>>();
let file_names = self.files.iter()
.map(|file| self.file_style.for_file(file, self.theme).paint().promote())
.collect::<Vec<_>>();
let mut last_working = self.make_rendered_grid(1, options, &file_names, rows.clone(), &drender);
if file_names.len() == 1 {
return Some(last_working);
}
for column_count in 2..100 {
let rendered = self.make_rendered_grid(column_count, options, &file_names, rows.clone(), &drender);
let the_grid_fits = rendered_grid_fits(&rendered, self.console_width);
if the_grid_fits {
last_working = rendered;
}
if !the_grid_fits || column_count == file_names.len() {
if let RowThreshold::MinimumRows(thresh) = self.row_threshold
&& last_working.row_count < thresh {
return None;
}
return Some(last_working);
}
}
None
}
fn make_table(&mut self, options: &'a TableOptions, drender: &DetailsRender<'_>) -> (Table<'a>, Vec<DetailsRow>) {
match (self.vcs, self.dir) {
(Some(g), Some(d)) => if ! g.has_anything_for(&d.path) { self.vcs = None },
(Some(g), None) => if ! self.files.iter().any(|f| g.has_anything_for(&f.path)) { self.vcs = None },
(None, _) => {},
}
let mut table = Table::new(options, self.vcs, self.theme);
let mut rows = Vec::new();
if self.details.header {
let row = table.header_row();
table.add_widths(&row);
rows.push(drender.render_header(row));
}
(table, rows)
}
fn make_rendered_grid(&mut self, column_count: usize, options: &'a TableOptions, file_names: &[TextCell], rows: Vec<TableRow>, drender: &DetailsRender<'_>) -> RenderedGrid {
let mut tables = Vec::new();
for _ in 0 .. column_count {
tables.push(self.make_table(options, drender));
}
let mut num_cells = rows.len();
if self.details.header {
num_cells += column_count;
}
let original_height = divide_rounding_up(rows.len(), column_count);
let height = divide_rounding_up(num_cells, column_count);
for (i, (file_name, row)) in file_names.iter().zip(rows.into_iter()).enumerate() {
let index = if self.grid.across {
i % column_count
}
else {
i / original_height
};
let (ref mut table, ref mut rows) = tables[index];
table.add_widths(&row);
let details_row = drender.render_file(row, file_name.clone(), TreeParams::new(TreeDepth::root(), false));
rows.push(details_row);
}
let columns = tables
.into_iter()
.map(|(table, details_rows)| {
drender.iterate_with_table(table, details_rows)
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let mut cells = Vec::new();
if self.grid.across {
for row in 0 .. height {
for column in &columns {
if row < column.len() {
cells.push(AnsiStrings(&column[row].contents).to_string());
}
}
}
}
else {
for column in &columns {
for cell in column {
cells.push(AnsiStrings(&cell.contents).to_string());
}
}
}
RenderedGrid {
cells,
column_count,
row_count: height,
}
}
}
fn rendered_grid_fits(rendered: &RenderedGrid, console_width: usize) -> bool {
if rendered.cells.is_empty() {
return true;
}
let mut col_widths = vec![0usize; rendered.column_count];
for (i, cell) in rendered.cells.iter().enumerate() {
let col = i % rendered.column_count;
let w = visual_width(cell);
if w > col_widths[col] {
col_widths[col] = w;
}
}
let spacing = if rendered.column_count > 1 { 4 * (rendered.column_count - 1) } else { 0 };
let total: usize = col_widths.iter().sum::<usize>() + spacing;
total <= console_width
}
fn visual_width(s: &str) -> usize {
use unicode_width::UnicodeWidthChar;
let mut width = 0;
let mut in_escape = false;
for c in s.chars() {
if in_escape {
if c == 'm' {
in_escape = false;
}
continue;
}
if c == '\x1b' {
in_escape = true;
continue;
}
width += c.width().unwrap_or(0);
}
width
}
fn divide_rounding_up(a: usize, b: usize) -> usize {
let mut result = a / b;
if !a.is_multiple_of(b) {
result += 1;
}
result
}
fn file_has_xattrs(file: &File<'_>) -> bool {
match file.path.attributes() {
Ok(attrs) => ! attrs.is_empty(),
Err(_) => false,
}
}