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

Skip to content

Commit 05c8ec1

Browse files
author
Sebastian Thiel
committed
The first test for user input, yeah!
1 parent 11147d8 commit 05c8ec1

4 files changed

Lines changed: 66 additions & 8 deletions

File tree

src/interactive/app.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct TerminalApp {
2626
}
2727

2828
impl TerminalApp {
29-
fn draw_to_terminal<B>(&self, terminal: &mut Terminal<B>) -> Result<(), Error>
29+
fn draw<B>(&self, terminal: &mut Terminal<B>) -> Result<(), Error>
3030
where
3131
B: Backend,
3232
{
@@ -35,6 +35,7 @@ impl TerminalApp {
3535
display,
3636
state,
3737
} = self;
38+
3839
terminal.draw(|mut f| {
3940
let full_screen = f.size();
4041
MainWindow {
@@ -44,6 +45,7 @@ impl TerminalApp {
4445
}
4546
.render(&mut f, full_screen)
4647
})?;
48+
4749
Ok(())
4850
}
4951
pub fn process_events<B, R>(
@@ -57,13 +59,14 @@ impl TerminalApp {
5759
{
5860
use termion::event::Key::{Char, Ctrl};
5961

60-
self.draw_to_terminal(terminal)?;
62+
self.draw(terminal)?;
6163
for key in keys.filter_map(Result::ok) {
6264
match key {
65+
Char('s') => self.state.sorting.toggle_size(),
6366
Ctrl('c') | Char('\n') | Char('q') => break,
6467
_ => {}
6568
};
66-
self.draw_to_terminal(terminal)?;
69+
self.draw(terminal)?;
6770
}
6871
Ok(WalkResult {
6972
num_errors: self.traversal.io_errors,
@@ -85,6 +88,7 @@ impl TerminalApp {
8588
let state = DisplayState {
8689
root: traversal.root_index,
8790
selected: None,
91+
sorting: Default::default(),
8892
};
8993
MainWindow {
9094
traversal,
@@ -99,6 +103,7 @@ impl TerminalApp {
99103
state: DisplayState {
100104
root: traversal.root_index,
101105
selected: None,
106+
sorting: Default::default(),
102107
},
103108
display: display_options,
104109
traversal: traversal,

src/interactive/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod app;
22
mod traverse;
3-
mod widgets;
3+
pub mod widgets;
44

55
pub use self::app::*;
66
pub use self::traverse::*;

src/interactive/widgets.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,35 @@ pub struct Entries<'a> {
1313
pub tree: &'a Tree,
1414
pub root: TreeIndex,
1515
pub display: DisplayOptions,
16+
pub sorting: SortMode,
17+
}
18+
19+
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq)]
20+
pub enum SortMode {
21+
SizeDescending,
22+
SizeAscending,
23+
}
24+
25+
impl SortMode {
26+
pub fn toggle_size(&mut self) {
27+
use SortMode::*;
28+
*self = match self {
29+
SizeAscending => SizeDescending,
30+
SizeDescending => SizeAscending,
31+
}
32+
}
33+
}
34+
35+
impl Default for SortMode {
36+
fn default() -> Self {
37+
SortMode::SizeDescending
38+
}
1639
}
1740

1841
pub struct DisplayState {
1942
pub root: TreeIndex,
2043
pub selected: Option<TreeIndex>,
44+
pub sorting: SortMode,
2145
}
2246

2347
pub struct MainWindow<'a, 'b> {
@@ -81,6 +105,7 @@ impl<'a, 'b> Widget for MainWindow<'a, 'b> {
81105
tree: &tree,
82106
root: state.root,
83107
display: *display,
108+
sorting: state.sorting,
84109
}
85110
.draw(entries, buf);
86111

@@ -95,16 +120,21 @@ impl<'a, 'b> Widget for MainWindow<'a, 'b> {
95120

96121
impl<'a> Widget for Entries<'a> {
97122
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
98-
use petgraph::Direction;
99123
let Self {
100124
tree,
101125
root,
102126
display,
127+
sorting,
103128
} = self;
129+
use petgraph::Direction;
130+
use SortMode::*;
104131
List::new(
105132
tree.neighbors_directed(*root, Direction::Outgoing)
106133
.filter_map(|w| tree.node_weight(w))
107-
.sorted_by(|l, r| l.size.cmp(&r.size))
134+
.sorted_by(|l, r| match sorting {
135+
SizeDescending => l.size.cmp(&r.size),
136+
SizeAscending => r.size.cmp(&l.size),
137+
})
108138
.rev()
109139
.map(|w| {
110140
Text::Raw(

tests/interactive.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
mod app {
2-
use dua::interactive::{EntryData, TerminalApp, Tree, TreeIndexType};
3-
use dua::{ByteFormat, Color, TraversalSorting, WalkOptions};
2+
use dua::{
3+
interactive::{widgets::SortMode, EntryData, TerminalApp, Tree, TreeIndexType},
4+
ByteFormat, Color, TraversalSorting, WalkOptions,
5+
};
46
use failure::Error;
57
use petgraph::prelude::NodeIndex;
68
use pretty_assertions::assert_eq;
79
use std::{ffi::OsString, fmt, path::Path};
10+
use termion::input::TermRead;
811
use tui::backend::TestBackend;
912
use tui::Terminal;
1013

@@ -38,6 +41,26 @@ mod app {
3841
Ok(())
3942
}
4043

44+
#[test]
45+
fn simple_user_journey() -> Result<(), Error> {
46+
let (mut terminal, mut app) = initialized_app_and_terminal("sample-02")?;
47+
assert_eq!(
48+
app.state.sorting,
49+
SortMode::SizeDescending,
50+
"it starts in descending order by size"
51+
);
52+
53+
// when hitting the S key
54+
app.process_events(&mut terminal, b"s".keys())?;
55+
assert_eq!(
56+
app.state.sorting,
57+
SortMode::SizeAscending,
58+
"it sets the sort to size ascending"
59+
);
60+
61+
Ok(())
62+
}
63+
4164
fn initialized_app_and_terminal(
4265
fixture_path: &str,
4366
) -> Result<(Terminal<TestBackend>, TerminalApp), Error> {

0 commit comments

Comments
 (0)