Skip to content

Commit

Permalink
feat/tui: draw cursor for search bar
Browse files Browse the repository at this point in the history
The cursor position is not correct, which need to be fixed in upstream dependency: joshka/tui-prompts#46
  • Loading branch information
kxxt committed May 11, 2024
1 parent e568d9d commit c09f890
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ bitflags = "2.5.0"
regex = "1.10.4"
indexmap = "2.2.6"
tui-prompts = "0.3.11"
# tui-prompts = { version = "0.3.11", path = "../../contrib/tui-prompts" }
# tui-popup = { version = "0.3.0", path = "../../contrib/tui-popup" }

[dev-dependencies]
Expand Down
18 changes: 16 additions & 2 deletions src/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,17 @@ impl App {
return Ok(());
}
Action::Render => {
tui.draw(|f| self.render(f.size(), f.buffer_mut()))?;
tui.draw(|f| {
self.render(f.size(), f.buffer_mut());
self
.query_builder
.as_ref()
.filter(|q| q.editing())
.inspect(|q| {
let (x, y) = q.cursor();
f.set_cursor(x, y);
});
})?;
}
Action::NextItem => {
self.event_list.next();
Expand Down Expand Up @@ -475,9 +485,13 @@ impl App {
}
Action::BeginSearch => {
if let Some(query_builder) = self.query_builder.as_mut() {
// action_tx.send(query_builder.edit())?;
query_builder.edit();
} else {
self.query_builder = Some(QueryBuilder::new(QueryKind::Search));
let mut query_builder = QueryBuilder::new(QueryKind::Search);
// action_tx.send(query_builder.edit())?;
query_builder.edit();
self.query_builder = Some(query_builder);
}
}
Action::EndSearch => {
Expand Down
7 changes: 7 additions & 0 deletions src/tui/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ impl QueryBuilder {

pub fn edit(&mut self) {
self.editing = true;
self.state.focus();
}

/// Get the current cursor position,
/// this should be called after render is called
pub fn cursor(&self) -> (u16, u16) {
self.state.cursor()
}

pub fn handle_key_events(&mut self, key: KeyEvent) -> Result<Option<Action>, String> {
Expand Down

0 comments on commit c09f890

Please sign in to comment.