working cursor perfectly well, lets polish it

This commit is contained in:
filipriec
2025-02-19 22:42:09 +01:00
parent bad77af2b3
commit 3d8c68f42f
4 changed files with 35 additions and 1 deletions

View File

@@ -8,3 +8,7 @@ enter_edit_mode = ["i", "ctrl+e"]
exit_edit_mode = ["esc", "ctrl+e"] exit_edit_mode = ["esc", "ctrl+e"]
previous_position = ["Left", "9"] previous_position = ["Left", "9"]
next_position = ["Right", "8"] next_position = ["Right", "8"]
move_left = ["h", "Left"]
move_right = ["l", "Right"]
move_up = ["k", "Up"]
move_down = ["j", "Down"]

View File

@@ -126,5 +126,18 @@ pub fn render_form(
let cursor_y = input_rows[i].y; let cursor_y = input_rows[i].y;
f.set_cursor(cursor_x, cursor_y); f.set_cursor(cursor_x, cursor_y);
} }
if is_active {
if is_edit_mode {
// Edit mode: vertical line at end of input
let cursor_x = input_rows[i].x + input.len() as u16;
let cursor_y = input_rows[i].y;
f.set_cursor(cursor_x, cursor_y);
} else {
// Read-only mode: block at start of field
let cursor_x = input_rows[i].x;
let cursor_y = input_rows[i].y;
f.set_cursor(cursor_x, cursor_y);
}
}
} }
} }

View File

@@ -4,6 +4,7 @@ use crossterm::{
execute, execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
}; };
use crossterm::cursor::{SetCursorStyle, EnableBlinking};
use ratatui::{backend::CrosstermBackend, Terminal}; use ratatui::{backend::CrosstermBackend, Terminal};
use std::io::{self, stdout}; use std::io::{self, stdout};
use tonic::transport::Channel; use tonic::transport::Channel;
@@ -20,10 +21,23 @@ pub struct AppTerminal {
} }
impl AppTerminal { impl AppTerminal {
pub fn set_cursor_style(&mut self, style: SetCursorStyle) -> Result<(), Box<dyn std::error::Error>> {
execute!(
self.terminal.backend_mut(),
style,
EnableBlinking,
)?;
Ok(())
}
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> { pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
enable_raw_mode()?; enable_raw_mode()?;
let mut stdout = stdout(); let mut stdout = stdout();
execute!(stdout, EnterAlternateScreen)?; execute!(
stdout,
EnterAlternateScreen,
SetCursorStyle::SteadyBlock
)?;
let backend = CrosstermBackend::new(stdout); let backend = CrosstermBackend::new(stdout);
let terminal = Terminal::new(backend)?; let terminal = Terminal::new(backend)?;

View File

@@ -1,5 +1,6 @@
// src/client/ui/handlers/event.rs // src/client/ui/handlers/event.rs
use crossterm::event::{Event, KeyCode, KeyModifiers}; use crossterm::event::{Event, KeyCode, KeyModifiers};
use crossterm::cursor::{SetCursorStyle};
use crate::client::terminal::AppTerminal; use crate::client::terminal::AppTerminal;
use crate::client::config::Config; use crate::client::config::Config;
use crate::proto::multieko2::{PostAdresarRequest, PutAdresarRequest}; use crate::proto::multieko2::{PostAdresarRequest, PutAdresarRequest};
@@ -39,6 +40,7 @@ impl EventHandler {
self.is_edit_mode = true; self.is_edit_mode = true;
self.edit_mode_cooldown = true; self.edit_mode_cooldown = true;
self.command_message = "Edit mode".to_string(); self.command_message = "Edit mode".to_string();
app_terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?; // Add this line
return Ok((false, self.command_message.clone())); return Ok((false, self.command_message.clone()));
} else if self.is_edit_mode && config.is_exit_edit_mode(key.code, key.modifiers) { } else if self.is_edit_mode && config.is_exit_edit_mode(key.code, key.modifiers) {
// Prevent exiting edit mode if there are unsaved changes // Prevent exiting edit mode if there are unsaved changes
@@ -49,6 +51,7 @@ impl EventHandler {
self.is_edit_mode = false; self.is_edit_mode = false;
self.edit_mode_cooldown = true; self.edit_mode_cooldown = true;
self.command_message = "Read-only mode".to_string(); self.command_message = "Read-only mode".to_string();
app_terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?; // Add this line
return Ok((false, self.command_message.clone())); return Ok((false, self.command_message.clone()));
} }