summaryrefslogtreecommitdiff
path: root/src/widgets/statusbar.rs
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2024-08-01 20:13:55 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2024-08-01 20:13:55 +0200
commita807d4e6fb96c4d8b8585b7dbb862e53486562ec (patch)
tree92481d73423fd925f4dd034d68cbfd2e30e216d5 /src/widgets/statusbar.rs
parent417cee4eeeaf7516dfeb59cdbe34fed18f30e0f7 (diff)
add evalsto
Diffstat (limited to 'src/widgets/statusbar.rs')
-rw-r--r--src/widgets/statusbar.rs85
1 files changed, 79 insertions, 6 deletions
diff --git a/src/widgets/statusbar.rs b/src/widgets/statusbar.rs
index 97b514d..60b58a6 100644
--- a/src/widgets/statusbar.rs
+++ b/src/widgets/statusbar.rs
@@ -1,4 +1,10 @@
-use ratatui::{layout::{Alignment, Rect}, style::Style, text::ToLine, widgets::Widget};
+use ratatui::{
+ crossterm::event::{KeyCode, KeyEvent},
+ layout::{Alignment, Rect},
+ style::{Style, Stylize},
+ text::{ToLine, ToSpan},
+ widgets::Widget,
+};
#[derive(Clone)]
pub struct StatusBar {
@@ -9,6 +15,8 @@ pub struct StatusBar {
middle_alignment: Alignment,
right: String,
right_style: Style,
+ input: Option<String>,
+ cursor: u16,
}
impl StatusBar {
@@ -21,6 +29,8 @@ impl StatusBar {
middle_alignment: Alignment::Center,
right: String::new(),
right_style: Style::default(),
+ input: None,
+ cursor: 0,
}
}
@@ -105,12 +115,49 @@ impl StatusBar {
self.middle_alignment = alignment;
}
+ pub fn set_input_mode(&mut self, input: bool) {
+ if input {
+ self.input = Some(String::new());
+ self.cursor = 0;
+ } else {
+ self.input = None;
+ }
+ }
+
+ pub fn input(&self) -> Option<&str> {
+ self.input.as_ref().map(|s| s.as_str())
+ }
+
pub fn area(&self, rect: Rect) -> Rect {
let mut inner = rect.clone();
inner.height -= 1;
inner
}
+ pub fn handle_keyevent(&mut self, event: KeyEvent) {
+ if let Some(input) = &mut self.input {
+ match event.code {
+ KeyCode::Char(c) => {
+ input.insert(self.cursor as usize, c);
+ self.cursor += 1;
+ }
+ KeyCode::Backspace => {
+ if self.cursor > 0 {
+ self.cursor -= 1;
+ input.remove(self.cursor as usize).to_string();
+ }
+ }
+ KeyCode::Left => {
+ self.cursor = (self.cursor as i32 - 1).max(0) as u16;
+ }
+ KeyCode::Right => {
+ self.cursor = (self.cursor + 1).min(input.len() as u16);
+ }
+ _ => {}
+ }
+ }
+ }
+
fn layout(&self, mut area: Rect) -> (Rect, Rect, Rect) {
area.y += area.height - 1;
area.height = 1;
@@ -142,11 +189,37 @@ impl Widget for &mut StatusBar {
.left_aligned()
.style(self.left_style)
.render(left, buf);
- self.middle
- .to_line()
- .alignment(self.middle_alignment)
- .style(self.middle_style)
- .render(middle, buf);
+
+ if let Some(input) = &self.input {
+ (":".to_owned() + input)
+ .to_line()
+ .alignment(Alignment::Left)
+ .style(self.middle_style)
+ .render(middle, buf);
+
+ input
+ .chars()
+ .nth(self.cursor as usize)
+ .unwrap_or(' ')
+ .to_span()
+ .reversed()
+ .render(
+ Rect::new(
+ middle.x + 1 + self.cursor,
+ middle.y,
+ middle.width,
+ middle.height,
+ ),
+ buf,
+ );
+ } else {
+ self.middle
+ .to_line()
+ .alignment(self.middle_alignment)
+ .style(self.middle_style)
+ .render(middle, buf);
+ }
+
self.right
.to_line()
.right_aligned()