Skip to content

Commit bfffd98

Browse files
bug fixes, perf
1 parent a16f4c1 commit bfffd98

File tree

5 files changed

+46
-21
lines changed

5 files changed

+46
-21
lines changed

.config/config.json5

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"Menu": {
44
"<Ctrl-c>": "Quit",
55
"q": "AbortQuery",
6-
"r": "LoadMenu",
76
"<Alt-1>": "FocusMenu",
87
"<Alt-2>": "FocusEditor",
98
"<Alt-3>": "FocusData",

src/components/editor.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,13 @@ struct Selection {
3636
pub struct Editor<'a> {
3737
command_tx: Option<UnboundedSender<Action>>,
3838
config: Config,
39-
lines: Vec<Vec<char>>,
40-
cursor: CursorPosition,
4139
selection: Option<Selection>,
4240
textarea: TextArea<'a>,
4341
}
4442

4543
impl<'a> Editor<'a> {
4644
pub fn new() -> Self {
47-
Editor {
48-
command_tx: None,
49-
config: Config::default(),
50-
cursor: CursorPosition { row: 0, col: 0 },
51-
selection: None,
52-
lines: vec![vec![]],
53-
textarea: TextArea::default(),
54-
}
45+
Editor { command_tx: None, config: Config::default(), selection: None, textarea: TextArea::default() }
5546
}
5647
}
5748

@@ -86,8 +77,7 @@ impl<'a> Component for Editor<'a> {
8677
fn update(&mut self, action: Action, app_state: &AppState) -> Result<Option<Action>> {
8778
if let Action::MenuSelect(schema, table) = action {
8879
let query = format!("select * from {}.{} limit 100", schema, table);
89-
let chars: Vec<char> = format!("select * from {}.{} limit 100", schema, table).chars().collect();
90-
self.lines = vec![chars];
80+
self.textarea = TextArea::from(vec![query.clone()]);
9181
self.command_tx.as_ref().unwrap().send(Action::Query(query))?;
9282
} else if let Action::SubmitEditorQuery = action {
9383
if let Some(sender) = &self.command_tx {

src/components/menu.rs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,19 @@ impl Menu {
8383
match self.menu_focus {
8484
MenuFocus::Table => {
8585
if let Some(i) = self.list_state.selected() {
86-
let tables = self.table_map.get_index(self.schema_index).unwrap().1;
87-
self.list_state =
88-
ListState::default().with_selected(Some(i.saturating_add(1).clamp(0, tables.len().saturating_sub(1))));
86+
let tables = self.table_map.get_index(self.schema_index).unwrap().1.to_owned();
87+
let filtered_tables: Vec<String> = tables
88+
.into_iter()
89+
.filter(|t| {
90+
if let Some(search) = self.search.as_ref() {
91+
t.to_lowercase().contains(search.to_lowercase().trim())
92+
} else {
93+
true
94+
}
95+
})
96+
.collect();
97+
self.list_state = ListState::default()
98+
.with_selected(Some(i.saturating_add(1).clamp(0, filtered_tables.len().saturating_sub(1))));
8999
}
90100
},
91101
MenuFocus::Schema => {
@@ -109,8 +119,18 @@ impl Menu {
109119
match self.menu_focus {
110120
MenuFocus::Table => {
111121
if let Some(i) = self.list_state.selected() {
112-
let tables = self.table_map.get_index(self.schema_index).unwrap().1;
113-
self.list_state = ListState::default().with_selected(Some(tables.len().saturating_sub(1)));
122+
let tables = self.table_map.get_index(self.schema_index).unwrap().1.to_owned();
123+
let filtered_tables: Vec<String> = tables
124+
.into_iter()
125+
.filter(|t| {
126+
if let Some(search) = self.search.as_ref() {
127+
t.to_lowercase().contains(search.to_lowercase().trim())
128+
} else {
129+
true
130+
}
131+
})
132+
.collect();
133+
self.list_state = ListState::default().with_selected(Some(filtered_tables.len().saturating_sub(1)));
114134
}
115135
},
116136
MenuFocus::Schema => {
@@ -214,6 +234,7 @@ impl Component for Menu {
214234
KeyCode::Char('k') => self.scroll_up(),
215235
KeyCode::Char('g') => self.scroll_top(),
216236
KeyCode::Char('G') => self.scroll_bottom(),
237+
KeyCode::Char('R') => self.command_tx.as_ref().unwrap().send(Action::LoadMenu)?,
217238
_ => {},
218239
}
219240
}
@@ -223,7 +244,22 @@ impl Component for Menu {
223244
self.search_focused = false;
224245
} else if let Some(selected) = self.list_state.selected() {
225246
let (schema, tables) = self.table_map.get_index(self.schema_index).unwrap();
226-
self.command_tx.as_ref().unwrap().send(Action::MenuSelect(schema.clone(), tables[selected].clone()))?;
247+
let filtered_tables: Vec<String> = tables
248+
.iter()
249+
.filter(|t| {
250+
if let Some(search) = self.search.as_ref() {
251+
t.to_lowercase().contains(search.to_lowercase().trim())
252+
} else {
253+
true
254+
}
255+
})
256+
.cloned()
257+
.collect();
258+
self
259+
.command_tx
260+
.as_ref()
261+
.unwrap()
262+
.send(Action::MenuSelect(schema.clone(), filtered_tables[selected].clone()))?;
227263
}
228264
},
229265
KeyCode::Esc => self.reset_search(),

src/components/scroll_table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'a> ScrollTable<'a> {
6161
column_width: u16,
6262
) -> &mut Self {
6363
let requested_width = column_width.saturating_mul(column_count as u16);
64-
let max_height = u16::MAX.saturating_div(requested_width);
64+
let max_height = u16::MAX.saturating_div(std::cmp::max(1, requested_width));
6565
self.table = *table;
6666
self.column_width = column_width;
6767
self.requested_width = requested_width;

src/tui.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub struct Tui {
5858
impl Tui {
5959
pub fn new() -> Result<Self> {
6060
let tick_rate = 4.0;
61-
let frame_rate = 30.0;
61+
let frame_rate = 15.0;
6262
let terminal = ratatui::Terminal::new(Backend::new(io()))?;
6363
let (event_tx, event_rx) = mpsc::unbounded_channel();
6464
let cancellation_token = CancellationToken::new();

0 commit comments

Comments
 (0)