Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4112a9b

Browse files
author
Sebastian Thiel
committed
Separate Footer widget; refresh display before event loop
1 parent 7eb8574 commit 4112a9b

2 files changed

Lines changed: 75 additions & 34 deletions

File tree

src/interactive/app.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,32 @@ impl Traversal {
186186
}
187187

188188
/// State and methods representing the interactive disk usage analyser for the terminal
189-
#[derive(Default, Debug)]
190189
pub struct TerminalApp {
191190
pub traversal: Traversal,
191+
pub display: DisplayOptions,
192192
}
193193

194194
const GUI_REFRESH_RATE: Duration = Duration::from_millis(100);
195195

196196
impl TerminalApp {
197+
fn draw<B>(&self, terminal: &mut Terminal<B>) -> Result<(), Error>
198+
where
199+
B: Backend,
200+
{
201+
let Self { traversal, display } = self;
202+
terminal.draw(|mut f| {
203+
let full_screen = f.size();
204+
super::widgets::MainWindow {
205+
traversal,
206+
display: *display,
207+
}
208+
.render(&mut f, full_screen)
209+
})?;
210+
Ok(())
211+
}
197212
pub fn process_events<B, R>(
198213
&mut self,
199-
_terminal: &mut Terminal<B>,
214+
terminal: &mut Terminal<B>,
200215
keys: Keys<R>,
201216
) -> Result<WalkResult, Error>
202217
where
@@ -205,11 +220,13 @@ impl TerminalApp {
205220
{
206221
use termion::event::Key::{Char, Ctrl};
207222

223+
self.draw(terminal)?;
208224
for key in keys.filter_map(Result::ok) {
209225
match key {
210226
Ctrl('c') | Char('\n') | Char('q') => break,
211-
_ => dbg!(&key),
227+
_ => {}
212228
};
229+
self.draw(terminal)?;
213230
}
214231
Ok(WalkResult {
215232
num_errors: self.traversal.io_errors,
@@ -226,10 +243,11 @@ impl TerminalApp {
226243
{
227244
let display_options: DisplayOptions = options.clone().into();
228245
Ok(TerminalApp {
246+
display: display_options,
229247
traversal: Traversal::from_walk(options, input, move |traversal| {
230248
terminal.draw(|mut f| {
231249
let full_screen = f.size();
232-
super::widgets::InitWindow {
250+
super::widgets::MainWindow {
233251
traversal,
234252
display: display_options,
235253
}

src/interactive/widgets.rs

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{DisplayOptions, Traversal, Tree, TreeIndex};
2+
use crate::ByteFormat;
23
use tui::layout::{Constraint, Direction, Layout};
34
use tui::style::{Color, Style};
45
use tui::{
@@ -13,56 +14,78 @@ pub struct Entries<'a> {
1314
pub display: DisplayOptions,
1415
}
1516

16-
pub struct InitWindow<'a> {
17+
pub struct MainWindow<'a> {
1718
pub traversal: &'a Traversal,
1819
pub display: DisplayOptions,
1920
}
2021

22+
pub struct Footer {
23+
pub total_bytes: u64,
24+
pub entries_traversed: u64,
25+
pub format: ByteFormat,
26+
}
27+
28+
impl Widget for Footer {
29+
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
30+
assert!(area.height == 1, "The footer must be a line");
31+
let bg_color = Color::White;
32+
let text_color = Color::Black;
33+
let margin = 1;
34+
self.background(area, buf, bg_color);
35+
buf.set_stringn(
36+
area.x + margin,
37+
area.y,
38+
format!(
39+
"Total disk usage: {} Entries: {}",
40+
format!("{}", self.format.display(self.total_bytes)).trim(),
41+
self.entries_traversed
42+
),
43+
(area.width - margin) as usize,
44+
Style {
45+
fg: text_color,
46+
bg: bg_color,
47+
..Default::default()
48+
},
49+
)
50+
}
51+
}
52+
2153
fn get_size_or_panic(tree: &Tree, node_idx: TreeIndex) -> u64 {
2254
tree.node_weight(node_idx)
2355
.expect("node should always be retrievable with valid index")
2456
.size
2557
}
2658

27-
impl<'a> Widget for InitWindow<'a> {
59+
impl<'a> Widget for MainWindow<'a> {
2860
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
61+
let Self {
62+
traversal:
63+
Traversal {
64+
tree,
65+
root_index,
66+
entries_traversed,
67+
..
68+
},
69+
display,
70+
} = *self;
2971
let regions = Layout::default()
3072
.direction(Direction::Vertical)
3173
.constraints([Constraint::Max(256), Constraint::Length(1)].as_ref())
3274
.split(area);
3375
let (entries, footer) = (regions[0], regions[1]);
3476
Entries {
35-
tree: &self.traversal.tree,
36-
root: self.traversal.root_index,
37-
display: self.display,
77+
tree: &tree,
78+
root: *root_index,
79+
display: display,
3880
}
3981
.draw(entries, buf);
4082

41-
let bg_color = Color::White;
42-
let text_color = Color::Black;
43-
let margin = 1;
44-
self.background(footer, buf, bg_color);
45-
buf.set_stringn(
46-
footer.x + margin,
47-
footer.y,
48-
format!(
49-
"Total disk usage: {}",
50-
format!(
51-
"{}",
52-
self.display.byte_format.display(get_size_or_panic(
53-
&self.traversal.tree,
54-
self.traversal.root_index
55-
))
56-
)
57-
.trim()
58-
),
59-
(footer.width - margin) as usize,
60-
Style {
61-
fg: text_color,
62-
bg: bg_color,
63-
..Default::default()
64-
},
65-
)
83+
Footer {
84+
total_bytes: get_size_or_panic(&tree, *root_index),
85+
entries_traversed: *entries_traversed,
86+
format: display.byte_format,
87+
}
88+
.draw(footer, buf);
6689
}
6790
}
6891

0 commit comments

Comments
 (0)