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

Skip to content

Commit 28f5ac9

Browse files
committed
feat: Automatically resize if the terminal changes in size. (#28)
1 parent e35baea commit 28f5ac9

4 files changed

Lines changed: 25 additions & 25 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/interactive/app/eventloop.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::interactive::{
55
SortMode,
66
};
77
use anyhow::Result;
8-
use crosstermion::input::{key_input_channel, Key};
8+
use crosstermion::input::{input_channel, Event, Key};
99
use dua::{
1010
traverse::{Traversal, TreeIndex},
1111
WalkOptions, WalkResult,
@@ -69,16 +69,20 @@ impl AppState {
6969
traversal: &mut Traversal,
7070
display: &mut DisplayOptions,
7171
terminal: &mut Terminal<B>,
72-
keys: impl Iterator<Item = Key>,
72+
events: impl Iterator<Item = Event>,
7373
) -> Result<ProcessingResult>
7474
where
7575
B: Backend,
7676
{
7777
use crosstermion::input::Key::*;
7878
use FocussedPane::*;
7979

80-
self.draw(window, traversal, *display, terminal)?;
81-
for key in keys {
80+
for event in events {
81+
let key = match event {
82+
Event::Key(key) => key,
83+
Event::Resize(_, _) => Alt('\r'),
84+
};
85+
8286
self.reset_message();
8387
match key {
8488
Char('?') => self.toggle_help_pane(window),
@@ -106,17 +110,15 @@ impl AppState {
106110
}
107111

108112
match self.focussed {
109-
FocussedPane::Mark => {
110-
self.dispatch_to_mark_pane(key, window, traversal, *display, terminal)
111-
}
112-
FocussedPane::Help => {
113+
Mark => self.dispatch_to_mark_pane(key, window, traversal, *display, terminal),
114+
Help => {
113115
window
114116
.help_pane
115117
.as_mut()
116118
.expect("help pane")
117119
.process_events(key);
118120
}
119-
FocussedPane::Main => match key {
121+
Main => match key {
120122
Char('O') => self.open_that(traversal),
121123
Char(' ') => self.mark_entry(
122124
CursorMode::KeepPosition,
@@ -184,7 +186,7 @@ pub struct TerminalApp {
184186
pub window: MainWindow,
185187
}
186188

187-
type KeyboardInputAndApp = (std::sync::mpsc::Receiver<Key>, TerminalApp);
189+
type KeyboardInputAndApp = (std::sync::mpsc::Receiver<Event>, TerminalApp);
188190

189191
impl TerminalApp {
190192
pub fn refresh_view<B>(&mut self, terminal: &mut Terminal<B>)
@@ -198,14 +200,14 @@ impl TerminalApp {
198200
&mut self.traversal,
199201
&mut self.display,
200202
terminal,
201-
std::iter::once(Key::Alt('\r')),
203+
std::iter::once(Event::Key(Key::Alt('\r'))),
202204
)
203205
.ok();
204206
}
205207
pub fn process_events<B>(
206208
&mut self,
207209
terminal: &mut Terminal<B>,
208-
keys: impl Iterator<Item = Key>,
210+
events: impl Iterator<Item = Event>,
209211
) -> Result<WalkResult>
210212
where
211213
B: Backend,
@@ -215,7 +217,7 @@ impl TerminalApp {
215217
&mut self.traversal,
216218
&mut self.display,
217219
terminal,
218-
keys,
220+
events,
219221
)? {
220222
ProcessingResult::Finished(res) | ProcessingResult::ExitRequested(res) => Ok(res),
221223
}
@@ -240,7 +242,7 @@ impl TerminalApp {
240242
let (_, keys_rx) = std::sync::mpsc::channel();
241243
keys_rx
242244
}
243-
Interaction::Full => key_input_channel(),
245+
Interaction::Full => input_channel(),
244246
};
245247

246248
let fetch_buffered_key_events = || {

src/interactive/app/tests/journeys_with_writes.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::interactive::app::tests::utils::{
22
initialized_app_and_terminal_from_paths, into_keys, WritableFixture,
33
};
44
use anyhow::Result;
5+
use crosstermion::input::Event;
6+
use crosstermion::input::Key;
57
use pretty_assertions::assert_eq;
68

79
#[test]
@@ -24,11 +26,7 @@ fn basic_user_journey_with_deletion() -> Result<()> {
2426
// When selecting the marker window and pressing the combination to delete entries
2527
app.process_events(
2628
&mut terminal,
27-
vec![
28-
crosstermion::input::Key::Char('\t'),
29-
crosstermion::input::Key::Ctrl('r'),
30-
]
31-
.into_iter(),
29+
vec![Event::Key(Key::Char('\t')), Event::Key(Key::Ctrl('r'))].into_iter(),
3230
)?;
3331
assert!(
3432
app.window.mark_pane.is_none(),

src/interactive/app/tests/utils.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ use crate::interactive::{app::tests::FIXTURE_PATH, Interaction, TerminalApp};
2121

2222
pub fn into_keys<'a>(
2323
bytes: impl Iterator<Item = &'a u8> + 'a,
24-
) -> impl Iterator<Item = crosstermion::input::Key> + 'a {
25-
bytes.map(|b| crosstermion::input::Key::Char(std::char::from_u32(*b as u32).unwrap()))
24+
) -> impl Iterator<Item = crosstermion::input::Event> + 'a {
25+
bytes.map(|b| {
26+
crosstermion::input::Event::Key(crosstermion::input::Key::Char(
27+
std::char::from_u32(*b as u32).unwrap(),
28+
))
29+
})
2630
}
2731

2832
pub fn node_by_index(app: &TerminalApp, id: TreeIndex) -> &EntryData {

0 commit comments

Comments
 (0)