Skip to content

Commit 1fcb305

Browse files
vimlike keybindings
1 parent 6064e30 commit 1fcb305

File tree

2 files changed

+75
-8
lines changed

2 files changed

+75
-8
lines changed

src/components/data.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<'a> SettableDataTable<'a> for Data<'a> {
7575
let value_rows = rows.iter().map(|r| Row::new(row_to_vec(r)).bottom_margin(1)).collect::<Vec<Row>>();
7676
let buf_table =
7777
Table::default().rows(value_rows).header(header_row).style(Style::default()).column_spacing(1);
78-
self.scrollable.set_table(Box::new(buf_table), 36_u16.saturating_mul(headers.len() as u16), rows.len());
78+
self.scrollable.set_table(Box::new(buf_table), headers.len(), rows.len(), 36_u16);
7979
self.data_state = DataState::HasResults;
8080
}
8181
},
@@ -107,18 +107,36 @@ impl<'a> Component for Data<'a> {
107107
}
108108
if let Some(Event::Key(key)) = event {
109109
match key.code {
110-
KeyCode::Right => {
110+
KeyCode::Right | KeyCode::Char('l') => {
111111
self.scrollable.scroll(ScrollDirection::Right);
112112
},
113-
KeyCode::Left => {
113+
KeyCode::Left | KeyCode::Char('h') => {
114114
self.scrollable.scroll(ScrollDirection::Left);
115115
},
116-
KeyCode::Down => {
116+
KeyCode::Down | KeyCode::Char('j') => {
117117
self.scrollable.scroll(ScrollDirection::Down);
118118
},
119-
KeyCode::Up => {
119+
KeyCode::Up | KeyCode::Char('k') => {
120120
self.scrollable.scroll(ScrollDirection::Up);
121121
},
122+
KeyCode::Char('e') => {
123+
self.scrollable.next_column();
124+
},
125+
KeyCode::Char('b') => {
126+
self.scrollable.prev_column();
127+
},
128+
KeyCode::Char('g') => {
129+
self.scrollable.top_row();
130+
},
131+
KeyCode::Char('G') => {
132+
self.scrollable.bottom_row();
133+
},
134+
KeyCode::Char('0') => {
135+
self.scrollable.first_column();
136+
},
137+
KeyCode::Char('$') => {
138+
self.scrollable.last_column();
139+
},
122140
_ => {},
123141
}
124142
};

src/components/scroll_table.rs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub struct ScrollTable<'a> {
2727
parent_area: Rect,
2828
block: Option<Block<'a>>,
2929
requested_width: u16,
30+
column_width: u16,
3031
max_height: u16,
3132
x_offset: u16,
3233
y_offset: usize,
@@ -42,6 +43,7 @@ impl<'a> ScrollTable<'a> {
4243
parent_area: Rect::new(0, 0, 0, 0),
4344
block: None,
4445
requested_width: 0,
46+
column_width: 0,
4547
max_height: 0,
4648
x_offset: 0,
4749
y_offset: 0,
@@ -50,9 +52,17 @@ impl<'a> ScrollTable<'a> {
5052
}
5153
}
5254

53-
pub fn set_table(&mut self, table: Box<Table<'a>>, requested_width: u16, row_count: usize) -> &mut Self {
55+
pub fn set_table(
56+
&mut self,
57+
table: Box<Table<'a>>,
58+
column_count: usize,
59+
row_count: usize,
60+
column_width: u16,
61+
) -> &mut Self {
62+
let requested_width = column_width.saturating_mul(column_count as u16);
5463
let max_height = u16::MAX.saturating_div(requested_width);
5564
self.table = *table;
65+
self.column_width = column_width;
5666
self.requested_width = requested_width;
5767
self.max_height = max_height;
5868
self.max_y_offset = row_count.saturating_sub(1);
@@ -66,14 +76,53 @@ impl<'a> ScrollTable<'a> {
6676

6777
pub fn scroll(&mut self, direction: ScrollDirection) -> &mut Self {
6878
match direction {
69-
ScrollDirection::Left => self.x_offset = self.x_offset.saturating_sub(1),
70-
ScrollDirection::Right => self.x_offset = Ord::min(self.x_offset.saturating_add(1), self.max_x_offset),
79+
ScrollDirection::Left => self.x_offset = self.x_offset.saturating_sub(2),
80+
ScrollDirection::Right => self.x_offset = Ord::min(self.x_offset.saturating_add(2), self.max_x_offset),
7181
ScrollDirection::Up => self.y_offset = self.y_offset.saturating_sub(1),
7282
ScrollDirection::Down => self.y_offset = Ord::min(self.y_offset.saturating_add(1), self.max_y_offset),
7383
}
7484
self
7585
}
7686

87+
pub fn next_column(&mut self) -> &mut Self {
88+
let x_over = self.x_offset % self.column_width;
89+
self.x_offset = Ord::min(self.x_offset.saturating_add(self.column_width).saturating_sub(x_over), self.max_x_offset);
90+
self
91+
}
92+
93+
pub fn prev_column(&mut self) -> &mut Self {
94+
let x_over = self.x_offset % self.column_width;
95+
match x_over {
96+
0 => {
97+
self.x_offset = self.x_offset.saturating_sub(self.column_width);
98+
},
99+
x => {
100+
self.x_offset = self.x_offset.saturating_sub(x);
101+
},
102+
}
103+
self
104+
}
105+
106+
pub fn bottom_row(&mut self) -> &mut Self {
107+
self.y_offset = self.max_y_offset;
108+
self
109+
}
110+
111+
pub fn top_row(&mut self) -> &mut Self {
112+
self.y_offset = 0;
113+
self
114+
}
115+
116+
pub fn last_column(&mut self) -> &mut Self {
117+
self.x_offset = self.max_x_offset;
118+
self
119+
}
120+
121+
pub fn first_column(&mut self) -> &mut Self {
122+
self.x_offset = 0;
123+
self
124+
}
125+
77126
pub fn reset_scroll(&mut self) -> &mut Self {
78127
self.x_offset = 0;
79128
self.y_offset = 0;

0 commit comments

Comments
 (0)