complete redesign
This commit is contained in:
28
client/Cargo.toml
Normal file
28
client/Cargo.toml
Normal file
@@ -0,0 +1,28 @@
|
||||
[package]
|
||||
name = "client"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
common = { path = "../common" }
|
||||
|
||||
chrono = { version = "0.4.39", features = ["serde"] }
|
||||
clap = { version = "4.5.29", features = ["derive"] }
|
||||
console = "0.15.10"
|
||||
crossterm = "0.28.1"
|
||||
dialoguer = "0.11.0"
|
||||
dirs = "6.0.0"
|
||||
dotenvy = "0.15.7"
|
||||
prost = "0.13.5"
|
||||
ratatui = "0.29.0"
|
||||
serde = { version = "1.0.217", features = ["derive"] }
|
||||
serde_json = "1.0.138"
|
||||
serde_with = "3.12.0"
|
||||
sqlx = { version = "0.8.3", features = ["chrono", "postgres", "runtime-tokio", "runtime-tokio-native-tls", "time"] }
|
||||
tokio = { version = "1.43.0", features = ["full", "macros"] }
|
||||
toml = "0.8.20"
|
||||
tonic = "0.12.3"
|
||||
tonic-build = "0.12.3"
|
||||
tonic-reflection = "0.12.3"
|
||||
tracing = "0.1.41"
|
||||
tracing-subscriber = "0.3.19"
|
||||
19
client/config.toml
Normal file
19
client/config.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
# config.toml
|
||||
[keybindings]
|
||||
save = [":w", "ctrl+s"]
|
||||
quit = [":q", "ctrl+q"]
|
||||
force_quit = [":q!", "ctrl+shift+q"]
|
||||
save_and_quit = [":wq", "ctrl+shift+s"]
|
||||
enter_edit_mode = ["i", "ctrl+e"]
|
||||
exit_edit_mode = ["esc", "ctrl+e"]
|
||||
previous_position = ["Left", "9"]
|
||||
next_position = ["Right", "8"]
|
||||
|
||||
move_left = ["h"]
|
||||
move_right = ["l"]
|
||||
move_up = ["k", "Up"]
|
||||
move_down = ["j", "Down"]
|
||||
|
||||
[colors]
|
||||
theme = "dark"
|
||||
# Options: "light", "dark", "high_contrast"
|
||||
68
client/src/colors.rs
Normal file
68
client/src/colors.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
// src/client/colors.rs
|
||||
use ratatui::style::Color;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Theme {
|
||||
pub bg: Color,
|
||||
pub fg: Color,
|
||||
pub accent: Color,
|
||||
pub secondary: Color,
|
||||
pub highlight: Color,
|
||||
pub warning: Color,
|
||||
pub border: Color,
|
||||
}
|
||||
|
||||
impl Theme {
|
||||
pub fn from_str(theme_name: &str) -> Self {
|
||||
match theme_name.to_lowercase().as_str() {
|
||||
"dark" => Self::dark(),
|
||||
"high_contrast" => Self::high_contrast(),
|
||||
_ => Self::light(),
|
||||
}
|
||||
}
|
||||
|
||||
// Default light theme
|
||||
pub fn light() -> Self {
|
||||
Self {
|
||||
bg: Color::Rgb(245, 245, 245), // Light gray
|
||||
fg: Color::Rgb(64, 64, 64), // Dark gray
|
||||
accent: Color::Rgb(173, 216, 230), // Pastel blue
|
||||
secondary: Color::Rgb(255, 165, 0), // Orange for secondary
|
||||
highlight: Color::Rgb(152, 251, 152), // Pastel green
|
||||
warning: Color::Rgb(255, 182, 193), // Pastel pink
|
||||
border: Color::Rgb(220, 220, 220), // Light gray border
|
||||
}
|
||||
}
|
||||
|
||||
// High-contrast dark theme
|
||||
pub fn dark() -> Self {
|
||||
Self {
|
||||
bg: Color::Rgb(30, 30, 30), // Dark background
|
||||
fg: Color::Rgb(255, 255, 255), // White text
|
||||
accent: Color::Rgb(0, 191, 255), // Bright blue
|
||||
secondary: Color::Rgb(255, 215, 0), // Gold for secondary
|
||||
highlight: Color::Rgb(50, 205, 50), // Bright green
|
||||
warning: Color::Rgb(255, 99, 71), // Bright red
|
||||
border: Color::Rgb(100, 100, 100), // Medium gray border
|
||||
}
|
||||
}
|
||||
|
||||
// High-contrast light theme
|
||||
pub fn high_contrast() -> Self {
|
||||
Self {
|
||||
bg: Color::Rgb(255, 255, 255), // White background
|
||||
fg: Color::Rgb(0, 0, 0), // Black text
|
||||
accent: Color::Rgb(0, 0, 255), // Blue
|
||||
secondary: Color::Rgb(255, 140, 0), // Dark orange for secondary
|
||||
highlight: Color::Rgb(0, 128, 0), // Green
|
||||
warning: Color::Rgb(255, 0, 0), // Red
|
||||
border: Color::Rgb(0, 0, 0), // Black border
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Theme {
|
||||
fn default() -> Self {
|
||||
Self::light() // Default to light theme
|
||||
}
|
||||
}
|
||||
10
client/src/components1/handlers.rs
Normal file
10
client/src/components1/handlers.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
// src/client/components/mod.rs
|
||||
pub mod form;
|
||||
pub mod preview_card;
|
||||
pub mod command_line;
|
||||
pub mod status_line;
|
||||
|
||||
pub use command_line::render_command_line;
|
||||
pub use form::render_form;
|
||||
pub use preview_card::render_preview_card;
|
||||
pub use status_line::render_status_line;
|
||||
35
client/src/components1/handlers/command_line.rs
Normal file
35
client/src/components1/handlers/command_line.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
// src/client/components/command_line.rs
|
||||
use ratatui::{
|
||||
widgets::{Block, Paragraph},
|
||||
style::Style,
|
||||
layout::Rect,
|
||||
Frame,
|
||||
};
|
||||
use crate::client::colors::Theme;
|
||||
|
||||
pub fn render_command_line(f: &mut Frame, area: Rect, input: &str, active: bool, theme: &Theme, message: &str) {
|
||||
let prompt = if active {
|
||||
":"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
// Combine the prompt, input, and message
|
||||
let display_text = if message.is_empty() {
|
||||
format!("{}{}", prompt, input)
|
||||
} else {
|
||||
format!("{}{} | {}", prompt, input, message)
|
||||
};
|
||||
|
||||
let style = if active {
|
||||
Style::default().fg(theme.accent)
|
||||
} else {
|
||||
Style::default().fg(theme.fg)
|
||||
};
|
||||
|
||||
let paragraph = Paragraph::new(display_text)
|
||||
.block(Block::default().style(Style::default().bg(theme.bg)))
|
||||
.style(style);
|
||||
|
||||
f.render_widget(paragraph, area);
|
||||
}
|
||||
142
client/src/components1/handlers/form.rs
Normal file
142
client/src/components1/handlers/form.rs
Normal file
@@ -0,0 +1,142 @@
|
||||
// src/client/components1/handlers/form.rs
|
||||
use ratatui::{
|
||||
widgets::{Paragraph, Block, Borders},
|
||||
layout::{Layout, Constraint, Direction, Rect, Margin, Alignment},
|
||||
style::Style,
|
||||
text::{Line, Span},
|
||||
Frame,
|
||||
};
|
||||
use crate::client::colors::Theme;
|
||||
use crate::client::ui::form::FormState;
|
||||
|
||||
pub fn render_form(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
form_state: &FormState,
|
||||
fields: &[&str],
|
||||
current_field: &usize,
|
||||
inputs: &[&String],
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
total_count: u64,
|
||||
current_position: u64,
|
||||
) {
|
||||
// Create Adresar card
|
||||
let adresar_card = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(Style::default().fg(theme.border))
|
||||
.title(" Adresar ")
|
||||
.style(Style::default().bg(theme.bg).fg(theme.fg));
|
||||
|
||||
f.render_widget(adresar_card, area);
|
||||
|
||||
// Define the inner area for the form (inside the card)
|
||||
let inner_area = area.inner(Margin {
|
||||
horizontal: 1,
|
||||
vertical: 1,
|
||||
});
|
||||
|
||||
// Create a vertical layout for the entire form content
|
||||
let main_layout = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Length(1), // For count and position
|
||||
Constraint::Min(1), // For form fields
|
||||
])
|
||||
.split(inner_area);
|
||||
|
||||
// Render the count and position at the very top
|
||||
let count_position_text = format!("Total: {} | Current Position: {}", total_count, current_position);
|
||||
let count_position_paragraph = Paragraph::new(count_position_text)
|
||||
.style(Style::default().fg(theme.fg))
|
||||
.alignment(Alignment::Left);
|
||||
f.render_widget(count_position_paragraph, main_layout[0]);
|
||||
|
||||
// Split the remaining space into label and input columns
|
||||
let columns = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
|
||||
.split(main_layout[1]);
|
||||
|
||||
// Create compact input container
|
||||
let input_container = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(if is_edit_mode {
|
||||
if form_state.has_unsaved_changes {
|
||||
Style::default().fg(theme.warning) // Red color
|
||||
} else {
|
||||
Style::default().fg(theme.accent) // Blue color
|
||||
}
|
||||
} else {
|
||||
Style::default().fg(theme.secondary) // Yellow color
|
||||
})
|
||||
.style(Style::default().bg(theme.bg));
|
||||
|
||||
// Place the input container at the top
|
||||
let input_container_area = Rect {
|
||||
x: columns[1].x,
|
||||
y: columns[1].y,
|
||||
width: columns[1].width,
|
||||
height: fields.len() as u16 + 2, // +2 for borders
|
||||
};
|
||||
|
||||
f.render_widget(&input_container, input_container_area);
|
||||
|
||||
// Input area inside borders
|
||||
let input_area = input_container.inner(input_container_area);
|
||||
|
||||
// Split the remaining area for the form inputs
|
||||
let input_rows = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints(vec![Constraint::Length(1); fields.len()])
|
||||
.split(input_area);
|
||||
|
||||
// Render labels close to the border
|
||||
for (i, field) in fields.iter().enumerate() {
|
||||
let label = Paragraph::new(Line::from(Span::styled(
|
||||
format!("{}:", field),
|
||||
Style::default().fg(theme.fg),
|
||||
)));
|
||||
f.render_widget(label, Rect {
|
||||
x: columns[0].x,
|
||||
y: input_container_area.y + 1 + i as u16, // Align with input rows
|
||||
width: columns[0].width,
|
||||
height: 1,
|
||||
});
|
||||
}
|
||||
|
||||
// Render inputs with left-aligned text and free cursor movement
|
||||
for (i, input) in inputs.iter().enumerate() {
|
||||
let is_active = i == *current_field;
|
||||
|
||||
let input_display = Paragraph::new(input.as_str())
|
||||
.alignment(Alignment::Left)
|
||||
.style(if is_active {
|
||||
Style::default().fg(theme.highlight)
|
||||
} else {
|
||||
Style::default().fg(theme.fg)
|
||||
});
|
||||
|
||||
f.render_widget(input_display, input_rows[i]);
|
||||
|
||||
// Position cursor at the correct position in the active field
|
||||
if is_active && is_edit_mode { // Move cursor logic inside the loop
|
||||
let cursor_x = input_rows[i].x + input.len() as u16;
|
||||
let cursor_y = input_rows[i].y;
|
||||
f.set_cursor_position((cursor_x, cursor_y)); // Updated to set_cursor_position
|
||||
}
|
||||
if is_active {
|
||||
if is_edit_mode {
|
||||
// Edit mode: cursor at current_cursor_pos instead of end
|
||||
let cursor_x = input_rows[i].x + form_state.current_cursor_pos as u16;
|
||||
let cursor_y = input_rows[i].y;
|
||||
f.set_cursor_position((cursor_x, cursor_y)); // Updated to set_cursor_position
|
||||
} else {
|
||||
// Read-only mode: cursor at current_cursor_pos
|
||||
let cursor_x = input_rows[i].x + form_state.current_cursor_pos as u16;
|
||||
let cursor_y = input_rows[i].y;
|
||||
f.set_cursor_position((cursor_x, cursor_y)); // Updated to set_cursor_position
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
33
client/src/components1/handlers/preview_card.rs
Normal file
33
client/src/components1/handlers/preview_card.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
// src/client/components/preview_card.rs
|
||||
use ratatui::{
|
||||
widgets::{Block, Borders, List, ListItem},
|
||||
layout::Rect,
|
||||
style::Style,
|
||||
text::Text,
|
||||
Frame,
|
||||
};
|
||||
use crate::client::colors::Theme;
|
||||
|
||||
pub fn render_preview_card(f: &mut Frame, area: Rect, fields: &[&String], theme: &Theme) {
|
||||
let card = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(Style::default().fg(theme.border))
|
||||
.title(" Preview Card ")
|
||||
.style(Style::default().bg(theme.bg).fg(theme.fg));
|
||||
|
||||
let items = vec![
|
||||
ListItem::new(Text::from(format!("Firma: {}", fields[0]))),
|
||||
ListItem::new(Text::from(format!("Ulica: {}", fields[1]))),
|
||||
ListItem::new(Text::from(format!("Mesto: {}", fields[2]))),
|
||||
ListItem::new(Text::from(format!("PSC: {}", fields[3]))),
|
||||
ListItem::new(Text::from(format!("ICO: {}", fields[4]))),
|
||||
ListItem::new(Text::from(format!("Kontakt: {}", fields[5]))),
|
||||
ListItem::new(Text::from(format!("Telefon: {}", fields[6]))),
|
||||
];
|
||||
|
||||
let list = List::new(items)
|
||||
.block(card)
|
||||
.style(Style::default().bg(theme.bg).fg(theme.fg));
|
||||
|
||||
f.render_widget(list, area);
|
||||
}
|
||||
80
client/src/components1/handlers/status_line.rs
Normal file
80
client/src/components1/handlers/status_line.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
// src/client/components1/handlers/status_line.rs
|
||||
use ratatui::{
|
||||
widgets::Paragraph,
|
||||
style::Style,
|
||||
layout::Rect,
|
||||
Frame,
|
||||
text::{Line, Span},
|
||||
};
|
||||
use crate::client::colors::Theme;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn render_status_line(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
current_dir: &str,
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
) {
|
||||
// Program name and version
|
||||
let program_info = format!("multieko2 v{}", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
let mode_text = if is_edit_mode {
|
||||
"[EDIT]"
|
||||
} else {
|
||||
"[READ-ONLY]"
|
||||
};
|
||||
|
||||
// Shorten the current directory path
|
||||
let home_dir = dirs::home_dir().map(|p| p.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
let display_dir = if current_dir.starts_with(&home_dir) {
|
||||
current_dir.replacen(&home_dir, "~", 1)
|
||||
} else {
|
||||
current_dir.to_string()
|
||||
};
|
||||
|
||||
// Create the full status line text
|
||||
let full_text = format!("{} | {} | {}", mode_text, display_dir, program_info);
|
||||
|
||||
// Check if the full text fits in the available width
|
||||
let available_width = area.width as usize;
|
||||
let mut display_text = if full_text.len() <= available_width {
|
||||
// If it fits, use the full text
|
||||
full_text
|
||||
} else {
|
||||
// If it doesn't fit, prioritize mode and program info, and show only the directory name
|
||||
let dir_name = Path::new(current_dir)
|
||||
.file_name()
|
||||
.and_then(|n| n.to_str())
|
||||
.unwrap_or(current_dir);
|
||||
format!("{} | {} | {}", mode_text, dir_name, program_info)
|
||||
};
|
||||
|
||||
// If even the shortened version overflows, truncate it
|
||||
if display_text.len() > available_width {
|
||||
display_text = display_text.chars().take(available_width).collect();
|
||||
}
|
||||
|
||||
// Create the status line text using Line and Span
|
||||
let status_line = Line::from(vec![
|
||||
Span::styled(mode_text, Style::default().fg(theme.accent)),
|
||||
Span::styled(" | ", Style::default().fg(theme.border)),
|
||||
Span::styled(
|
||||
display_text.split(" | ").nth(1).unwrap_or(""), // Directory part
|
||||
Style::default().fg(theme.fg),
|
||||
),
|
||||
Span::styled(" | ", Style::default().fg(theme.border)),
|
||||
Span::styled(
|
||||
program_info,
|
||||
Style::default()
|
||||
.fg(theme.secondary)
|
||||
.add_modifier(ratatui::style::Modifier::BOLD),
|
||||
),
|
||||
]);
|
||||
|
||||
// Render the status line
|
||||
let paragraph = Paragraph::new(status_line)
|
||||
.style(Style::default().bg(theme.bg));
|
||||
|
||||
f.render_widget(paragraph, area);
|
||||
}
|
||||
5
client/src/components1/mod.rs
Normal file
5
client/src/components1/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
// src/client/components1/mod.rs
|
||||
pub mod models;
|
||||
pub mod handlers;
|
||||
|
||||
pub use handlers::*;
|
||||
0
client/src/components1/models.rs
Normal file
0
client/src/components1/models.rs
Normal file
93
client/src/config.rs
Normal file
93
client/src/config.rs
Normal file
@@ -0,0 +1,93 @@
|
||||
// src/client/config.rs
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
use crossterm::event::{KeyCode, KeyModifiers};
|
||||
|
||||
#[derive(Debug, Deserialize, Default)]
|
||||
pub struct ColorsConfig {
|
||||
#[serde(default = "default_theme")]
|
||||
pub theme: String,
|
||||
}
|
||||
|
||||
fn default_theme() -> String {
|
||||
"light".to_string()
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
pub keybindings: HashMap<String, Vec<String>>,
|
||||
#[serde(default)]
|
||||
pub colors: ColorsConfig,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load() -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let config_str = std::fs::read_to_string("config.toml")?;
|
||||
let config: Config = toml::from_str(&config_str)?;
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
pub fn get_action_for_key(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
|
||||
for (action, bindings) in &self.keybindings {
|
||||
// Skip mode toggle actions
|
||||
if action == "enter_edit_mode" || action == "exit_edit_mode" {
|
||||
continue;
|
||||
}
|
||||
for binding in bindings {
|
||||
if Self::matches_keybinding(binding, key, modifiers) {
|
||||
return Some(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn matches_keybinding(binding: &str, key: KeyCode, modifiers: KeyModifiers) -> bool {
|
||||
let parts: Vec<&str> = binding.split('+').collect();
|
||||
let mut expected_modifiers = KeyModifiers::empty();
|
||||
let mut expected_key = None;
|
||||
|
||||
for part in parts {
|
||||
match part.to_lowercase().as_str() {
|
||||
"ctrl" => expected_modifiers |= KeyModifiers::CONTROL,
|
||||
"shift" => expected_modifiers |= KeyModifiers::SHIFT,
|
||||
"alt" => expected_modifiers |= KeyModifiers::ALT,
|
||||
"esc" => expected_key = Some(KeyCode::Esc),
|
||||
":" => expected_key = Some(KeyCode::Char(':')),
|
||||
part if part.len() == 1 => {
|
||||
let c = part.chars().next().unwrap();
|
||||
expected_key = Some(KeyCode::Char(c));
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
modifiers == expected_modifiers && Some(key) == expected_key
|
||||
}
|
||||
|
||||
pub fn get_action_for_command(&self, command: &str) -> Option<&str> {
|
||||
for (action, bindings) in &self.keybindings {
|
||||
for binding in bindings {
|
||||
if binding.starts_with(':') && binding.trim_start_matches(':') == command {
|
||||
return Some(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn is_enter_edit_mode(&self, key: KeyCode, modifiers: KeyModifiers) -> bool {
|
||||
if let Some(bindings) = self.keybindings.get("enter_edit_mode") {
|
||||
bindings.iter().any(|b| Self::matches_keybinding(b, key, modifiers))
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
pub fn is_exit_edit_mode(&self, key: KeyCode, modifiers: KeyModifiers) -> bool {
|
||||
if let Some(bindings) = self.keybindings.get("exit_edit_mode") {
|
||||
bindings.iter().any(|b| Self::matches_keybinding(b, key, modifiers))
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
3
client/src/main.rs
Normal file
3
client/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Client is running!");
|
||||
}
|
||||
9
client/src/mod.rs
Normal file
9
client/src/mod.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
// src/client/mod.rs
|
||||
mod ui;
|
||||
mod colors;
|
||||
mod components1;
|
||||
mod terminal;
|
||||
mod config;
|
||||
|
||||
pub use config::Config;
|
||||
pub use ui::run_ui;
|
||||
143
client/src/terminal.rs
Normal file
143
client/src/terminal.rs
Normal file
@@ -0,0 +1,143 @@
|
||||
// src/client/terminal.rs
|
||||
use crossterm::event::{self, Event};
|
||||
use crossterm::{
|
||||
execute,
|
||||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
||||
};
|
||||
use crossterm::cursor::{SetCursorStyle, EnableBlinking};
|
||||
use ratatui::{backend::CrosstermBackend, Terminal};
|
||||
use std::io::{self, stdout};
|
||||
use tonic::transport::Channel;
|
||||
|
||||
// Import the correct clients and proto messages from their respective modules
|
||||
use common::proto::multieko2::adresar::adresar_client::AdresarClient;
|
||||
use common::proto::multieko2::adresar::{AdresarResponse, PostAdresarRequest, PutAdresarRequest};
|
||||
use common::proto::multieko2::common::{CountResponse, PositionRequest, Empty};
|
||||
use common::proto::multieko2::table_structure::table_structure_service_client::TableStructureServiceClient;
|
||||
use common::proto::multieko2::table_structure::TableStructureResponse;
|
||||
|
||||
pub struct AppTerminal {
|
||||
terminal: Terminal<CrosstermBackend<io::Stdout>>,
|
||||
adresar_client: AdresarClient<Channel>,
|
||||
table_structure_client: TableStructureServiceClient<Channel>,
|
||||
}
|
||||
|
||||
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>> {
|
||||
enable_raw_mode()?;
|
||||
let mut stdout = stdout();
|
||||
execute!(
|
||||
stdout,
|
||||
EnterAlternateScreen,
|
||||
SetCursorStyle::SteadyBlock
|
||||
)?;
|
||||
let backend = CrosstermBackend::new(stdout);
|
||||
let terminal = Terminal::new(backend)?;
|
||||
|
||||
// Initialize both gRPC clients
|
||||
let adresar_client = AdresarClient::connect("http://[::1]:50051").await?;
|
||||
let table_structure_client = TableStructureServiceClient::connect("http://[::1]:50051").await?;
|
||||
|
||||
Ok(Self { terminal, adresar_client, table_structure_client })
|
||||
}
|
||||
|
||||
pub fn draw<F>(&mut self, f: F) -> Result<(), Box<dyn std::error::Error>>
|
||||
where
|
||||
F: FnOnce(&mut ratatui::Frame),
|
||||
{
|
||||
self.terminal.draw(f)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn read_event(&self) -> Result<Event, Box<dyn std::error::Error>> {
|
||||
Ok(event::read()?)
|
||||
}
|
||||
|
||||
pub fn cleanup(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
disable_raw_mode()?;
|
||||
execute!(self.terminal.backend_mut(), LeaveAlternateScreen)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn handle_command(
|
||||
&mut self,
|
||||
action: &str,
|
||||
is_saved: &mut bool,
|
||||
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"quit" => {
|
||||
if *is_saved {
|
||||
self.cleanup()?;
|
||||
Ok((true, "Exiting.".to_string()))
|
||||
} else {
|
||||
Ok((false, "No changes saved. Use :q! to force quit.".to_string()))
|
||||
}
|
||||
}
|
||||
"force_quit" => {
|
||||
self.cleanup()?;
|
||||
Ok((true, "Force exiting without saving.".to_string()))
|
||||
}
|
||||
"save_and_quit" => {
|
||||
*is_saved = true;
|
||||
self.cleanup()?;
|
||||
Ok((true, "State saved. Exiting.".to_string()))
|
||||
}
|
||||
_ => Ok((false, format!("Action not recognized: {}", action))),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Adresar service methods use adresar_client
|
||||
pub async fn get_adresar_count(&mut self) -> Result<u64, Box<dyn std::error::Error>> {
|
||||
let request = tonic::Request::new(Empty::default());
|
||||
let response: CountResponse = self.adresar_client.get_adresar_count(request).await?.into_inner();
|
||||
Ok(response.count as u64)
|
||||
}
|
||||
|
||||
pub async fn get_adresar_by_position(&mut self, position: u64) -> Result<AdresarResponse, Box<dyn std::error::Error>> {
|
||||
let request = tonic::Request::new(PositionRequest { position: position as i64 });
|
||||
let response: AdresarResponse = self.adresar_client.get_adresar_by_position(request).await?.into_inner();
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub async fn post_adresar(
|
||||
&mut self,
|
||||
request: PostAdresarRequest,
|
||||
) -> Result<tonic::Response<AdresarResponse>, Box<dyn std::error::Error>> {
|
||||
let request = tonic::Request::new(request);
|
||||
let response = self.adresar_client.post_adresar(request).await?;
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub async fn put_adresar(
|
||||
&mut self,
|
||||
request: PutAdresarRequest,
|
||||
) -> Result<tonic::Response<AdresarResponse>, Box<dyn std::error::Error>> {
|
||||
let request = tonic::Request::new(request);
|
||||
let response = self.adresar_client.put_adresar(request).await?;
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
// Table structure method uses table_structure_client
|
||||
pub async fn get_table_structure(
|
||||
&mut self,
|
||||
) -> Result<TableStructureResponse, Box<dyn std::error::Error>> {
|
||||
let request = tonic::Request::new(Empty::default());
|
||||
let response = self.table_structure_client
|
||||
.get_adresar_table_structure(request)
|
||||
.await?;
|
||||
Ok(response.into_inner())
|
||||
}
|
||||
}
|
||||
9
client/src/ui/handlers.rs
Normal file
9
client/src/ui/handlers.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
// src/client/ui/handlers.rs
|
||||
|
||||
pub mod ui;
|
||||
pub mod form;
|
||||
pub mod event;
|
||||
pub mod render;
|
||||
pub mod state;
|
||||
|
||||
pub use ui::run_ui;
|
||||
422
client/src/ui/handlers/event.rs
Normal file
422
client/src/ui/handlers/event.rs
Normal file
@@ -0,0 +1,422 @@
|
||||
// src/client/ui/handlers/event.rs
|
||||
|
||||
use crossterm::event::{Event, KeyCode, KeyModifiers};
|
||||
use crossterm::cursor::{SetCursorStyle};
|
||||
use crate::client::terminal::AppTerminal;
|
||||
use crate::client::config::Config;
|
||||
use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest};
|
||||
use super::form::FormState;
|
||||
|
||||
pub struct EventHandler {
|
||||
pub command_mode: bool,
|
||||
pub command_input: String,
|
||||
pub command_message: String,
|
||||
pub is_edit_mode: bool,
|
||||
pub edit_mode_cooldown: bool,
|
||||
pub ideal_cursor_column: usize,
|
||||
}
|
||||
|
||||
impl EventHandler {
|
||||
pub fn new() -> Self {
|
||||
EventHandler {
|
||||
command_mode: false,
|
||||
command_input: String::new(),
|
||||
command_message: String::new(),
|
||||
is_edit_mode: false,
|
||||
edit_mode_cooldown: false,
|
||||
ideal_cursor_column: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn handle_event(
|
||||
&mut self,
|
||||
event: Event,
|
||||
config: &Config,
|
||||
app_terminal: &mut AppTerminal,
|
||||
form_state: &mut FormState,
|
||||
is_saved: &mut bool,
|
||||
total_count: u64,
|
||||
current_position: &mut u64,
|
||||
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||
if let Event::Key(key) = event {
|
||||
if !self.is_edit_mode && config.is_enter_edit_mode(key.code, key.modifiers) {
|
||||
self.is_edit_mode = true;
|
||||
self.edit_mode_cooldown = true;
|
||||
self.command_message = "Edit mode".to_string();
|
||||
app_terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
|
||||
return Ok((false, self.command_message.clone()));
|
||||
} else if self.is_edit_mode && config.is_exit_edit_mode(key.code, key.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();
|
||||
app_terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
|
||||
return Ok((false, self.command_message.clone()));
|
||||
}
|
||||
|
||||
if !self.is_edit_mode {
|
||||
// Handle navigation between entries
|
||||
if key.code == KeyCode::Left {
|
||||
let new_position = current_position.saturating_sub(1);
|
||||
if new_position >= 1 {
|
||||
*current_position = new_position;
|
||||
match app_terminal.get_adresar_by_position(*current_position).await {
|
||||
Ok(response) => {
|
||||
// Update the ID field - this is what was missing
|
||||
form_state.id = response.id;
|
||||
|
||||
// Update all form fields dynamically
|
||||
form_state.values = vec![
|
||||
response.firma,
|
||||
response.kz,
|
||||
response.drc,
|
||||
response.ulica,
|
||||
response.psc,
|
||||
response.mesto,
|
||||
response.stat,
|
||||
response.banka,
|
||||
response.ucet,
|
||||
response.skladm,
|
||||
response.ico,
|
||||
response.kontakt,
|
||||
response.telefon,
|
||||
response.skladu,
|
||||
response.fax,
|
||||
];
|
||||
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
form_state.has_unsaved_changes = false;
|
||||
self.command_message = format!("Loaded entry {}", *current_position);
|
||||
}
|
||||
Err(e) => {
|
||||
self.command_message = format!("Error loading entry: {}", e);
|
||||
}
|
||||
}
|
||||
return Ok((false, self.command_message.clone()));
|
||||
}
|
||||
} else if key.code == KeyCode::Right {
|
||||
if *current_position <= total_count {
|
||||
*current_position += 1;
|
||||
|
||||
if *current_position <= total_count {
|
||||
match app_terminal.get_adresar_by_position(*current_position).await {
|
||||
Ok(response) => {
|
||||
|
||||
// Update the ID field - this was missing
|
||||
form_state.id = response.id;
|
||||
|
||||
// Update all form fields dynamically
|
||||
form_state.values = vec![
|
||||
response.firma,
|
||||
response.kz,
|
||||
response.drc,
|
||||
response.ulica,
|
||||
response.psc,
|
||||
response.mesto,
|
||||
response.stat,
|
||||
response.banka,
|
||||
response.ucet,
|
||||
response.skladm,
|
||||
response.ico,
|
||||
response.kontakt,
|
||||
response.telefon,
|
||||
response.skladu,
|
||||
response.fax,
|
||||
];
|
||||
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
form_state.has_unsaved_changes = false;
|
||||
self.command_message = format!("Loaded entry {}", *current_position);
|
||||
}
|
||||
Err(e) => {
|
||||
self.command_message = format!("Error loading entry: {}", e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Clear form when entering new entry position
|
||||
form_state.reset_to_empty();
|
||||
form_state.current_field = 0;
|
||||
form_state.current_cursor_pos = 0;
|
||||
self.command_message = "New entry mode".to_string();
|
||||
}
|
||||
return Ok((false, self.command_message.clone()));
|
||||
}
|
||||
} else {
|
||||
// Handle movement keybindings
|
||||
if let Some(action) = config.get_action_for_key(key.code, key.modifiers) {
|
||||
match action {
|
||||
"move_left" => {
|
||||
form_state.current_cursor_pos = form_state.current_cursor_pos.saturating_sub(1);
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos;
|
||||
return Ok((false, "".to_string()));
|
||||
}
|
||||
"move_right" => {
|
||||
let current_input = form_state.get_current_input();
|
||||
if form_state.current_cursor_pos < current_input.len() {
|
||||
form_state.current_cursor_pos += 1;
|
||||
}
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos;
|
||||
return Ok((false, "".to_string()));
|
||||
}
|
||||
"move_up" => {
|
||||
if form_state.current_field == 0 {
|
||||
// Wrap to the last field when at the top
|
||||
form_state.current_field = form_state.fields.len() - 1;
|
||||
} else {
|
||||
form_state.current_field = form_state.current_field.saturating_sub(1);
|
||||
}
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
return Ok((false, "".to_string()));
|
||||
}
|
||||
"move_down" => {
|
||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
return Ok((false, "".to_string()));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
// Handle other keys (e.g., command mode)
|
||||
match key.code {
|
||||
KeyCode::Char(':') => {
|
||||
self.command_mode = true;
|
||||
self.command_input.clear();
|
||||
self.command_message.clear();
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
self.command_mode = false;
|
||||
self.command_input.clear();
|
||||
self.command_message.clear();
|
||||
}
|
||||
_ => {
|
||||
if !self.edit_mode_cooldown {
|
||||
let default_key = "i".to_string();
|
||||
let edit_key = config.keybindings.get("enter_edit_mode")
|
||||
.and_then(|keys| keys.first())
|
||||
.unwrap_or(&default_key);
|
||||
self.command_message = format!("Read-only mode - press {} to edit", edit_key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Edit mode handling
|
||||
if self.command_mode {
|
||||
match key.code {
|
||||
KeyCode::Enter => {
|
||||
let command = self.command_input.trim();
|
||||
if command.is_empty() {
|
||||
self.command_message = "Empty command".to_string();
|
||||
return Ok((false, self.command_message.clone()));
|
||||
}
|
||||
|
||||
let action = config.get_action_for_command(command)
|
||||
.unwrap_or("unknown");
|
||||
|
||||
if action == "save" {
|
||||
let is_new = *current_position == total_count + 1;
|
||||
|
||||
let message = if is_new {
|
||||
// POST new entry
|
||||
let post_request = PostAdresarRequest {
|
||||
firma: form_state.values[0].clone(),
|
||||
kz: form_state.values[1].clone(),
|
||||
drc: form_state.values[2].clone(),
|
||||
ulica: form_state.values[3].clone(),
|
||||
psc: form_state.values[4].clone(),
|
||||
mesto: form_state.values[5].clone(),
|
||||
stat: form_state.values[6].clone(),
|
||||
banka: form_state.values[7].clone(),
|
||||
ucet: form_state.values[8].clone(),
|
||||
skladm: form_state.values[9].clone(),
|
||||
ico: form_state.values[10].clone(),
|
||||
kontakt: form_state.values[11].clone(),
|
||||
telefon: form_state.values[12].clone(),
|
||||
skladu: form_state.values[13].clone(),
|
||||
fax: form_state.values[14].clone(),
|
||||
};
|
||||
let response = app_terminal.post_adresar(post_request).await?;
|
||||
// Update state
|
||||
let new_total = app_terminal.get_adresar_count().await?;
|
||||
*current_position = new_total;
|
||||
form_state.id = response.into_inner().id;
|
||||
"New entry created".to_string()
|
||||
} else {
|
||||
// PUT existing entry
|
||||
let put_request = PutAdresarRequest {
|
||||
id: form_state.id,
|
||||
firma: form_state.values[0].clone(),
|
||||
kz: form_state.values[1].clone(),
|
||||
drc: form_state.values[2].clone(),
|
||||
ulica: form_state.values[3].clone(),
|
||||
psc: form_state.values[4].clone(),
|
||||
mesto: form_state.values[5].clone(),
|
||||
stat: form_state.values[6].clone(),
|
||||
banka: form_state.values[7].clone(),
|
||||
ucet: form_state.values[8].clone(),
|
||||
skladm: form_state.values[9].clone(),
|
||||
ico: form_state.values[10].clone(),
|
||||
kontakt: form_state.values[11].clone(),
|
||||
telefon: form_state.values[12].clone(),
|
||||
skladu: form_state.values[13].clone(),
|
||||
fax: form_state.values[14].clone(),
|
||||
};
|
||||
let _ = app_terminal.put_adresar(put_request).await?;
|
||||
"Entry updated".to_string()
|
||||
};
|
||||
|
||||
*is_saved = true;
|
||||
form_state.has_unsaved_changes = false;
|
||||
self.command_input.clear(); // Clear the command input
|
||||
self.command_mode = false; // Reset command mode
|
||||
self.command_message.clear(); // Clear the command message
|
||||
return Ok((false, message));
|
||||
} else {
|
||||
let (should_exit, message) = app_terminal
|
||||
.handle_command(action, is_saved)
|
||||
.await?;
|
||||
self.command_message = message;
|
||||
self.command_input.clear(); // Clear the command input
|
||||
self.command_mode = false; // Reset command mode
|
||||
return Ok((should_exit, self.command_message.clone()));
|
||||
}
|
||||
}
|
||||
KeyCode::Char(c) => self.command_input.push(c),
|
||||
KeyCode::Backspace => {
|
||||
self.command_input.pop();
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
self.command_mode = false;
|
||||
self.command_input.clear();
|
||||
self.command_message.clear();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else {
|
||||
// Handle arrow keys in edit mode
|
||||
match key.code {
|
||||
KeyCode::Left => {
|
||||
form_state.current_cursor_pos = form_state.current_cursor_pos.saturating_sub(1);
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos;
|
||||
return Ok((false, "".to_string()));
|
||||
}
|
||||
KeyCode::Right => {
|
||||
let current_input = form_state.get_current_input();
|
||||
if form_state.current_cursor_pos < current_input.len() {
|
||||
form_state.current_cursor_pos += 1;
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos;
|
||||
}
|
||||
return Ok((false, "".to_string()));
|
||||
}
|
||||
KeyCode::Char(':') => {
|
||||
self.command_mode = true;
|
||||
self.command_input.clear();
|
||||
self.command_message.clear();
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
if config.is_exit_edit_mode(key.code, key.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();
|
||||
return Ok((false, self.command_message.clone()));
|
||||
}
|
||||
}
|
||||
KeyCode::Down => {
|
||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
}
|
||||
KeyCode::Up => {
|
||||
if form_state.current_field == 0 {
|
||||
form_state.current_field = form_state.fields.len() - 1;
|
||||
} else {
|
||||
form_state.current_field = form_state.current_field.saturating_sub(1);
|
||||
}
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
}
|
||||
KeyCode::Tab => {
|
||||
if key.modifiers.contains(KeyModifiers::SHIFT) {
|
||||
if form_state.current_field == 0 {
|
||||
form_state.current_field = form_state.fields.len() - 1;
|
||||
} else {
|
||||
form_state.current_field = form_state.current_field.saturating_sub(1);
|
||||
}
|
||||
} else {
|
||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
||||
}
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
}
|
||||
KeyCode::BackTab => {
|
||||
if form_state.current_field == 0 {
|
||||
form_state.current_field = form_state.fields.len() - 1;
|
||||
} else {
|
||||
form_state.current_field = form_state.current_field.saturating_sub(1);
|
||||
}
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
}
|
||||
KeyCode::Char(c) => {
|
||||
let cursor_pos = form_state.current_cursor_pos;
|
||||
let field_value = form_state.get_current_input_mut();
|
||||
let mut chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos <= chars.len() {
|
||||
chars.insert(cursor_pos, c);
|
||||
*field_value = chars.into_iter().collect();
|
||||
form_state.current_cursor_pos = cursor_pos + 1;
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos;
|
||||
form_state.has_unsaved_changes = true;
|
||||
}
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
if form_state.current_cursor_pos > 0 {
|
||||
let cursor_pos = form_state.current_cursor_pos;
|
||||
let field_value = form_state.get_current_input_mut();
|
||||
let mut chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos <= chars.len() && cursor_pos > 0 {
|
||||
chars.remove(cursor_pos - 1);
|
||||
*field_value = chars.into_iter().collect();
|
||||
form_state.current_cursor_pos = cursor_pos - 1;
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos;
|
||||
form_state.has_unsaved_changes = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Delete => {
|
||||
let cursor_pos = form_state.current_cursor_pos;
|
||||
let field_value = form_state.get_current_input_mut();
|
||||
let chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos < chars.len() {
|
||||
let mut new_chars = chars.clone();
|
||||
new_chars.remove(cursor_pos);
|
||||
*field_value = new_chars.into_iter().collect();
|
||||
form_state.has_unsaved_changes = true;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.edit_mode_cooldown = false;
|
||||
Ok((false, self.command_message.clone()))
|
||||
}
|
||||
}
|
||||
77
client/src/ui/handlers/form.rs
Normal file
77
client/src/ui/handlers/form.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
// src/client/ui/handlers/form.rs
|
||||
|
||||
use crate::client::components1::render_form;
|
||||
use crate::client::colors::Theme;
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui::Frame;
|
||||
|
||||
pub struct FormState {
|
||||
pub id: i64,
|
||||
pub fields: Vec<String>, // Use Vec<String> for dynamic field names
|
||||
pub values: Vec<String>, // Store field values dynamically
|
||||
pub current_field: usize,
|
||||
pub has_unsaved_changes: bool,
|
||||
pub current_cursor_pos: usize,
|
||||
}
|
||||
|
||||
impl FormState {
|
||||
/// Create a new FormState with dynamic fields.
|
||||
pub fn new(fields: Vec<String>) -> Self {
|
||||
let values = vec![String::new(); fields.len()]; // Initialize values for each field
|
||||
FormState {
|
||||
id: 0,
|
||||
fields,
|
||||
values,
|
||||
current_field: 0,
|
||||
has_unsaved_changes: false,
|
||||
current_cursor_pos: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(
|
||||
&self,
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
total_count: u64,
|
||||
current_position: u64,
|
||||
) {
|
||||
// Convert Vec<String> to Vec<&str> for fields
|
||||
let fields: Vec<&str> = self.fields.iter().map(|s| s.as_str()).collect();
|
||||
// Convert Vec<String> to Vec<&String> for values
|
||||
let values: Vec<&String> = self.values.iter().collect();
|
||||
|
||||
render_form(
|
||||
f,
|
||||
area,
|
||||
self,
|
||||
&fields,
|
||||
&self.current_field,
|
||||
&values,
|
||||
&theme,
|
||||
is_edit_mode,
|
||||
total_count,
|
||||
current_position,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn reset_to_empty(&mut self) {
|
||||
self.id = 0; // Reset ID to 0 for new entries
|
||||
self.values.iter_mut().for_each(|v| v.clear()); // Clear all values
|
||||
self.has_unsaved_changes = false;
|
||||
}
|
||||
|
||||
pub fn get_current_input(&self) -> &str {
|
||||
self.values
|
||||
.get(self.current_field)
|
||||
.map(|s| s.as_str())
|
||||
.unwrap_or("")
|
||||
}
|
||||
|
||||
pub fn get_current_input_mut(&mut self) -> &mut String {
|
||||
self.values
|
||||
.get_mut(self.current_field)
|
||||
.expect("Invalid current_field index")
|
||||
}
|
||||
}
|
||||
53
client/src/ui/handlers/render.rs
Normal file
53
client/src/ui/handlers/render.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
// src/client/ui/handlers/render.rs
|
||||
|
||||
use crate::client::components1::{render_command_line, render_preview_card, render_status_line};
|
||||
use crate::client::colors::Theme;
|
||||
use ratatui::layout::{Constraint, Direction, Layout};
|
||||
use ratatui::Frame;
|
||||
use super::form::FormState;
|
||||
|
||||
pub fn render_ui(
|
||||
f: &mut Frame,
|
||||
form_state: &mut FormState,
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
total_count: u64,
|
||||
current_position: u64,
|
||||
current_dir: &str,
|
||||
command_input: &str,
|
||||
command_mode: bool,
|
||||
command_message: &str,
|
||||
) {
|
||||
let root = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Min(10), // Main content area
|
||||
Constraint::Length(1), // Status line
|
||||
Constraint::Length(1), // Command line
|
||||
])
|
||||
.split(f.area());
|
||||
|
||||
// Main content area
|
||||
let main_chunks = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Percentage(60), Constraint::Percentage(40)])
|
||||
.split(root[0]);
|
||||
|
||||
// Left panel - Form
|
||||
form_state.render(f, main_chunks[0], theme, is_edit_mode, total_count, current_position);
|
||||
|
||||
// Right panel - Preview Card
|
||||
let preview_values: Vec<&String> = form_state.values.iter().collect();
|
||||
render_preview_card(
|
||||
f,
|
||||
main_chunks[1],
|
||||
&preview_values, // Pass dynamic values as &[&String]
|
||||
&theme,
|
||||
);
|
||||
|
||||
// Status line
|
||||
render_status_line(f, root[1], current_dir, theme, is_edit_mode);
|
||||
|
||||
// Command line
|
||||
render_command_line(f, root[2], command_input, command_mode, theme, command_message);
|
||||
}
|
||||
32
client/src/ui/handlers/state.rs
Normal file
32
client/src/ui/handlers/state.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
// src/client/ui/handlers/state.rs
|
||||
|
||||
use std::env;
|
||||
|
||||
pub struct AppState {
|
||||
pub is_saved: bool,
|
||||
pub current_dir: String,
|
||||
pub total_count: u64,
|
||||
pub current_position: u64,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let current_dir = env::current_dir()?
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
Ok(AppState {
|
||||
is_saved: false,
|
||||
current_dir,
|
||||
total_count: 0,
|
||||
current_position: 0,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn update_total_count(&mut self, total_count: u64) {
|
||||
self.total_count = total_count;
|
||||
}
|
||||
|
||||
pub fn update_current_position(&mut self, current_position: u64) {
|
||||
self.current_position = current_position;
|
||||
}
|
||||
}
|
||||
123
client/src/ui/handlers/ui.rs
Normal file
123
client/src/ui/handlers/ui.rs
Normal file
@@ -0,0 +1,123 @@
|
||||
// src/client/ui/handlers/ui.rs
|
||||
|
||||
use crate::client::terminal::AppTerminal;
|
||||
use crate::client::colors::Theme;
|
||||
use crate::client::config::Config;
|
||||
use crate::client::ui::handlers::{event::EventHandler, form::FormState, state::AppState, render::render_ui};
|
||||
|
||||
pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let config = Config::load()?;
|
||||
let mut app_terminal = AppTerminal::new().await?;
|
||||
let theme = Theme::from_str(&config.colors.theme);
|
||||
|
||||
// Fetch table structure at startup (one-time)
|
||||
// TODO: Later, consider implementing a live update for table structure changes.
|
||||
let table_structure = app_terminal.get_table_structure().await?;
|
||||
|
||||
// Extract the column names from the response
|
||||
let column_names: Vec<String> = table_structure
|
||||
.columns
|
||||
.iter()
|
||||
.map(|col| col.name.clone())
|
||||
.collect();
|
||||
|
||||
// Initialize FormState with dynamic fields
|
||||
let mut form_state = FormState::new(column_names);
|
||||
|
||||
// The rest of your UI initialization remains the same
|
||||
let mut event_handler = EventHandler::new();
|
||||
let mut app_state = AppState::new()?;
|
||||
|
||||
// Fetch the total count of Adresar entries
|
||||
let total_count = app_terminal.get_adresar_count().await?;
|
||||
app_state.update_total_count(total_count);
|
||||
app_state.update_current_position(total_count.saturating_add(1)); // Start in new entry mode
|
||||
form_state.reset_to_empty();
|
||||
|
||||
loop {
|
||||
let total_count = app_terminal.get_adresar_count().await?;
|
||||
app_state.update_total_count(total_count);
|
||||
|
||||
app_terminal.draw(|f| {
|
||||
render_ui(
|
||||
f,
|
||||
&mut form_state,
|
||||
&theme,
|
||||
event_handler.is_edit_mode,
|
||||
app_state.total_count,
|
||||
app_state.current_position,
|
||||
&app_state.current_dir,
|
||||
&event_handler.command_input,
|
||||
event_handler.command_mode,
|
||||
&event_handler.command_message,
|
||||
);
|
||||
})?;
|
||||
|
||||
let event = app_terminal.read_event()?;
|
||||
let (should_exit, message) = event_handler.handle_event(
|
||||
event,
|
||||
&config,
|
||||
&mut app_terminal,
|
||||
&mut form_state,
|
||||
&mut app_state.is_saved,
|
||||
app_state.total_count,
|
||||
&mut app_state.current_position,
|
||||
).await?;
|
||||
|
||||
// Handle position changes and update form state
|
||||
if !event_handler.is_edit_mode {
|
||||
// Ensure position never exceeds total_count + 1
|
||||
if app_state.current_position > total_count + 1 {
|
||||
app_state.current_position = total_count + 1;
|
||||
}
|
||||
if app_state.current_position > total_count {
|
||||
// New entry - reset form
|
||||
form_state.reset_to_empty();
|
||||
form_state.current_field = 0;
|
||||
} else if app_state.current_position >= 1 && app_state.current_position <= total_count {
|
||||
// Existing entry - load data
|
||||
match app_terminal.get_adresar_by_position(app_state.current_position).await {
|
||||
Ok(response) => {
|
||||
// Set the ID properly
|
||||
form_state.id = response.id;
|
||||
|
||||
// Update form values dynamically
|
||||
form_state.values = vec![
|
||||
response.firma,
|
||||
response.kz,
|
||||
response.drc,
|
||||
response.ulica,
|
||||
response.psc,
|
||||
response.mesto,
|
||||
response.stat,
|
||||
response.banka,
|
||||
response.ucet,
|
||||
response.skladm,
|
||||
response.ico,
|
||||
response.kontakt,
|
||||
response.telefon,
|
||||
response.skladu,
|
||||
response.fax,
|
||||
];
|
||||
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(current_input.len());
|
||||
form_state.has_unsaved_changes = false;
|
||||
event_handler.command_message = format!("Loaded entry {}", app_state.current_position);
|
||||
}
|
||||
Err(e) => {
|
||||
event_handler.command_message = format!("Error loading entry: {}", e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Invalid position - reset to first entry
|
||||
app_state.current_position = 1;
|
||||
}
|
||||
}
|
||||
|
||||
event_handler.command_message = message;
|
||||
if should_exit {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
5
client/src/ui/mod.rs
Normal file
5
client/src/ui/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
// src/client/ui/mod.rs
|
||||
pub mod models;
|
||||
pub mod handlers;
|
||||
|
||||
pub use handlers::*;
|
||||
0
client/src/ui/models.rs
Normal file
0
client/src/ui/models.rs
Normal file
Reference in New Issue
Block a user