exa/src/output/grid.rs
Benjamin Sago 0d613652a7 Replace FileName::new with a factory
The new FileStyles value will contain all the fields necessary to “style” a file’s name. Right now this is only the Classify field, but there can be more later. The benefit of this is that when we add more, we won’t need to update all the places where file names are displayed.
2017-07-08 12:11:11 +01:00

63 lines
1.7 KiB
Rust

use std::io::{Write, Result as IOResult};
use term_grid as tg;
use fs::File;
use output::colours::Colours;
use output::file_name::{FileStyle, LinkStyle};
#[derive(PartialEq, Debug, Copy, Clone)]
pub struct Options {
pub across: bool,
pub console_width: usize,
}
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 colours: &'a Colours,
pub style: &'a FileStyle,
pub opts: &'a Options,
}
impl<'a> Render<'a> {
pub fn render<W: Write>(&self, w: &mut W) -> IOResult<()> {
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.iter() {
let filename = self.style.for_file(file, LinkStyle::JustFilenames, self.colours).paint();
let width = filename.width();
grid.add(tg::Cell {
contents: filename.strings().to_string(),
width: *width,
});
}
if let Some(display) = grid.fit_into_width(self.opts.console_width) {
write!(w, "{}", display)
}
else {
// File names too long for a grid - drop down to just listing them!
for file in self.files.iter() {
let name_cell = self.style.for_file(file, LinkStyle::JustFilenames, self.colours).paint();
writeln!(w, "{}", name_cell.strings())?;
}
Ok(())
}
}
}