exa/src/output/lines.rs

49 lines
1.4 KiB
Rust
Raw Normal View History

use std::io::{Write, Result as IOResult};
2018-04-01 23:00:27 +00:00
use ansi_term::{ANSIStrings, ANSIGenericString};
2018-12-07 23:43:31 +00:00
use crate::fs::File;
use crate::output::cell::TextCell;
2018-12-07 23:43:31 +00:00
use crate::output::file_name::{FileName, FileStyle};
use crate::output::icons::painted_icon;
use crate::style::Colours;
2018-04-01 23:00:27 +00:00
#[derive(PartialEq, Debug, Copy, Clone)]
pub struct Options {
pub icons: bool,
2018-04-01 23:00:27 +00:00
}
2015-06-08 20:33:39 +00:00
/// The lines view literally just displays each file, line-by-line.
pub struct Render<'a> {
pub files: Vec<File<'a>>,
pub colours: &'a Colours,
pub style: &'a FileStyle,
2018-04-01 23:00:27 +00:00
pub opts: &'a Options,
}
impl<'a> Render<'a> {
pub fn render<W: Write>(&self, w: &mut W) -> IOResult<()> {
for file in &self.files {
let name_cell = self.render_file(file).paint();
2018-04-01 23:00:27 +00:00
if self.opts.icons {
// Create a TextCell for the icon then append the text to it
let mut cell = TextCell::default();
let icon = painted_icon(file, self.style);
2018-04-01 23:00:27 +00:00
cell.push(ANSIGenericString::from(icon), 2);
cell.append(name_cell.promote());
writeln!(w, "{}", ANSIStrings(&cell))?;
}
else {
2018-04-01 23:00:27 +00:00
writeln!(w, "{}", ANSIStrings(&name_cell))?;
}
}
Ok(())
}
Extract trait above file name colours This commit meddles about with both the Colours and the FileExtensions. Even though all the renderable fields were turned into traits, the FileName struct kept on accessing fields directly on the Colours value instead of calling methods on it. It also did the usual amount of colour misappropriation (such as ‘punctuation’ instead of specifying ‘normal_arrow’) In preparation for when custom file colours are configurable (any day now), the colourise-file-by-kind functionality (links, sockets, or directories) was separated from the colourise-file-by-name functionality (images, videos, archives). The FileStyle struct already allowed for both to be separate; it was only changed so that a type other than FileExtensions could be used instead, as long as it implements the FileColours trait. (I feel like I should re-visit the naming of all these at some point in the future) The decision to separate the two means that FileExtensions is the one assigning the colours, rather than going through the fields on a Colours value, which have all been removed. This is why a bunch of arbitrary Styles now exist in filetype.rs. Because the decision on which colourise-file-by-name code to use (currently just the standard extensions, or nothing if we aren’t colourising) is now determined by the Colours type (instead of being derived), it’s possible to get it wrong. And wrong it was! There was a bug where file names were colourised even though the rest of the --long output wasn’t, and this wasn’t caught by the xtests. It is now.
2017-08-26 19:43:47 +00:00
fn render_file<'f>(&self, file: &'f File<'a>) -> FileName<'f, 'a, Colours> {
self.style.for_file(file, self.colours).with_link_paths()
}
}