mirror of
https://github.com/bootandy/dust.git
synced 2025-12-05 20:40:11 -08:00
Compare commits
2 Commits
9957cd3d9c
...
d7fa260bba
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d7fa260bba | ||
|
|
5057337340 |
@@ -32,6 +32,7 @@ pub struct InitialDisplayData {
|
||||
pub is_screen_reader: bool,
|
||||
pub output_format: String,
|
||||
pub bars_on_right: bool,
|
||||
pub selected_index: i32,
|
||||
}
|
||||
|
||||
pub struct DisplayData {
|
||||
@@ -84,6 +85,7 @@ struct DrawData<'a> {
|
||||
display_data: &'a DisplayData,
|
||||
}
|
||||
|
||||
|
||||
impl DrawData<'_> {
|
||||
fn get_new_indent(&self, has_children: bool, was_i_last: bool) -> String {
|
||||
let chars = self.display_data.get_tree_chars(was_i_last, has_children);
|
||||
@@ -147,8 +149,6 @@ pub fn draw_it(
|
||||
terminal_width > num_chars_needed_on_left_most + 2,
|
||||
"Not enough terminal width"
|
||||
);
|
||||
// write!(stdout, "{}{}Hello!", termion::clear::All, termion::cursor::Goto(1, 1)).unwrap();
|
||||
// write!(stdout, "{}", termion::cursor::Goto(2, 1)).unwrap();
|
||||
|
||||
let allowed_width = terminal_width - num_chars_needed_on_left_most - 2;
|
||||
let num_indent_chars = 3;
|
||||
@@ -176,8 +176,14 @@ pub fn draw_it(
|
||||
display_data: &display_data,
|
||||
};
|
||||
|
||||
let mut test = if display_data.initial.is_reversed {
|
||||
recursive_child_count(root_node)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
if !skip_total {
|
||||
display_node(root_node, stdout, &draw_data, true, true);
|
||||
display_node(root_node, stdout, &draw_data, true, true, test);
|
||||
} else {
|
||||
for (count, c) in root_node
|
||||
.get_children_from_node(draw_data.display_data.initial.is_reversed)
|
||||
@@ -185,7 +191,13 @@ pub fn draw_it(
|
||||
{
|
||||
let is_biggest = display_data.is_biggest(count, root_node.num_siblings());
|
||||
let was_i_last = display_data.is_last(count, root_node.num_siblings());
|
||||
display_node(c, stdout, &draw_data, is_biggest, was_i_last);
|
||||
display_node(c, stdout, &draw_data, is_biggest, was_i_last, test);
|
||||
// not yet tested:
|
||||
if display_data.initial.is_reversed {
|
||||
test += recursive_child_count(c);
|
||||
} else {
|
||||
test += recursive_child_count(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -224,27 +236,45 @@ fn find_longest_dir_name(
|
||||
.fold(longest, max)
|
||||
}
|
||||
|
||||
pub fn recursive_child_count(node: &DisplayNode) -> i32 {
|
||||
let mut total = 1;
|
||||
for n in node.children.iter() {
|
||||
total += recursive_child_count(&n);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
fn display_node(
|
||||
node: &DisplayNode,
|
||||
stdout: &mut RawTerminal<Stdout>,
|
||||
draw_data: &DrawData,
|
||||
is_biggest: bool,
|
||||
is_last: bool,
|
||||
test: i32,
|
||||
) {
|
||||
// hacky way of working out how deep we are in the tree
|
||||
let indent = draw_data.get_new_indent(!node.children.is_empty(), is_last);
|
||||
let level = ((indent.chars().count() - 1) / 2) - 1;
|
||||
let bar_text = draw_data.generate_bar(node, level);
|
||||
|
||||
let cnt = if draw_data.display_data.initial.is_reversed {
|
||||
recursive_child_count(node)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
let to_print = format_string(
|
||||
node,
|
||||
&indent,
|
||||
&bar_text,
|
||||
is_biggest,
|
||||
&draw_data.display_data,
|
||||
test - cnt,
|
||||
);
|
||||
|
||||
let mut tt = test;
|
||||
if !draw_data.display_data.initial.is_reversed {
|
||||
tt += 1;
|
||||
write!(stdout, "{to_print}").unwrap()
|
||||
}
|
||||
|
||||
@@ -262,7 +292,13 @@ fn display_node(
|
||||
{
|
||||
let is_biggest = dd.display_data.is_biggest(count, num_siblings);
|
||||
let was_i_last = dd.display_data.is_last(count, num_siblings);
|
||||
display_node(c, stdout, &dd, is_biggest, was_i_last);
|
||||
|
||||
display_node(c, stdout, &dd, is_biggest, was_i_last, tt);
|
||||
if draw_data.display_data.initial.is_reversed {
|
||||
tt -= recursive_child_count(c)
|
||||
} else {
|
||||
tt += recursive_child_count(c)
|
||||
}
|
||||
}
|
||||
|
||||
if draw_data.display_data.initial.is_reversed {
|
||||
@@ -343,18 +379,22 @@ pub fn format_string(
|
||||
bars: &str,
|
||||
is_biggest: bool,
|
||||
display_data: &DisplayData,
|
||||
test: i32,
|
||||
) -> String {
|
||||
let (percent, name_and_padding) = get_name_percent(node, indent, bars, display_data);
|
||||
let pretty_size = get_pretty_size(node, is_biggest, display_data);
|
||||
let pretty_size = get_pretty_size(node, is_biggest, display_data, test);
|
||||
let pretty_name = get_pretty_name(node, name_and_padding, display_data);
|
||||
let marked = get_name_if_marked(test==display_data.initial.selected_index, pretty_name);
|
||||
let indent = get_indent_if_marked(test==display_data.initial.selected_index, indent);
|
||||
|
||||
// we can clean this and the method below somehow, not sure yet
|
||||
if display_data.initial.is_screen_reader {
|
||||
// if screen_reader then bars is 'depth'
|
||||
format!("{pretty_name} {bars} {pretty_size}{percent}")
|
||||
format!("{marked} {bars} {pretty_size}{percent}")
|
||||
} else if display_data.initial.by_filetime.is_some() {
|
||||
format!("{pretty_size} {indent}{pretty_name}")
|
||||
format!("{pretty_size} {indent}{marked}")
|
||||
} else {
|
||||
format!("{pretty_size} {indent} {pretty_name}{percent}")
|
||||
format!("{pretty_size} {indent} {marked}{percent}")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -384,13 +424,19 @@ fn get_name_percent(
|
||||
}
|
||||
}
|
||||
|
||||
fn get_pretty_size(node: &DisplayNode, is_biggest: bool, display_data: &DisplayData) -> String {
|
||||
fn get_pretty_size(
|
||||
node: &DisplayNode,
|
||||
is_biggest: bool,
|
||||
display_data: &DisplayData,
|
||||
n: i32,
|
||||
) -> String {
|
||||
let output = if display_data.initial.by_filecount {
|
||||
node.size.separate_with_commas()
|
||||
} else if display_data.initial.by_filetime.is_some() {
|
||||
get_pretty_file_modified_time(node.size as i64)
|
||||
} else {
|
||||
human_readable_number(node.size, &display_data.initial.output_format)
|
||||
// human_readable_number(n, &display_data.initial.output_format)
|
||||
format!("{n}")
|
||||
};
|
||||
let spaces_to_add = display_data.num_chars_needed_on_left_most - output.chars().count();
|
||||
let output = " ".repeat(spaces_to_add) + output.as_str();
|
||||
@@ -410,6 +456,33 @@ fn get_pretty_file_modified_time(timestamp: i64) -> String {
|
||||
local_datetime.format("%Y-%m-%dT%H:%M:%S").to_string()
|
||||
}
|
||||
|
||||
fn get_indent_if_marked(test: bool, indent: &str) -> String {
|
||||
if test {
|
||||
let mut new_name = String::new();
|
||||
for _ in indent.chars() {
|
||||
new_name.push(BLOCKS[0])
|
||||
}
|
||||
new_name
|
||||
} else {
|
||||
indent.into()
|
||||
}
|
||||
}
|
||||
fn get_name_if_marked(test: bool, name: String) -> String {
|
||||
if test {
|
||||
let mut new_name = String::new();
|
||||
for c in name.chars() {
|
||||
if c == ' ' {
|
||||
new_name.push(BLOCKS[0])
|
||||
} else {
|
||||
new_name.push(c)
|
||||
}
|
||||
}
|
||||
new_name
|
||||
} else {
|
||||
name.into()
|
||||
}
|
||||
}
|
||||
|
||||
fn get_pretty_name(
|
||||
node: &DisplayNode,
|
||||
name_and_padding: String,
|
||||
|
||||
41
src/main.rs
41
src/main.rs
@@ -12,6 +12,7 @@ mod utils;
|
||||
|
||||
use crate::cli::Cli;
|
||||
use crate::config::Config;
|
||||
use crate::display::recursive_child_count;
|
||||
use crate::display_node::DisplayNode;
|
||||
use crate::progress::RuntimeErrors;
|
||||
use clap::Parser;
|
||||
@@ -20,6 +21,7 @@ use display::InitialDisplayData;
|
||||
use filter::AggregateData;
|
||||
use progress::PIndicator;
|
||||
use regex::Error;
|
||||
use std::cmp::min;
|
||||
use std::collections::HashSet;
|
||||
use std::env;
|
||||
use std::fs::read_to_string;
|
||||
@@ -336,13 +338,15 @@ fn main() {
|
||||
is_colors,
|
||||
terminal_width,
|
||||
&mut out,
|
||||
0,
|
||||
);
|
||||
out.flush().unwrap();
|
||||
|
||||
let mut state = 0;
|
||||
for c in stdin.events() {
|
||||
write!(
|
||||
out,
|
||||
"{}{}Dust interactive (q to quit)",
|
||||
"{}{}Dust interactive (q to quit) {state}",
|
||||
termion::clear::All,
|
||||
termion::cursor::Goto(1, 1)
|
||||
)
|
||||
@@ -351,17 +355,35 @@ fn main() {
|
||||
let evt = c.unwrap();
|
||||
match evt {
|
||||
Event::Key(Key::Char('q')) => break,
|
||||
Event::Key(Key::Up | Key::Char('k')) => {
|
||||
write!(out, "up\n").unwrap();
|
||||
if !config.get_reverse(&options){
|
||||
state += 1;
|
||||
} else {
|
||||
state -= 1;
|
||||
}
|
||||
}
|
||||
Event::Key(Key::Down | Key::Char('j')) => {
|
||||
write!(out, "down\n").unwrap();
|
||||
if !config.get_reverse(&options){
|
||||
state -= 1;
|
||||
} else {
|
||||
state += 1;
|
||||
}
|
||||
}
|
||||
Event::Key(Key::Left | Key::Char('h')) => {
|
||||
write!(out, "left\n").unwrap();
|
||||
}
|
||||
Event::Key(Key::Right | Key::Char('l')) => {
|
||||
write!(out, "right\n").unwrap();
|
||||
}
|
||||
Event::Key(Key::Char(x)) => {
|
||||
write!(out,"{x} key\n" );
|
||||
}
|
||||
Event::Key(Key::Left) => {
|
||||
write!(out,"left\n");
|
||||
}
|
||||
Event::Key(Key::Right) => {
|
||||
write!(out,"right\n");
|
||||
write!(out, "{x} key\n").unwrap();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
state = max(0, state);
|
||||
state = min(recursive_child_count(&tree)-1, state);
|
||||
write!(out, "{}", termion::cursor::Goto(1, 3)).unwrap();
|
||||
print_output(
|
||||
&config,
|
||||
@@ -370,6 +392,7 @@ fn main() {
|
||||
is_colors,
|
||||
terminal_width,
|
||||
&mut out,
|
||||
state,
|
||||
);
|
||||
out.flush().unwrap();
|
||||
}
|
||||
@@ -383,6 +406,7 @@ fn print_output(
|
||||
is_colors: bool,
|
||||
terminal_width: usize,
|
||||
stdout: &mut RawTerminal<Stdout>,
|
||||
selected_index: i32,
|
||||
) {
|
||||
let output_format = config.get_output_format(&options);
|
||||
|
||||
@@ -401,6 +425,7 @@ fn print_output(
|
||||
is_screen_reader: config.get_screen_reader(&options),
|
||||
output_format,
|
||||
bars_on_right: config.get_bars_on_right(&options),
|
||||
selected_index: selected_index,
|
||||
};
|
||||
|
||||
draw_it(
|
||||
|
||||
Reference in New Issue
Block a user