From e85460998ee4366553338190e0e1264d436125d1 Mon Sep 17 00:00:00 2001 From: Ben S Date: Wed, 4 Feb 2015 00:59:27 +0000 Subject: [PATCH] Replace hash marks with decent-looking tree This commit has been brought to you by Unicode Box-Drawing Characters! --- src/output.rs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/output.rs b/src/output.rs index 05bc1ff..dab2b54 100644 --- a/src/output.rs +++ b/src/output.rs @@ -4,7 +4,7 @@ use std::iter::{AdditiveIterator, repeat}; use column::{Column, Cell}; use column::Alignment::Left; use dir::Dir; -use file::File; +use file::{File, GREY}; use options::{Columns, FileFilter}; use users::OSUsers; @@ -138,7 +138,9 @@ fn details_view(columns: &[Column], files: &[File], header: bool, tree: bool, fi let row = Row { depth: 0, cells: columns.iter().map(|c| Cell::paint(Plain.underline(), c.header())).collect(), - name: Plain.underline().paint("Name").to_string() + name: Plain.underline().paint("Name").to_string(), + last: false, + children: false, }; table.insert(0, row); @@ -148,6 +150,8 @@ fn details_view(columns: &[Column], files: &[File], header: bool, tree: bool, fi .map(|n| table.iter().map(|row| row.cells[n].length).max().unwrap_or(0)) .collect(); + let mut stack = Vec::new(); + for row in table.iter() { for (num, column) in columns.iter().enumerate() { let padding = column_widths[num] - row.cells[num].length; @@ -155,24 +159,35 @@ fn details_view(columns: &[Column], files: &[File], header: bool, tree: bool, fi } if tree { - for _ in range(0, row.depth) { - print!("#"); + stack.resize(row.depth + 1, "├──"); + stack[row.depth ] = if row.last { "└──" } else { "├──" }; + + for i in range(1, row.depth + 1) { + print!("{}", GREY.paint(stack[i ])); } - print!(" "); + if row.children { + stack[row.depth ] = if row.last { " " } else { "│ " }; + } + + if row.depth != 0 { + print!(" "); + } } print!("{}\n", row.name); } } -fn get_files(columns: &[Column], cache: &mut OSUsers, recurse: bool, dest: &mut Vec, src: &[File], depth: u8, filter: FileFilter) { - for file in src.iter() { +fn get_files(columns: &[Column], cache: &mut OSUsers, recurse: bool, dest: &mut Vec, src: &[File], depth: usize, filter: FileFilter) { + for (index, file) in src.iter().enumerate() { let row = Row { depth: depth, cells: columns.iter().map(|c| file.display(c, cache)).collect(), name: file.file_name_view(), + last: index == src.len() - 1, + children: file.this.is_some(), }; dest.push(row); @@ -187,7 +202,9 @@ fn get_files(columns: &[Column], cache: &mut OSUsers, recurse: bool, dest: &mut } struct Row { - pub depth: u8, + pub depth: usize, pub cells: Vec, pub name: String, + pub last: bool, + pub children: bool, }