Skip to content

Commit

Permalink
feat: Automatically resize if the terminal changes in size. (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Sep 12, 2022
1 parent e35baea commit 28f5ac9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
4 changes: 0 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 16 additions & 14 deletions src/interactive/app/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::interactive::{
SortMode,
};
use anyhow::Result;
use crosstermion::input::{key_input_channel, Key};
use crosstermion::input::{input_channel, Event, Key};
use dua::{
traverse::{Traversal, TreeIndex},
WalkOptions, WalkResult,
Expand Down Expand Up @@ -69,16 +69,20 @@ impl AppState {
traversal: &mut Traversal,
display: &mut DisplayOptions,
terminal: &mut Terminal<B>,
keys: impl Iterator<Item = Key>,
events: impl Iterator<Item = Event>,
) -> Result<ProcessingResult>
where
B: Backend,
{
use crosstermion::input::Key::*;
use FocussedPane::*;

self.draw(window, traversal, *display, terminal)?;
for key in keys {
for event in events {
let key = match event {
Event::Key(key) => key,
Event::Resize(_, _) => Alt('\r'),
};

self.reset_message();
match key {
Char('?') => self.toggle_help_pane(window),
Expand Down Expand Up @@ -106,17 +110,15 @@ impl AppState {
}

match self.focussed {
FocussedPane::Mark => {
self.dispatch_to_mark_pane(key, window, traversal, *display, terminal)
}
FocussedPane::Help => {
Mark => self.dispatch_to_mark_pane(key, window, traversal, *display, terminal),
Help => {
window
.help_pane
.as_mut()
.expect("help pane")
.process_events(key);
}
FocussedPane::Main => match key {
Main => match key {
Char('O') => self.open_that(traversal),
Char(' ') => self.mark_entry(
CursorMode::KeepPosition,
Expand Down Expand Up @@ -184,7 +186,7 @@ pub struct TerminalApp {
pub window: MainWindow,
}

type KeyboardInputAndApp = (std::sync::mpsc::Receiver<Key>, TerminalApp);
type KeyboardInputAndApp = (std::sync::mpsc::Receiver<Event>, TerminalApp);

impl TerminalApp {
pub fn refresh_view<B>(&mut self, terminal: &mut Terminal<B>)
Expand All @@ -198,14 +200,14 @@ impl TerminalApp {
&mut self.traversal,
&mut self.display,
terminal,
std::iter::once(Key::Alt('\r')),
std::iter::once(Event::Key(Key::Alt('\r'))),
)
.ok();
}
pub fn process_events<B>(
&mut self,
terminal: &mut Terminal<B>,
keys: impl Iterator<Item = Key>,
events: impl Iterator<Item = Event>,
) -> Result<WalkResult>
where
B: Backend,
Expand All @@ -215,7 +217,7 @@ impl TerminalApp {
&mut self.traversal,
&mut self.display,
terminal,
keys,
events,
)? {
ProcessingResult::Finished(res) | ProcessingResult::ExitRequested(res) => Ok(res),
}
Expand All @@ -240,7 +242,7 @@ impl TerminalApp {
let (_, keys_rx) = std::sync::mpsc::channel();
keys_rx
}
Interaction::Full => key_input_channel(),
Interaction::Full => input_channel(),
};

let fetch_buffered_key_events = || {
Expand Down
8 changes: 3 additions & 5 deletions src/interactive/app/tests/journeys_with_writes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::interactive::app::tests::utils::{
initialized_app_and_terminal_from_paths, into_keys, WritableFixture,
};
use anyhow::Result;
use crosstermion::input::Event;
use crosstermion::input::Key;
use pretty_assertions::assert_eq;

#[test]
Expand All @@ -24,11 +26,7 @@ fn basic_user_journey_with_deletion() -> Result<()> {
// When selecting the marker window and pressing the combination to delete entries
app.process_events(
&mut terminal,
vec![
crosstermion::input::Key::Char('\t'),
crosstermion::input::Key::Ctrl('r'),
]
.into_iter(),
vec![Event::Key(Key::Char('\t')), Event::Key(Key::Ctrl('r'))].into_iter(),
)?;
assert!(
app.window.mark_pane.is_none(),
Expand Down
8 changes: 6 additions & 2 deletions src/interactive/app/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ use crate::interactive::{app::tests::FIXTURE_PATH, Interaction, TerminalApp};

pub fn into_keys<'a>(
bytes: impl Iterator<Item = &'a u8> + 'a,
) -> impl Iterator<Item = crosstermion::input::Key> + 'a {
bytes.map(|b| crosstermion::input::Key::Char(std::char::from_u32(*b as u32).unwrap()))
) -> impl Iterator<Item = crosstermion::input::Event> + 'a {
bytes.map(|b| {
crosstermion::input::Event::Key(crosstermion::input::Key::Char(
std::char::from_u32(*b as u32).unwrap(),
))
})
}

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

0 comments on commit 28f5ac9

Please sign in to comment.