Skip to content

Commit

Permalink
page up + down in results section (#85)
Browse files Browse the repository at this point in the history
* wip

* update keybindings

* update readme

* pgup/down

* cargo fmt

* cargo fmt
  • Loading branch information
achristmascarl authored Sep 18, 2024
1 parent 1161088 commit 4ad1edb
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 35 deletions.
16 changes: 8 additions & 8 deletions .config/rainfrog_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"<Alt-2>" = "FocusEditor"
"<Alt-3>" = "FocusHistory"
"<Alt-4>" = "FocusData"
"<Ctrl-n>" = "FocusMenu"
"<Ctrl-b>" = "FocusEditor"
"<Ctrl-k>" = "FocusMenu"
"<Ctrl-j>" = "FocusEditor"
"<Ctrl-h>" = "FocusHistory"
"<Ctrl-g>" = "FocusData"
"<Tab>" = "CycleFocusForwards"
Expand All @@ -19,8 +19,8 @@
"<Alt-2>" = "FocusEditor"
"<Alt-3>" = "FocusHistory"
"<Alt-4>" = "FocusData"
"<Ctrl-n>" = "FocusMenu"
"<Ctrl-b>" = "FocusEditor"
"<Ctrl-k>" = "FocusMenu"
"<Ctrl-j>" = "FocusEditor"
"<Ctrl-h>" = "FocusHistory"
"<Ctrl-g>" = "FocusData"
"<Backtab>" = "CycleFocusBackwards"
Expand All @@ -32,8 +32,8 @@
"<Alt-2>" = "FocusEditor"
"<Alt-3>" = "FocusHistory"
"<Alt-4>" = "FocusData"
"<Ctrl-n>" = "FocusMenu"
"<Ctrl-b>" = "FocusEditor"
"<Ctrl-k>" = "FocusMenu"
"<Ctrl-j>" = "FocusEditor"
"<Ctrl-h>" = "FocusHistory"
"<Ctrl-g>" = "FocusData"
"<Tab>" = "CycleFocusForwards"
Expand All @@ -46,8 +46,8 @@
"<Alt-2>" = "FocusEditor"
"<Alt-3>" = "FocusHistory"
"<Alt-4>" = "FocusData"
"<Ctrl-n>" = "FocusMenu"
"<Ctrl-b>" = "FocusEditor"
"<Ctrl-k>" = "FocusMenu"
"<Ctrl-j>" = "FocusEditor"
"<Ctrl-h>" = "FocusHistory"
"<Ctrl-g>" = "FocusData"
"<Tab>" = "CycleFocusForwards"
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ rainfrog --url $(connection_url)
| keybinding | description |
|-----------------------------|----------------------------------------|
| `Ctrl+c` | quit program |
| `Alt+1`, `Ctrl+n` | change focus to menu |
| `Alt+2`, `Ctrl+b` | change focus to query editor |
| `Alt+1`, `Ctrl+k` | change focus to menu |
| `Alt+2`, `Ctrl+j` | change focus to query editor |
| `Alt+3`, `Ctrl+h` | change focus to query history |
| `Alt+4`, `Ctrl+g` | change focus to results |
| `Tab` | cycle focus forwards |
Expand Down Expand Up @@ -143,6 +143,8 @@ keybindings may not behave exactly like vim. the full list of active vim keybind
| `l`, `` | scroll right by 1 cell |
| `b` | scroll right by 1 cell |
| `e`, `w` | scroll left by 1 column |
| `{`, `PageUp`, `Ctrl+b` | jump up one page |
| `}`, `PageDown`, `Ctrl+f` | jump down one page |
| `g` | jump to top of table |
| `G` | jump to bottom of table |
| `0` | jump to first column |
Expand Down
2 changes: 1 addition & 1 deletion demo.tape
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Type "e"
Sleep 1s
Type "y"
Sleep 2s
Ctrl+B
Ctrl+J
Sleep 1s
Type "dd"
Sleep 1s
Expand Down
46 changes: 29 additions & 17 deletions src/components/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use ratatui::{prelude::*, symbols::scrollbar, widgets::*};
use serde::{Deserialize, Serialize};
use sqlparser::ast::Statement;
use tokio::sync::{mpsc::UnboundedSender, Mutex};
use tui_textarea::{Input, Key};

use super::{scroll_table::SelectionMode, Frame};
use crate::{
Expand Down Expand Up @@ -271,44 +272,55 @@ impl<'a> Component for Data<'a> {
if app_state.focus != Focus::Data {
return Ok(None);
}
match key.code {
KeyCode::Right | KeyCode::Char('l') => {
let input = Input::from(key);
match input {
Input { key: Key::Right, .. } | Input { key: Key::Char('l'), .. } => {
self.scroll(ScrollDirection::Right);
},
KeyCode::Left | KeyCode::Char('h') => {
Input { key: Key::Left, .. } | Input { key: Key::Char('h'), .. } => {
self.scroll(ScrollDirection::Left);
},
KeyCode::Down | KeyCode::Char('j') => {
Input { key: Key::Down, .. } | Input { key: Key::Char('j'), .. } => {
self.scroll(ScrollDirection::Down);
},
KeyCode::Up | KeyCode::Char('k') => {
Input { key: Key::Up, .. } | Input { key: Key::Char('k'), .. } => {
self.scroll(ScrollDirection::Up);
},
KeyCode::Char('e') | KeyCode::Char('w') => {
Input { key: Key::Char('e'), .. } | Input { key: Key::Char('w'), .. } => {
self.scrollable.next_column();
},
KeyCode::Char('b') => {
Input { key: Key::Char('b'), ctrl: false, .. } => {
self.scrollable.prev_column();
},
KeyCode::Char('g') => {
Input { key: Key::Char('g'), .. } => {
self.top();
},
KeyCode::Char('G') => {
Input { key: Key::Char('G'), .. } => {
self.bottom();
},
KeyCode::Char('0') => {
Input { key: Key::Char('0'), .. } => {
self.left();
},
KeyCode::Char('$') => {
Input { key: Key::Char('$'), .. } => {
self.right();
},
KeyCode::Char('v') => {
Input { key: Key::Char('{'), .. }
| Input { key: Key::Char('b'), ctrl: true, .. }
| Input { key: Key::PageUp, .. } => {
self.scrollable.pg_up();
},
Input { key: Key::Char('}'), .. }
| Input { key: Key::Char('f'), ctrl: true, .. }
| Input { key: Key::PageDown, .. } => {
self.scrollable.pg_down();
},
Input { key: Key::Char('v'), .. } => {
self.scrollable.transition_selection_mode(Some(SelectionMode::Cell));
},
KeyCode::Char('V') => {
Input { key: Key::Char('V'), .. } => {
self.scrollable.transition_selection_mode(Some(SelectionMode::Row));
},
KeyCode::Enter => {
Input { key: Key::Enter, .. } => {
match self.scrollable.get_selection_mode() {
Some(SelectionMode::Row) => {
self.scrollable.transition_selection_mode(Some(SelectionMode::Cell));
Expand All @@ -319,7 +331,7 @@ impl<'a> Component for Data<'a> {
_ => {},
};
},
KeyCode::Backspace => {
Input { key: Key::Backspace, .. } => {
match self.scrollable.get_selection_mode() {
Some(SelectionMode::Row) => {
self.scrollable.transition_selection_mode(None);
Expand All @@ -330,7 +342,7 @@ impl<'a> Component for Data<'a> {
_ => {},
};
},
KeyCode::Char('y') => {
Input { key: Key::Char('y'), .. } => {
if let DataState::HasResults(Rows { rows, .. }) = &self.data_state {
let (x, y) = self.scrollable.get_cell_offsets();
let row = &rows[y];
Expand All @@ -352,7 +364,7 @@ impl<'a> Component for Data<'a> {
self.scrollable.transition_selection_mode(Some(SelectionMode::Copied));
}
},
KeyCode::Esc => {
Input { key: Key::Esc, .. } => {
self.scrollable.transition_selection_mode(None);
},
_ => {},
Expand Down
36 changes: 31 additions & 5 deletions src/components/scroll_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct ScrollTable<'a> {
table: Table<'a>,
parent_area: Rect,
block: Option<Block<'a>>,
pg_height: u16,
requested_width: u16,
column_width: u16,
max_height: u16,
Expand All @@ -49,6 +50,7 @@ impl<'a> ScrollTable<'a> {
table: Table::default(),
parent_area: Rect::new(0, 0, 0, 0),
block: None,
pg_height: 0,
requested_width: 0,
column_width: 0,
max_height: 0,
Expand Down Expand Up @@ -79,9 +81,9 @@ impl<'a> ScrollTable<'a> {
pub fn scroll(&mut self, direction: ScrollDirection) -> &mut Self {
match direction {
ScrollDirection::Left => self.x_offset = self.x_offset.saturating_sub(2),
ScrollDirection::Right => self.x_offset = Ord::min(self.x_offset.saturating_add(2), self.max_x_offset),
ScrollDirection::Right => self.x_offset = std::cmp::min(self.x_offset.saturating_add(2), self.max_x_offset),
ScrollDirection::Up => self.y_offset = self.y_offset.saturating_sub(1),
ScrollDirection::Down => self.y_offset = Ord::min(self.y_offset.saturating_add(1), self.max_y_offset),
ScrollDirection::Down => self.y_offset = std::cmp::min(self.y_offset.saturating_add(1), self.max_y_offset),
}
self
}
Expand All @@ -91,7 +93,8 @@ impl<'a> ScrollTable<'a> {
return self;
}
let x_over = self.x_offset % self.column_width;
self.x_offset = Ord::min(self.x_offset.saturating_add(self.column_width).saturating_sub(x_over), self.max_x_offset);
self.x_offset =
std::cmp::min(self.x_offset.saturating_add(self.column_width).saturating_sub(x_over), self.max_x_offset);
self
}

Expand All @@ -111,6 +114,27 @@ impl<'a> ScrollTable<'a> {
self
}

pub fn pg_up(&mut self) -> &mut Self {
self.y_offset = self.y_offset.saturating_sub(std::cmp::max(
1,
self.pg_height.saturating_div(2).saturating_sub(
u16::from(self.pg_height % 2 == 0), // always round down
) as usize,
));
self
}

pub fn pg_down(&mut self) -> &mut Self {
let new_y_offset = self.y_offset.saturating_add(std::cmp::max(
1,
self.pg_height.saturating_div(2).saturating_sub(
u16::from(self.pg_height % 2 == 0), // always rounds down
) as usize,
));
self.y_offset = std::cmp::min(self.max_y_offset, new_y_offset);
self
}

pub fn bottom_row(&mut self) -> &mut Self {
self.y_offset = self.max_y_offset;
self
Expand Down Expand Up @@ -169,6 +193,8 @@ impl<'a> ScrollTable<'a> {
impl<'a> Component for ScrollTable<'a> {
fn draw(&mut self, f: &mut Frame<'_>, area: Rect, app_state: &AppState) -> Result<()> {
self.parent_area = area;
let render_area = self.block.inner_if_some(area);
self.pg_height = std::cmp::min(self.max_height, render_area.height).saturating_sub(3);
self.max_x_offset = self.get_max_x_offset(&self.parent_area, &self.block);
let max_x_offset = self.max_x_offset;
let x_offset = self.x_offset;
Expand Down Expand Up @@ -249,8 +275,8 @@ impl<'a> Widget for Renderer<'a> {
ratatui::widgets::StatefulWidgetRef::render_ref(table, content_buf.area, &mut content_buf, &mut table_state);
let content_width = content_buf.area.width;
let content_height = content_buf.area.height;
let max_x = Ord::min(area.x.saturating_add(area.width), area.x.saturating_add(content_width));
let max_y = Ord::min(area.y.saturating_add(area.height), area.y.saturating_add(content_height));
let max_x = std::cmp::min(area.x.saturating_add(area.width), area.x.saturating_add(content_width));
let max_y = std::cmp::min(area.y.saturating_add(area.height), area.y.saturating_add(content_height));
for y in area.y..max_y {
let content_y = y - area.y;
let row = get_row(&content_buf.content, content_y, content_width);
Expand Down
8 changes: 6 additions & 2 deletions src/vim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,12 @@ impl Vim {
Input { key: Key::Char('y'), ctrl: true, .. } => textarea.scroll((-1, 0)),
Input { key: Key::Char('d'), ctrl: true, .. } => textarea.scroll(Scrolling::HalfPageDown),
Input { key: Key::Char('u'), ctrl: true, .. } => textarea.scroll(Scrolling::HalfPageUp),
Input { key: Key::Char('f'), ctrl: true, .. } => textarea.scroll(Scrolling::PageDown),
Input { key: Key::Char('b'), ctrl: true, .. } => textarea.scroll(Scrolling::PageUp),
Input { key: Key::Char('f'), ctrl: true, .. } | Input { key: Key::PageDown, .. } => {
textarea.scroll(Scrolling::PageDown)
},
Input { key: Key::Char('b'), ctrl: true, .. } | Input { key: Key::PageUp, .. } => {
textarea.scroll(Scrolling::PageUp)
},
Input { key: Key::Char('v'), ctrl: false, .. } if self.mode == Mode::Normal => {
textarea.start_selection();
return Transition::Mode(Mode::Visual);
Expand Down

0 comments on commit 4ad1edb

Please sign in to comment.