Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add key bindings in TUI mode #206

Merged
merged 3 commits into from
Mar 1, 2024
Merged

Add key bindings in TUI mode #206

merged 3 commits into from
Mar 1, 2024

Conversation

9999years
Copy link
Member

Adds a bunch of Vim/less-like key bindings I wanted when I was testing the original TUI PR. These will probably be removed as soon as we do anything interesting with the UI, but they're nice for now.

@9999years 9999years requested a review from evanrelf March 1, 2024 00:53
@github-actions github-actions bot added the patch Bug fixes or non-functional changes label Mar 1, 2024
evanrelf
evanrelf previously approved these changes Mar 1, 2024
@@ -90,7 +92,7 @@ pub async fn run_tui(
.ok_or_else(|| miette!("No more crossterm events"))?
.into_diagnostic()
.wrap_err("Failed to get next crossterm event")?;
handle_event(&mut tui, event)?;
handle_event(&mut tui, event, terminal.get_frame())?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember seeing in flamegraphs for my text editor that get_frame and equivalents were expensive to call.

It would be more efficient if you deferred calling get_frame until you absolutely need it (in this case, only for frame-dependent movements like Ctrl-u and Ctrl-d).

You could achieve this by putting the TerminalGuard inside the Tui, which you already have an exclusive reference to in handle_event.

In practice, I don't think we need to worry about performance issues like this at the moment, but if it's easy to do (my mental borrow checker isn't very good) it's worth considering anyways.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually have another change to implement some of this! I'll merge this as-is and then fix it up.

@@ -90,7 +92,7 @@ pub async fn run_tui(
.ok_or_else(|| miette!("No more crossterm events"))?
.into_diagnostic()
.wrap_err("Failed to get next crossterm event")?;
handle_event(&mut tui, event)?;
handle_event(&mut tui, event, terminal.get_frame())?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should pass the Rect representing the size of the Frame, rather than the entire Frame (which has explicit lifetimes, access to the terminal Buffer, etc.).

Suggested change
handle_event(&mut tui, event, terminal.get_frame())?;
handle_event(&mut tui, event, terminal.get_frame().size())?;

Comment on lines +141 to +165
Event::Key(key) => match key.modifiers {
KeyModifiers::NONE => match key.code {
KeyCode::Char('j') => {
tui.scroll_offset += 1;
}
KeyCode::Char('k') => {
tui.scroll_offset = tui.scroll_offset.saturating_sub(1);
}
KeyCode::Char('g') => {
tui.scroll_offset = 0;
}
_ => {}
},

#[allow(clippy::single_match)]
KeyModifiers::SHIFT => match key.code {
KeyCode::Char('G') => {
tui.scroll_offset = last_line;
}
_ => {}
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might be interested in the declarative macros I wrote for my text editor to match on crossterm's Keys more concisely:

  • Usage example
      match event {
          Event::Key(key) if k!(key, Left) || k!(key, CONTROL, 'b') => {
              command_mode.move_backward(1);
          }
          Event::Key(key) if k!(key, Right) || k!(key, CONTROL, 'f') => {
              command_mode.move_forward(1);
          }
          Event::Key(key) if k!(key, Left) || k!(key, ALT, 'b') => {
              command_mode.move_backward_word();
          }
      // ...
  • Implementation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm I'll add a TODO for this

@9999years 9999years merged commit 6729413 into main Mar 1, 2024
28 checks passed
@9999years 9999years deleted the rebeccat/tui-key-bindings branch March 1, 2024 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch Bug fixes or non-functional changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants