Compare commits

...

11 Commits

Author SHA1 Message Date
filipriec
d1d33b5752 project redesign 2025-03-23 13:50:47 +01:00
filipriec
c6c6c5ed81 restored rendering 2025-03-23 12:59:36 +01:00
filipriec
4ddcb34205 nothing 2025-03-23 12:56:27 +01:00
filipriec
83393a20e2 fix of the error 2025-03-23 12:44:33 +01:00
filipriec
13d501e6d7 not working 2025-03-23 12:30:00 +01:00
filipriec
993febd204 admin panel keyindings 2025-03-23 11:28:39 +01:00
filipriec
49fe2aa793 edit mode is now perfectly working 2025-03-23 10:55:05 +01:00
filipriec
87a572783a i think its a step in the right direction, needs to export other functions now 2025-03-23 10:03:59 +01:00
filipriec
ca8dea53fd VERY SUSPICIOUS BREAKING FUNCTIONALITY CHECK LATER 2025-03-23 00:49:19 +01:00
filipriec
fef2f12c9a :disabled in the edit mode, cant type it tho, needs fix 2025-03-23 00:28:51 +01:00
filipriec
1a529a70bf gamechanging, commands works only on their windows properly well 2025-03-22 23:32:33 +01:00
13 changed files with 300 additions and 130 deletions

View File

@@ -1,6 +1,17 @@
# config.toml
[keybindings]
enter_command_mode = [":", "ctrl+;"]
[keybindings.intro]
next_option = ["j", "Right"]
previous_option = ["k", "Left"]
select = ["Enter"]
[keybindings.admin]
move_up = ["k"]
move_down = ["j"]
[keybindings.common]
save = ["ctrl+s"]
quit = ["ctrl+q"]
@@ -33,7 +44,6 @@ move_line_start = ["0"]
move_line_end = ["$"]
move_first_line = ["gg"]
move_last_line = ["x"]
enter_command_mode = [":", "ctrl+;"]
[keybindings.edit]
exit_edit_mode = ["esc","ctrl+e"]

View File

@@ -1,4 +1,4 @@
// client/src/config/config.rs
// src/config/binds/config.rs
use serde::Deserialize;
use std::collections::HashMap;
@@ -25,6 +25,10 @@ pub struct Config {
#[derive(Debug, Deserialize)]
pub struct ModeKeybindings {
#[serde(default)]
pub intro: HashMap<String, Vec<String>>,
#[serde(default)]
pub admin: HashMap<String, Vec<String>>,
#[serde(default)]
pub read_only: HashMap<String, Vec<String>>,
#[serde(default)]
@@ -33,7 +37,6 @@ pub struct ModeKeybindings {
pub command: HashMap<String, Vec<String>>,
#[serde(default)]
pub common: HashMap<String, Vec<String>>,
// Store top-level keybindings that aren't in a specific mode section
#[serde(flatten)]
pub global: HashMap<String, Vec<String>>,
}
@@ -49,6 +52,20 @@ impl Config {
Ok(config)
}
/// Gets an action for a key in Intro mode, only checking intro and global bindings
pub fn get_intro_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
self.get_action_for_key_in_mode(&self.keybindings.intro, key, modifiers)
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers))
}
/// Gets an action for a key in Admin mode, checking common and global bindings
pub fn get_admin_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
self.get_action_for_key_in_mode(&self.keybindings.admin, key, modifiers)
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.common, key, modifiers))
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.intro, key, modifiers))
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers))
}
/// Gets an action for a key in Read-Only mode, also checking common keybindings.
pub fn get_read_only_action_for_key(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
self.get_action_for_key_in_mode(&self.keybindings.read_only, key, modifiers)
@@ -70,6 +87,25 @@ impl Config {
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers))
}
/// Context-aware keybinding resolution
pub fn get_action_for_current_context(
&self,
is_edit_mode: bool,
command_mode: bool,
show_intro: bool,
show_admin: bool,
key: KeyCode,
modifiers: KeyModifiers
) -> Option<&str> {
match (show_intro, show_admin, command_mode, is_edit_mode) {
(true, _, _, _) => self.get_intro_action(key, modifiers),
(_, true, _, _) => self.get_admin_action(key, modifiers),
(_, _, true, _) => self.get_command_action_for_key(key, modifiers),
(_, _, _, true) => self.get_edit_action_for_key(key, modifiers),
_ => self.get_read_only_action_for_key(key, modifiers),
}
}
/// Helper function to get an action for a key in a specific mode.
pub fn get_action_for_key_in_mode<'a>(
&self,

View File

@@ -0,0 +1,4 @@
// src/client/modes/canvas.rs
pub mod edit;
pub mod common;
pub mod read_only;

View File

@@ -1,4 +1,4 @@
// src/modes/handlers/common.rs
// src/modes/canvas/common.rs
use crate::tui::terminal::grpc_client::GrpcClient;
use crate::ui::handlers::form::FormState;

View File

@@ -1,4 +1,4 @@
// src/modes/handlers/edit.rs
// src/modes/canvas/edit.rs
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
use crate::tui::terminal::{
@@ -6,7 +6,7 @@ use crate::tui::terminal::{
};
use crate::config::binds::config::Config;
use crate::ui::handlers::form::FormState;
use super::common;
use crate::modes::canvas::common;
pub async fn handle_edit_event_internal(
key: KeyEvent,
@@ -19,6 +19,24 @@ pub async fn handle_edit_event_internal(
total_count: u64,
grpc_client: &mut GrpcClient,
) -> Result<String, Box<dyn std::error::Error>> {
if let Some("enter_command_mode") = config.get_action_for_key_in_mode(&config.keybindings.global, key.code, key.modifiers) {
// Ignore in edit mode and process as normal input
handle_edit_specific_input(key, form_state, ideal_cursor_column);
return Ok(command_message.clone());
}
// Check common actions first
if let Some(action) = config.get_action_for_key_in_mode(&config.keybindings.common, key.code, key.modifiers) {
return execute_common_action(
action,
form_state,
grpc_client,
is_saved,
current_position,
total_count,
).await;
}
if let Some(action) = config.get_edit_action_for_key(key.code, key.modifiers) {
return execute_edit_action(
action,
@@ -40,6 +58,48 @@ pub async fn handle_edit_event_internal(
Ok(command_message.clone())
}
async fn execute_common_action(
action: &str,
form_state: &mut FormState,
grpc_client: &mut GrpcClient,
is_saved: &mut bool,
current_position: &mut u64,
total_count: u64,
) -> Result<String, Box<dyn std::error::Error>> {
match action {
"save" => {
common::save(
form_state,
grpc_client,
is_saved,
current_position,
total_count,
).await
},
"revert" => {
common::revert(
form_state,
grpc_client,
current_position,
total_count,
).await
},
"move_up" | "move_down" => {
// Reuse edit mode's existing logic
execute_edit_action(
action,
form_state,
&mut 0, // Dummy ideal_cursor_column (not used here)
grpc_client,
is_saved,
current_position,
total_count,
).await
},
_ => Ok(format!("Common action not handled: {}", action)),
}
}
fn handle_edit_specific_input(
key: KeyEvent,
form_state: &mut FormState,

View File

@@ -1,6 +1,3 @@
// src/client/modes/handlers.rs
pub mod event;
pub mod edit;
pub mod common;
pub mod command_mode;
pub mod read_only;

View File

@@ -4,7 +4,9 @@ use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
use crate::tui::terminal::grpc_client::GrpcClient;
use crate::config::binds::config::Config;
use crate::ui::handlers::form::FormState;
use super::common;
use crate::modes::{
canvas::{common},
};
pub async fn handle_command_event(
key: KeyEvent,

View File

@@ -1,5 +1,5 @@
// src/modes/handlers/event.rs
use crossterm::event::{Event, KeyCode};
use crossterm::event::Event;
use crossterm::cursor::SetCursorStyle;
use crate::tui::terminal::{
core::TerminalCore,
@@ -9,9 +9,11 @@ use crate::tui::terminal::{
use crate::config::binds::config::Config;
use crate::ui::handlers::form::FormState;
use crate::ui::handlers::rat_state::UiStateHandler;
use crate::modes::handlers::{edit, command_mode, read_only};
use crate::modes::{
handlers::{command_mode},
canvas::{edit, read_only, common},
};
use crate::config::binds::key_sequences::KeySequenceTracker;
use super::common;
pub struct EventHandler {
pub command_mode: bool,
@@ -51,130 +53,164 @@ impl EventHandler {
) -> Result<(bool, String), Box<dyn std::error::Error>> {
if app_state.ui.show_intro {
if let Event::Key(key) = event {
match key.code {
KeyCode::Left => intro_state.previous_option(),
KeyCode::Right => intro_state.next_option(),
KeyCode::Enter => {
if intro_state.selected_option == 0 {
app_state.ui.show_intro = false;
} else {
app_state.ui.show_intro = false;
app_state.ui.show_admin = true;
}
return Ok((false, String::new()));
}
_ => {}
let action = config.get_intro_action(key.code, key.modifiers);
match action {
Some("previous_option") => intro_state.previous_option(),
Some("next_option") => intro_state.next_option(),
Some("select") => {
app_state.ui.show_intro = false;
app_state.ui.show_admin = intro_state.selected_option == 1;
},
_ => {} // Ignore all other keys
}
}
return Ok((false, String::new()));
}
if let Event::Key(key) = event {
let key_code = key.code;
let modifiers = key.modifiers;
if let Event::Key(key) = event {
let key_code = key.code;
let modifiers = key.modifiers;
if UiStateHandler::toggle_sidebar(
&mut app_state.ui,
config,
key_code,
modifiers,
) {
return Ok((false, format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
}
if let Some(action) = config.get_action_for_key_in_mode(
&config.keybindings.common,
key_code,
modifiers
) {
match action {
"save" => {
let message = common::save(
form_state,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
return Ok((false, message));
},
"force_quit" => {
let (should_exit, message) = command_handler.handle_command("force_quit", terminal).await?;
return Ok((should_exit, message));
},
"save_and_quit" => {
let (should_exit, message) = command_handler.handle_command("save_and_quit", terminal).await?;
return Ok((should_exit, message));
},
"revert" => {
let message = common::revert(
form_state,
grpc_client,
current_position,
total_count,
).await?;
return Ok((false, message));
},
_ => {}
// Handle admin panel mode
if app_state.ui.show_admin {
if let Some(action) = config.get_admin_action(key_code, modifiers) {
match action {
"move_up" => {
// Handle up movement using app_state directly
app_state.admin_selected_item = app_state.admin_selected_item.saturating_sub(1);
}
"move_down" => {
// Handle down movement using app_state
app_state.admin_selected_item = app_state.admin_selected_item.saturating_add(1);
}
_ => {}
}
return Ok((false, format!("Admin: {}", action)));
}
return Ok((false, String::new()));
}
}
if self.command_mode {
let (should_exit, message, exit_command_mode) = command_mode::handle_command_event(
key,
if UiStateHandler::toggle_sidebar(
&mut app_state.ui,
config,
form_state,
&mut self.command_input,
&mut self.command_message,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
if exit_command_mode {
self.command_mode = false;
key_code,
modifiers,
) {
return Ok((false, format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
}
return Ok((should_exit, message));
}
// Handle edit mode first to allow normal character input
if self.is_edit_mode {
if config.is_exit_edit_mode(key_code, modifiers) {
if form_state.has_unsaved_changes {
self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string();
return Ok((false, self.command_message.clone()));
}
self.is_edit_mode = false;
self.edit_mode_cooldown = true;
self.command_message = "Read-only mode".to_string();
terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
if self.is_edit_mode {
if config.is_exit_edit_mode(key_code, modifiers) {
if form_state.has_unsaved_changes {
self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string();
let current_input = form_state.get_current_input();
if !current_input.is_empty() && form_state.current_cursor_pos >= current_input.len() {
form_state.current_cursor_pos = current_input.len() - 1;
self.ideal_cursor_column = form_state.current_cursor_pos;
}
return Ok((false, self.command_message.clone()));
}
self.is_edit_mode = false;
self.edit_mode_cooldown = true;
self.command_message = "Read-only mode".to_string();
terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
let current_input = form_state.get_current_input();
if !current_input.is_empty() && form_state.current_cursor_pos >= current_input.len() {
form_state.current_cursor_pos = current_input.len() - 1;
self.ideal_cursor_column = form_state.current_cursor_pos;
}
return Ok((false, self.command_message.clone()));
let result = edit::handle_edit_event_internal(
key,
config,
form_state,
&mut self.ideal_cursor_column,
&mut self.command_message,
&mut app_state.ui.is_saved,
current_position,
total_count,
grpc_client,
).await?;
self.key_sequence_tracker.reset();
return Ok((false, result));
}
let result = edit::handle_edit_event_internal(
key,
config,
form_state,
&mut self.ideal_cursor_column,
&mut self.command_message,
&mut app_state.ui.is_saved,
current_position,
total_count,
grpc_client,
).await?;
// Global command mode activation
let context_action = config.get_action_for_current_context(
self.is_edit_mode,
self.command_mode,
app_state.ui.show_intro,
app_state.ui.show_admin,
key_code,
modifiers
);
if let Some("enter_command_mode") = context_action {
self.command_mode = true;
self.command_input.clear();
self.command_message.clear();
return Ok((false, String::new()));
}
if let Some(action) = config.get_action_for_key_in_mode(
&config.keybindings.common,
key_code,
modifiers
) {
match action {
"save" => {
let message = common::save(
form_state,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
return Ok((false, message));
},
"force_quit" => {
let (should_exit, message) = command_handler.handle_command("force_quit", terminal).await?;
return Ok((should_exit, message));
},
"save_and_quit" => {
let (should_exit, message) = command_handler.handle_command("save_and_quit", terminal).await?;
return Ok((should_exit, message));
},
"revert" => {
let message = common::revert(
form_state,
grpc_client,
current_position,
total_count,
).await?;
return Ok((false, message));
},
_ => {}
}
}
if self.command_mode {
let (should_exit, message, exit_command_mode) = command_mode::handle_command_event(
key,
config,
form_state,
&mut self.command_input,
&mut self.command_message,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
if exit_command_mode {
self.command_mode = false;
}
return Ok((should_exit, message));
}
self.key_sequence_tracker.reset();
return Ok((false, result));
} else {
if let Some(action) = config.get_read_only_action_for_key(key_code, modifiers) {
if action == "enter_command_mode" {
self.command_mode = true;
@@ -218,7 +254,6 @@ impl EventHandler {
&mut self.ideal_cursor_column,
).await;
}
}
self.edit_mode_cooldown = false;
Ok((false, self.command_message.clone()))

View File

@@ -1,4 +1,6 @@
// src/client/modes/mod.rs
pub mod handlers;
pub mod canvas;
pub use handlers::*;
pub use canvas::*;

View File

@@ -17,6 +17,8 @@ pub struct AppState {
pub current_position: u64,
pub profile_tree: ProfileTreeResponse,
pub selected_profile: Option<String>,
pub admin_selected_item: usize, // Tracks selection in admin panel
pub admin_profiles: Vec<String>, // Stores admin panel data
// UI preferences
pub ui: UiState,
@@ -33,6 +35,8 @@ impl AppState {
current_position: 0,
profile_tree: ProfileTreeResponse::default(),
selected_profile: None,
admin_selected_item: 0,
admin_profiles: Vec::new(),
ui: UiState::default(),
})
}

View File

@@ -27,7 +27,6 @@ pub fn render_ui(
command_message: &str,
app_state: &AppState,
intro_state: &intro::IntroState,
admin_panel_state: &mut AdminPanelState,
) {
render_background(f, f.area(), theme);
@@ -44,11 +43,30 @@ pub fn render_ui(
if app_state.ui.show_intro {
intro_state.render(f, main_content_area, theme);
} else if app_state.ui.show_admin {
admin_panel_state.render(
// Create temporary AdminPanelState for rendering
let mut admin_state = AdminPanelState::new(
if app_state.admin_profiles.is_empty() {
// Fallback if admin_profiles is empty
app_state.profile_tree.profiles
.iter()
.map(|p| p.name.clone())
.collect()
} else {
app_state.admin_profiles.clone()
}
);
// Set the selected item
if !admin_state.profiles.is_empty() {
let safe_index = app_state.admin_selected_item.min(admin_state.profiles.len() - 1);
admin_state.list_state.select(Some(safe_index));
}
admin_state.render(
f,
main_content_area,
theme,
&app_state.profile_tree,
&app_state.profile_tree,
&app_state.selected_profile,
);
} else {

View File

@@ -28,11 +28,14 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
app_state.profile_tree = profile_tree;
// Now create admin panel with profiles from app_state
let profiles = app_state.profile_tree.profiles
.iter()
.map(|p| p.name.clone())
.collect();
let mut admin_panel_state = AdminPanelState::new(profiles);
if intro_state.selected_option == 1 {
app_state.ui.show_admin = true;
app_state.admin_profiles = app_state.profile_tree.profiles
.iter()
.map(|p| p.name.clone())
.collect();
app_state.admin_selected_item = 0;
}
// Fetch table structure at startup (one-time)
let table_structure = grpc_client.get_table_structure().await?;
@@ -75,7 +78,6 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
&event_handler.command_message,
&app_state,
&intro_state,
&mut admin_panel_state,
);
})?;