Skip to content

Commit

Permalink
feat: handling quitting on the components not on top level
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed May 10, 2024
1 parent 7a8eb8b commit 00706d1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
11 changes: 9 additions & 2 deletions config/src/default_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub static DEFAULT_CONFIG: &str = r##"
"0" = "MoveToLineStart"
"C-d" = "PageDown"
"C-u" = "PageUp"
"d" = { "w" = "DeleteWord", "d" = "DeleteLine", "b" = "DeleteBack", "j" = "DeleteCurrAndBelow", "k" = "DeleteCurrAndAbove", "l" = "DeleteCurrentChar", "h" = "DeletePreviousChar" }
"S-D" = "DeleteUntilEOL"
"x" = "DeleteCurrentChar"
"o" = "InsertLineBelow"
Expand All @@ -31,12 +30,20 @@ pub static DEFAULT_CONFIG: &str = r##"
#"/" = { EnterMode = "Search" }
"i" = { EnterMode = "Insert" }
"S-I" = ["MoveToLineStart", { EnterMode = "Insert" }]
"S-A" = "InsertAtEOL"
"S-B" = "MoveAfterWhitespaceReverse"
"S-W" = "MoveAfterWhitespace"
"S-X" = "DeletePreviousNonWrapping"
[editor_keys.normal.d]
"w" = "DeleteWord"
"d" = "DeleteLine"
"b" = "DeleteBack"
"j" = "DeleteCurrAndBelow"
"k" = "DeleteCurrAndAbove"
"l" = "DeleteCurrentChar"
"h" = "DeletePreviousChar"
[editor_keys.insert]
"Tab" = "InsertTab"
"Enter" = "InsertLine"
Expand Down
11 changes: 11 additions & 0 deletions tui/src/components/api_explorer/api_explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ impl<'ae> ApiExplorer<'ae> {

#[tracing::instrument(skip_all, err)]
fn handle_sidebar_key_event(&mut self, key_event: KeyEvent) -> anyhow::Result<Option<Command>> {
if let (KeyCode::Char('c'), KeyModifiers::CONTROL) = (key_event.code, key_event.modifiers) {
return Ok(Some(Command::Quit));
};

match key_event.code {
KeyCode::Enter => {
if let Some(ref req) = self.hovered_request {
Expand Down Expand Up @@ -188,6 +192,9 @@ impl<'ae> ApiExplorer<'ae> {
}

fn handle_req_uri_key_event(&mut self, key_event: KeyEvent) -> anyhow::Result<Option<Command>> {
if let (KeyCode::Char('c'), KeyModifiers::CONTROL) = (key_event.code, key_event.modifiers) {
return Ok(Some(Command::Quit));
};
match key_event.code {
KeyCode::Char('i') => self.selected_pane = Some(PaneFocus::Preview),
KeyCode::Enter => {
Expand Down Expand Up @@ -292,6 +299,10 @@ impl<'ae> ApiExplorer<'ae> {
}

fn handle_preview_key_event(&mut self, key_event: KeyEvent) -> anyhow::Result<Option<Command>> {
if let (KeyCode::Char('c'), KeyModifiers::CONTROL) = (key_event.code, key_event.modifiers) {
return Ok(Some(Command::Quit));
};

match key_event.code {
KeyCode::Enter => self.selected_pane = Some(PaneFocus::Preview),
KeyCode::Tab => self.preview_tab = ResViewerTabs::next(&self.preview_tab),
Expand Down
9 changes: 8 additions & 1 deletion tui/src/components/api_explorer/req_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ratatui::{
Frame,
};
use reqtui::{
command::Command,
schema::types::Request,
syntax::highlighter::HIGHLIGHTER,
text_object::{cursor::Cursor, TextObject, Write},
Expand Down Expand Up @@ -460,8 +461,8 @@ impl<'re> ReqEditor<'re> {
fn move_down(&mut self) {
let len_lines = self.body.len_lines();
if self.cursor.row().lt(&len_lines.saturating_sub(1)) {
self.maybe_scroll_view();
self.cursor.move_down(1);
self.maybe_scroll_view();
}
let current_line_len = self.body.line_len(self.cursor.row());
self.cursor.maybe_snap_to_col(current_line_len);
Expand Down Expand Up @@ -573,6 +574,12 @@ impl Eventful for ReqEditor<'_> {
return Ok(None);
}

if let (KeyCode::Char('c'), KeyModifiers::CONTROL, EditorMode::Normal) =
(key_event.code, key_event.modifiers, &self.editor_mode)
{
return Ok(Some(Command::Quit));
};

match self.editor_mode {
EditorMode::Normal => match self.config.editor_keys.normal.get(&key_str) {
Some(KeyAction::Simple(action)) => self.handle_action(action),
Expand Down
4 changes: 4 additions & 0 deletions tui/src/components/dashboard/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,10 @@ impl Component for Dashboard<'_> {

impl Eventful for Dashboard<'_> {
fn handle_key_event(&mut self, key_event: KeyEvent) -> anyhow::Result<Option<Command>> {
if let (KeyCode::Char('c'), KeyModifiers::CONTROL) = (key_event.code, key_event.modifiers) {
return Ok(Some(Command::Quit));
};

match self.pane_focus {
PaneFocus::List => self.handle_list_key_event(key_event),
PaneFocus::Form => self.handle_form_key_event(key_event),
Expand Down
17 changes: 3 additions & 14 deletions tui/src/screen_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{
use reqtui::{command::Command, schema::Schema};

use anyhow::Context;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::{layout::Rect, Frame};
use tokio::sync::mpsc::UnboundedSender;

Expand Down Expand Up @@ -126,20 +125,11 @@ impl Component for ScreenManager<'_> {

impl Eventful for ScreenManager<'_> {
fn handle_event(&mut self, event: Option<Event>) -> anyhow::Result<Option<Command>> {
if let Some(Event::Key(KeyEvent {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::CONTROL,
..
})) = event
{
return Ok(Some(Command::Quit));
};

match self.curr_screen {
Screens::Editor => self
.api_explorer
.as_mut()
.context("should never be able to switch to editor screen without having a schema")?
.expect("should never be able to switch to editor screen without having a schema")
.handle_event(event),
Screens::Dashboard => self.dashboard.handle_event(event),
Screens::TerminalTooSmall => Ok(None),
Expand All @@ -150,10 +140,9 @@ impl Eventful for ScreenManager<'_> {
#[cfg(test)]
mod tests {
use super::*;
use reqtui::schema::types::*;

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::{backend::TestBackend, Terminal};
use reqtui::schema;
use reqtui::schema::{self, types::*};
use std::{
fs::{create_dir, File},
io::Write,
Expand Down

0 comments on commit 00706d1

Please sign in to comment.