mirror of
https://github.com/Llewellynvdm/exa.git
synced 2024-06-05 17:00:49 +00:00
f1e3e7c7ff
This commit makes adding icons to file names something that the file name renderer does, rather than something that each individual view does. This is now possible thanks to the previous commit a1869f2
, which moved the option to do this into the same module. The repeated code has been removed.
It happens to fix a bug where the width of each column was being incorrectly calculated for the grid-details view, making lines slightly too long for the terminal because the icon wasn't being taken into account.
65 lines
1.7 KiB
Rust
65 lines
1.7 KiB
Rust
use std::io::{self, Write};
|
||
|
||
use term_grid as tg;
|
||
|
||
use crate::fs::File;
|
||
use crate::output::file_name::Options as FileStyle;
|
||
use crate::theme::Theme;
|
||
|
||
|
||
#[derive(PartialEq, Debug, Copy, Clone)]
|
||
pub struct Options {
|
||
pub across: bool,
|
||
}
|
||
|
||
impl Options {
|
||
pub fn direction(self) -> tg::Direction {
|
||
if self.across { tg::Direction::LeftToRight }
|
||
else { tg::Direction::TopToBottom }
|
||
}
|
||
}
|
||
|
||
|
||
pub struct Render<'a> {
|
||
pub files: Vec<File<'a>>,
|
||
pub theme: &'a Theme,
|
||
pub file_style: &'a FileStyle,
|
||
pub opts: &'a Options,
|
||
pub console_width: usize,
|
||
}
|
||
|
||
impl<'a> Render<'a> {
|
||
pub fn render<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
||
let mut grid = tg::Grid::new(tg::GridOptions {
|
||
direction: self.opts.direction(),
|
||
filling: tg::Filling::Spaces(2),
|
||
});
|
||
|
||
grid.reserve(self.files.len());
|
||
|
||
for file in &self.files {
|
||
let filename = self.file_style.for_file(file, self.theme).paint();
|
||
|
||
grid.add(tg::Cell {
|
||
contents: filename.strings().to_string(),
|
||
width: *filename.width(),
|
||
});
|
||
}
|
||
|
||
if let Some(display) = grid.fit_into_width(self.console_width) {
|
||
write!(w, "{}", display)
|
||
}
|
||
else {
|
||
// File names too long for a grid - drop down to just listing them!
|
||
// This isn’t *quite* the same as the lines view, which also
|
||
// displays full link paths.
|
||
for file in &self.files {
|
||
let name_cell = self.file_style.for_file(file, self.theme).paint();
|
||
writeln!(w, "{}", name_cell.strings())?;
|
||
}
|
||
|
||
Ok(())
|
||
}
|
||
}
|
||
}
|