Replace the links boolean with an enum field

This commit is contained in:
Benjamin Sago 2017-05-07 14:08:36 +01:00
parent 88fecb7b26
commit ccf8d44058
5 changed files with 28 additions and 18 deletions

View File

@ -101,7 +101,7 @@ use output::colours::Colours;
use output::column::{Alignment, Column, Columns, SizeFormat};
use output::cell::{TextCell, TextCellContents, DisplayWidth};
use output::tree::TreeTrunk;
use output::file_name::FileName;
use output::file_name::{FileName, LinkStyle};
/// With the **Details** view, the output gets formatted into columns, with
@ -310,7 +310,7 @@ impl Details {
let row = Row {
depth: depth,
cells: Some(egg.cells),
name: FileName::new(&egg.file, &self.colours).paint(true, self.classify).promote(),
name: FileName::new(&egg.file, LinkStyle::FullLinkPaths, &self.colours).paint(self.classify).promote(),
last: index == num_eggs - 1,
};
@ -443,8 +443,8 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
self.rows.push(row);
}
pub fn filename(&self, file: File, links: bool) -> TextCellContents {
FileName::new(&file, &self.opts.colours).paint(links, self.opts.classify)
pub fn filename(&self, file: File, links: LinkStyle) -> TextCellContents {
FileName::new(&file, links, &self.opts.colours).paint(self.opts.classify)
}
pub fn add_file_with_cells(&mut self, cells: Vec<TextCell>, name_cell: TextCell, depth: usize, last: bool) {

View File

@ -8,24 +8,32 @@ use output::escape;
use output::cell::TextCellContents;
pub enum LinkStyle {
JustFilenames,
FullLinkPaths,
}
pub struct FileName<'a, 'dir: 'a> {
file: &'a File<'dir>,
colours: &'a Colours,
target: Option<FileTarget<'dir>>,
link_style: LinkStyle,
}
impl<'a, 'dir> FileName<'a, 'dir> {
pub fn new(file: &'a File<'dir>, colours: &'a Colours) -> FileName<'a, 'dir> {
pub fn new(file: &'a File<'dir>, link_style: LinkStyle, colours: &'a Colours) -> FileName<'a, 'dir> {
let target = if file.is_link() { Some(file.link_target()) }
else { None };
FileName {
file: file,
colours: colours,
target: target,
link_style: link_style,
}
}
pub fn paint(&self, links: bool, classify: bool) -> TextCellContents {
pub fn paint(&self, classify: bool) -> TextCellContents {
let mut bits = Vec::new();
if self.file.dir.is_none() {
@ -40,9 +48,9 @@ impl<'a, 'dir> FileName<'a, 'dir> {
}
}
if links && self.target.is_some() {
match self.target.as_ref().unwrap() {
&FileTarget::Ok(ref target) => {
if let (&LinkStyle::FullLinkPaths, Some(ref target)) = (&self.link_style, self.target.as_ref()) {
match **target {
FileTarget::Ok(ref target) => {
bits.push(Style::default().paint(" "));
bits.push(self.colours.punctuation.paint("->"));
bits.push(Style::default().paint(" "));
@ -52,21 +60,21 @@ impl<'a, 'dir> FileName<'a, 'dir> {
}
if !target.name.is_empty() {
let target = FileName::new(&target, self.colours);
let target = FileName::new(&target, LinkStyle::FullLinkPaths, self.colours);
for bit in target.coloured_file_name() {
bits.push(bit);
}
}
},
&FileTarget::Broken(ref broken_path) => {
FileTarget::Broken(ref broken_path) => {
bits.push(Style::default().paint(" "));
bits.push(self.colours.broken_arrow.paint("->"));
bits.push(Style::default().paint(" "));
escape(broken_path.display().to_string(), &mut bits, self.colours.broken_filename, self.colours.control_char.underline());
},
&FileTarget::Err(_) => {
FileTarget::Err(_) => {
// Do nothing -- the error gets displayed on the next line
},
}

View File

@ -4,7 +4,7 @@ use term_grid as grid;
use fs::File;
use output::colours::Colours;
use output::file_name::FileName;
use output::file_name::{FileName, LinkStyle};
#[derive(PartialEq, Debug, Copy, Clone)]
@ -28,7 +28,7 @@ impl Grid {
grid.reserve(files.len());
for file in files.iter() {
let filename = FileName::new(file, &self.colours).paint(false, self.classify);
let filename = FileName::new(file, LinkStyle::JustFilenames, &self.colours).paint(self.classify);
let width = filename.width();
grid.add(grid::Cell {
@ -43,7 +43,7 @@ impl Grid {
else {
// File names too long for a grid - drop down to just listing them!
for file in files.iter() {
let name_cell = FileName::new(file, &self.colours).paint(false, self.classify);
let name_cell = FileName::new(file, LinkStyle::JustFilenames, &self.colours).paint(self.classify);
writeln!(w, "{}", name_cell.strings())?;
}
Ok(())

View File

@ -12,6 +12,8 @@ use output::cell::TextCell;
use output::column::Column;
use output::details::{Details, Table, Environment};
use output::grid::Grid;
use output::file_name::LinkStyle;
#[derive(PartialEq, Debug, Clone)]
pub struct GridDetails {
@ -45,7 +47,7 @@ impl GridDetails {
.collect::<Vec<_>>();
let file_names = files.into_iter()
.map(|file| first_table.filename(file, false).promote())
.map(|file| first_table.filename(file, LinkStyle::JustFilenames).promote())
.collect::<Vec<_>>();
(cells, file_names)

View File

@ -4,7 +4,7 @@ use ansi_term::ANSIStrings;
use fs::File;
use output::file_name::FileName;
use output::file_name::{FileName, LinkStyle};
use super::colours::Colours;
@ -18,7 +18,7 @@ pub struct Lines {
impl Lines {
pub fn view<W: Write>(&self, files: Vec<File>, w: &mut W) -> IOResult<()> {
for file in files {
let name_cell = FileName::new(&file, &self.colours).paint(true, self.classify);
let name_cell = FileName::new(&file, LinkStyle::FullLinkPaths, &self.colours).paint(self.classify);
writeln!(w, "{}", ANSIStrings(&name_cell))?;
}
Ok(())