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::column::{Alignment, Column, Columns, SizeFormat};
use output::cell::{TextCell, TextCellContents, DisplayWidth}; use output::cell::{TextCell, TextCellContents, DisplayWidth};
use output::tree::TreeTrunk; 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 /// With the **Details** view, the output gets formatted into columns, with
@ -310,7 +310,7 @@ impl Details {
let row = Row { let row = Row {
depth: depth, depth: depth,
cells: Some(egg.cells), 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, last: index == num_eggs - 1,
}; };
@ -443,8 +443,8 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
self.rows.push(row); self.rows.push(row);
} }
pub fn filename(&self, file: File, links: bool) -> TextCellContents { pub fn filename(&self, file: File, links: LinkStyle) -> TextCellContents {
FileName::new(&file, &self.opts.colours).paint(links, self.opts.classify) 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) { 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; use output::cell::TextCellContents;
pub enum LinkStyle {
JustFilenames,
FullLinkPaths,
}
pub struct FileName<'a, 'dir: 'a> { pub struct FileName<'a, 'dir: 'a> {
file: &'a File<'dir>, file: &'a File<'dir>,
colours: &'a Colours, colours: &'a Colours,
target: Option<FileTarget<'dir>>, target: Option<FileTarget<'dir>>,
link_style: LinkStyle,
} }
impl<'a, 'dir> FileName<'a, 'dir> { 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()) } let target = if file.is_link() { Some(file.link_target()) }
else { None }; else { None };
FileName { FileName {
file: file, file: file,
colours: colours, colours: colours,
target: target, 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(); let mut bits = Vec::new();
if self.file.dir.is_none() { if self.file.dir.is_none() {
@ -40,9 +48,9 @@ impl<'a, 'dir> FileName<'a, 'dir> {
} }
} }
if links && self.target.is_some() { if let (&LinkStyle::FullLinkPaths, Some(ref target)) = (&self.link_style, self.target.as_ref()) {
match self.target.as_ref().unwrap() { match **target {
&FileTarget::Ok(ref target) => { FileTarget::Ok(ref target) => {
bits.push(Style::default().paint(" ")); bits.push(Style::default().paint(" "));
bits.push(self.colours.punctuation.paint("->")); bits.push(self.colours.punctuation.paint("->"));
bits.push(Style::default().paint(" ")); bits.push(Style::default().paint(" "));
@ -52,21 +60,21 @@ impl<'a, 'dir> FileName<'a, 'dir> {
} }
if !target.name.is_empty() { 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() { for bit in target.coloured_file_name() {
bits.push(bit); bits.push(bit);
} }
} }
}, },
&FileTarget::Broken(ref broken_path) => { FileTarget::Broken(ref broken_path) => {
bits.push(Style::default().paint(" ")); bits.push(Style::default().paint(" "));
bits.push(self.colours.broken_arrow.paint("->")); bits.push(self.colours.broken_arrow.paint("->"));
bits.push(Style::default().paint(" ")); bits.push(Style::default().paint(" "));
escape(broken_path.display().to_string(), &mut bits, self.colours.broken_filename, self.colours.control_char.underline()); 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 // 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 fs::File;
use output::colours::Colours; use output::colours::Colours;
use output::file_name::FileName; use output::file_name::{FileName, LinkStyle};
#[derive(PartialEq, Debug, Copy, Clone)] #[derive(PartialEq, Debug, Copy, Clone)]
@ -28,7 +28,7 @@ impl Grid {
grid.reserve(files.len()); grid.reserve(files.len());
for file in files.iter() { 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(); let width = filename.width();
grid.add(grid::Cell { grid.add(grid::Cell {
@ -43,7 +43,7 @@ impl Grid {
else { else {
// File names too long for a grid - drop down to just listing them! // File names too long for a grid - drop down to just listing them!
for file in files.iter() { 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())?; writeln!(w, "{}", name_cell.strings())?;
} }
Ok(()) Ok(())

View File

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

View File

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