lsd/src/display.rs

450 lines
13 KiB
Rust
Raw Normal View History

use crate::color::{ColoredString, Colors};
2019-10-24 15:18:42 +00:00
use crate::flags::{Block, Display, Flags, Layout};
2019-02-09 10:41:42 +00:00
use crate::icon::Icons;
2019-10-23 14:47:10 +00:00
use crate::meta::{FileType, Meta};
2019-01-20 10:22:14 +00:00
use ansi_term::{ANSIString, ANSIStrings};
use std::collections::HashMap;
2018-12-02 16:22:51 +00:00
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
use terminal_size::terminal_size;
use unicode_width::UnicodeWidthStr;
2018-12-02 16:22:51 +00:00
2018-12-13 15:50:47 +00:00
const EDGE: &str = "\u{251c}\u{2500}\u{2500}"; // "├──"
const LINE: &str = "\u{2502} "; // "├ "
const CORNER: &str = "\u{2514}\u{2500}\u{2500}"; // "└──"
2019-01-23 17:02:36 +00:00
const BLANK: &str = " ";
2018-12-04 13:54:56 +00:00
2019-10-23 15:26:39 +00:00
pub fn grid(metas: &[Meta], flags: &Flags, colors: &Colors, icons: &Icons) -> String {
2019-01-20 10:22:14 +00:00
let term_width = match terminal_size() {
Some((w, _)) => Some(w.0 as usize),
None => None,
2019-01-20 10:22:14 +00:00
};
inner_display_grid(metas, &flags, colors, icons, 0, term_width)
2019-01-20 10:22:14 +00:00
}
2019-10-23 15:26:39 +00:00
pub fn tree(metas: &[Meta], flags: &Flags, colors: &Colors, icons: &Icons) -> String {
inner_display_tree(metas, &flags, colors, icons, 0, "")
2019-01-20 10:22:14 +00:00
}
2019-10-24 15:18:42 +00:00
fn inner_display_grid(
2019-10-23 15:26:39 +00:00
metas: &[Meta],
flags: &Flags,
2019-01-20 10:22:14 +00:00
colors: &Colors,
icons: &Icons,
depth: usize,
2019-10-24 15:18:42 +00:00
term_width: Option<usize>,
2019-01-20 10:22:14 +00:00
) -> String {
let mut output = String::new();
2019-10-24 13:42:26 +00:00
let padding_rules = get_padding_rules(&metas, flags);
let mut grid = match flags.layout {
Layout::OneLine => Grid::new(GridOptions {
filling: Filling::Spaces(1),
direction: Direction::LeftToRight,
}),
_ => Grid::new(GridOptions {
filling: Filling::Spaces(2),
direction: Direction::TopToBottom,
}),
};
2018-12-02 16:22:51 +00:00
2019-05-24 13:30:36 +00:00
// The first iteration (depth == 0) corresponds to the inputs given by the
// user. We defer displaying directories given by the user unless we've been
// asked to display the directory itself (rather than its contents).
let skip_dirs = (depth == 0) && (flags.display != Display::DisplayDirectoryItself);
2019-01-20 10:22:14 +00:00
// print the files first.
2019-10-23 15:26:39 +00:00
for meta in metas {
2019-05-24 13:30:36 +00:00
// Maybe skip showing the directory meta now; show its contents later.
if let (true, FileType::Directory { .. }) = (skip_dirs, meta.file_type) {
continue;
}
let blocks = get_output(&meta, &colors, &icons, &flags, &padding_rules);
for block in blocks {
let block_str = block.to_string();
grid.add(Cell {
width: get_visible_width(&block_str),
contents: block_str,
});
}
2018-12-02 16:22:51 +00:00
}
2019-10-24 15:18:42 +00:00
if flags.layout == Layout::Grid {
if let Some(tw) = term_width {
if let Some(gridded_output) = grid.fit_into_width(tw) {
output += &gridded_output.to_string();
} else {
//does not fit into grid, usually because (some) filename(s)
//are longer or almost as long as term_width
//print line by line instead!
output += &grid.fit_into_columns(1).to_string();
2019-01-20 10:22:14 +00:00
}
} else {
output += &grid.fit_into_columns(1).to_string();
}
2019-01-20 10:22:14 +00:00
} else {
2019-10-24 15:18:42 +00:00
output += &grid.fit_into_columns(flags.blocks.len()).to_string();
2018-12-02 16:22:51 +00:00
}
2019-01-20 10:22:14 +00:00
let should_display_folder_path = should_display_folder_path(depth, &metas);
2019-01-12 15:17:40 +00:00
2019-01-20 10:22:14 +00:00
// print the folder content
for meta in metas {
if meta.content.is_some() {
if should_display_folder_path {
output += &display_folder_path(&meta);
2019-01-12 15:17:40 +00:00
}
2019-01-20 10:22:14 +00:00
output += &inner_display_grid(
2019-10-24 15:18:42 +00:00
meta.content.as_ref().unwrap(),
&flags,
2019-01-20 10:22:14 +00:00
colors,
icons,
depth + 1,
term_width,
);
2019-01-12 15:17:40 +00:00
}
}
2019-01-20 10:22:14 +00:00
output
}
fn inner_display_tree(
2019-10-23 15:26:39 +00:00
metas: &[Meta],
flags: &Flags,
2019-01-20 10:22:14 +00:00
colors: &Colors,
icons: &Icons,
depth: usize,
2019-01-23 17:02:36 +00:00
prefix: &str,
2019-01-20 10:22:14 +00:00
) -> String {
let mut output = String::new();
let last_idx = metas.len();
2019-10-24 13:42:26 +00:00
let padding_rules = get_padding_rules(&metas, flags);
let mut grid = Grid::new(GridOptions {
2019-11-06 10:02:53 +00:00
filling: Filling::Spaces(1),
direction: Direction::LeftToRight,
});
2019-10-30 09:17:02 +00:00
for meta in metas.iter() {
for block in get_output(&meta, &colors, &icons, &flags, &padding_rules) {
let block_str = block.to_string();
grid.add(Cell {
width: get_visible_width(&block_str),
contents: block_str,
});
}
}
let content = grid.fit_into_columns(flags.blocks.len()).to_string();
let mut lines = content.lines();
2019-10-30 09:17:02 +00:00
for (idx, meta) in metas.iter().enumerate() {
2019-01-20 10:22:14 +00:00
let is_last_folder_elem = idx + 1 != last_idx;
2018-12-04 13:54:56 +00:00
2019-01-23 17:02:36 +00:00
if depth > 0 {
output += prefix;
2018-12-04 13:54:56 +00:00
2019-01-23 17:02:36 +00:00
if is_last_folder_elem {
output += EDGE;
} else {
output += CORNER;
}
output += " ";
2018-12-04 13:54:56 +00:00
}
2019-10-30 09:17:02 +00:00
output += &String::from(lines.next().unwrap());
output += "\n";
2018-12-04 13:54:56 +00:00
2019-01-20 10:22:14 +00:00
if meta.content.is_some() {
2019-01-23 17:02:36 +00:00
let mut new_prefix = String::from(prefix);
if depth > 0 {
if is_last_folder_elem {
new_prefix += LINE;
} else {
new_prefix += BLANK;
}
}
output += &inner_display_tree(
2019-10-23 15:26:39 +00:00
&meta.content.as_ref().unwrap(),
&flags,
2019-01-23 17:02:36 +00:00
colors,
icons,
depth + 1,
&new_prefix,
);
2019-01-20 10:22:14 +00:00
}
2018-12-04 13:54:56 +00:00
}
2019-01-20 10:22:14 +00:00
output
}
fn should_display_folder_path(depth: usize, metas: &[Meta]) -> bool {
if depth > 0 {
true
} else {
let folder_number = metas
.iter()
.filter(|x| match x.file_type {
FileType::Directory { .. } => true,
_ => false,
})
.count();
folder_number > 1 || folder_number < metas.len()
}
}
fn display_folder_path(meta: &Meta) -> String {
let mut output = String::new();
output.push('\n');
output += &meta.path.to_string_lossy();
output += ":\n";
output
}
fn get_output<'a>(
meta: &'a Meta,
colors: &'a Colors,
icons: &'a Icons,
flags: &'a Flags,
padding_rules: &HashMap<Block, usize>,
) -> Vec<ANSIString<'a>> {
let mut strings: Vec<ANSIString> = Vec::new();
for block in flags.blocks.iter() {
match block {
Block::INode => {
strings.push(meta.inode.render(colors));
}
Block::Permission => {
let s: &[ColoredString] = &[
meta.file_type.render(colors),
meta.permissions.render(colors),
];
let res = ANSIStrings(s).to_string();
strings.push(ColoredString::from(res));
}
Block::User => strings.push(meta.owner.render_user(colors)),
Block::Group => strings.push(meta.owner.render_group(colors)),
Block::Size => strings.push(meta.size.render(
colors,
2019-10-23 14:47:10 +00:00
&flags,
padding_rules[&Block::SizeValue],
)),
2019-10-23 14:47:10 +00:00
Block::SizeValue => strings.push(meta.size.render_value(colors, flags)),
2019-10-24 13:42:26 +00:00
Block::Date => strings.push(meta.date.render(colors, &flags)),
2019-05-18 13:49:29 +00:00
Block::Name => {
2019-10-29 13:23:36 +00:00
let s: String = if flags.no_symlink {
ANSIStrings(&[
meta.name.render(colors, icons),
meta.indicator.render(&flags),
])
.to_string()
} else {
ANSIStrings(&[
meta.name.render(colors, icons),
meta.indicator.render(&flags),
meta.symlink.render(colors),
])
.to_string()
};
strings.push(ColoredString::from(s));
2019-05-18 13:49:29 +00:00
}
};
}
strings
2019-01-20 10:22:14 +00:00
}
fn get_visible_width(input: &str) -> usize {
let mut nb_invisible_char = 0;
// If the input has color, do not compute the length contributed by the color to the actual length
for (idx, _) in input.match_indices("\u{1b}[") {
let (_, s) = input.split_at(idx);
let m_pos = s.find('m');
if let Some(len) = m_pos {
2019-10-30 09:17:02 +00:00
nb_invisible_char += len
}
2019-01-20 10:22:14 +00:00
}
UnicodeWidthStr::width(input) - nb_invisible_char
}
fn detect_size_lengths(metas: &[Meta], flags: &Flags) -> usize {
2019-01-20 10:22:14 +00:00
let mut max_value_length: usize = 0;
2018-12-02 16:22:51 +00:00
2019-01-20 10:22:14 +00:00
for meta in metas {
2019-10-23 15:34:57 +00:00
let value_len = meta.size.value_string(flags).len();
if value_len > max_value_length {
max_value_length = value_len;
}
2018-12-02 16:22:51 +00:00
}
2019-01-20 10:22:14 +00:00
max_value_length
2018-12-02 16:22:51 +00:00
}
2019-05-04 10:56:27 +00:00
2019-10-24 13:42:26 +00:00
fn get_padding_rules(metas: &[Meta], flags: &Flags) -> HashMap<Block, usize> {
let mut padding_rules: HashMap<Block, usize> = HashMap::new();
2019-10-24 13:42:26 +00:00
if flags.blocks.contains(&Block::Size) {
let size_val = detect_size_lengths(&metas, &flags);
2019-10-24 13:42:26 +00:00
padding_rules.insert(Block::SizeValue, size_val);
}
padding_rules
}
#[cfg(test)]
mod tests {
use super::*;
2019-02-09 10:41:42 +00:00
use crate::color;
use crate::color::Colors;
use crate::icon;
use crate::icon::Icons;
use crate::meta::{FileType, Name};
2018-12-20 02:51:44 +00:00
use std::path::Path;
#[test]
fn test_display_get_visible_width_without_icons() {
for (s, l) in &[
(",!", 22),
("ASCII1234-_", 11),
("制作样本。", 10),
("日本語", 6),
("샘플은 무료로 드리겠습니다", 26),
("👩🐩", 4),
("🔬", 2),
] {
let path = Path::new(s);
let name = Name::new(
&path,
FileType::File {
exec: false,
uid: false,
},
);
2018-12-20 02:51:44 +00:00
let output = name.render(
&Colors::new(color::Theme::NoColor),
&Icons::new(icon::Theme::NoIcon),
2018-12-20 02:51:44 +00:00
);
2019-01-20 10:22:14 +00:00
assert_eq!(get_visible_width(&output), *l);
}
}
#[test]
fn test_display_get_visible_width_with_icons() {
for (s, l) in &[
// Add 3 characters for the icons.
2019-11-04 16:10:09 +00:00
(",!", 24),
("ASCII1234-_", 13),
("File with space", 17),
("制作样本。", 12),
("日本語", 8),
("샘플은 무료로 드리겠습니다", 28),
("👩🐩", 6),
("🔬", 4),
] {
let path = Path::new(s);
let name = Name::new(
&path,
FileType::File {
exec: false,
uid: false,
},
);
let output = name
.render(
&Colors::new(color::Theme::NoColor),
&Icons::new(icon::Theme::Fancy),
)
.to_string();
2019-01-20 10:22:14 +00:00
assert_eq!(get_visible_width(&output), *l);
}
}
#[test]
fn test_display_get_visible_width_with_colors() {
for (s, l) in &[
2019-03-11 15:12:16 +00:00
(",!", 22),
("ASCII1234-_", 11),
2019-03-11 15:12:16 +00:00
("File with space", 15),
("制作样本。", 10),
("日本語", 6),
("샘플은 무료로 드리겠습니다", 26),
("👩🐩", 4),
("🔬", 2),
] {
let path = Path::new(s);
let name = Name::new(
&path,
FileType::File {
exec: false,
uid: false,
},
);
let output = name
.render(
&Colors::new(color::Theme::NoLscolors),
&Icons::new(icon::Theme::NoIcon),
)
.to_string();
// check if the color is present.
assert_eq!(true, output.starts_with("\u{1b}[38;5;"));
assert_eq!(true, output.ends_with("[0m"));
2019-03-11 15:12:16 +00:00
assert_eq!(get_visible_width(&output), *l);
}
}
#[test]
fn test_display_get_visible_width_without_colors() {
for (s, l) in &[
(",!", 22),
("ASCII1234-_", 11),
("File with space", 15),
("制作样本。", 10),
("日本語", 6),
("샘플은 무료로 드리겠습니다", 26),
("👩🐩", 4),
("🔬", 2),
] {
let path = Path::new(s);
let name = Name::new(
&path,
FileType::File {
exec: false,
uid: false,
},
);
let output = name
.render(
&Colors::new(color::Theme::NoColor),
&Icons::new(icon::Theme::NoIcon),
)
.to_string();
// check if the color is present.
assert_eq!(false, output.starts_with("\u{1b}[38;5;"));
assert_eq!(false, output.ends_with("[0m"));
2019-01-20 10:22:14 +00:00
assert_eq!(get_visible_width(&output), *l);
}
}
}