it compiled

This commit is contained in:
filipriec
2025-03-20 19:43:44 +01:00
parent 135b213f0d
commit 01ed33df0a
13 changed files with 213 additions and 148 deletions

View File

@@ -0,0 +1,12 @@
// src/components/handlers.rs
pub mod form;
pub mod preview_card;
pub mod command_line;
pub mod status_line;
pub mod canvas;
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;
pub use canvas::*;

View File

@@ -0,0 +1,51 @@
// src/components/handlers/canvas.rs
use ratatui::{
widgets::{Paragraph, Block, Borders},
layout::{Rect, Margin},
style::Style,
text::Text,
Frame,
};
use crate::config::colors::Theme;
pub struct CanvasState<'a> {
pub content: Text<'a>,
pub cursor_position: (u16, u16),
pub scroll_offset: u16,
}
pub fn render_canvas(
f: &mut Frame,
area: Rect,
state: &CanvasState,
theme: &Theme,
is_active: bool,
is_edit_mode: bool,
border_style: Style,
) {
// Render the container block
let container = Block::default()
.borders(Borders::ALL)
.border_style(border_style)
.style(Style::default().bg(theme.bg));
f.render_widget(container, area);
// Inner content area
let inner_area = area.inner(Margin {
horizontal: 1,
vertical: 1,
});
// Render the content
let content = Paragraph::new(state.content.clone())
.scroll((state.scroll_offset, 0));
f.render_widget(content, inner_area);
// Set cursor position if active
if is_active && is_edit_mode {
let cursor_x = inner_area.x + state.cursor_position.0;
let cursor_y = inner_area.y + state.cursor_position.1;
f.set_cursor_position((cursor_x, cursor_y));
}
}

View File

@@ -0,0 +1,35 @@
// src/client/components/command_line.rs
use ratatui::{
widgets::{Block, Paragraph},
style::Style,
layout::Rect,
Frame,
};
use crate::config::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);
}

View File

@@ -0,0 +1,154 @@
// src/components/handlers/form.rs
use ratatui::{
widgets::{Paragraph, Block, Borders},
layout::{Layout, Constraint, Direction, Rect, Margin, Alignment},
style::Style,
text::{Line, Span, Text},
Frame,
};
use crate::config::colors::Theme;
use crate::ui::form::FormState;
use crate::components::canvas::{self, CanvasState};
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,
) {
// Split the main area into canvas and container sections
let main_layout = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(3), // For header and controls
Constraint::Min(1), // For canvas
])
.split(area);
// Render the header section
render_header(f, main_layout[0], theme, total_count, current_position);
// Render the canvas area
render_canvas_area(
f,
main_layout[1],
form_state,
fields,
current_field,
inputs,
theme,
is_edit_mode,
);
}
fn render_header(
f: &mut Frame,
area: Rect,
theme: &Theme,
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 header content
let inner_area = area.inner(Margin {
horizontal: 1,
vertical: 1,
});
// Render the count and position
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, inner_area);
}
fn render_canvas_area(
f: &mut Frame,
area: Rect,
form_state: &FormState,
fields: &[&str],
current_field: &usize,
inputs: &[&String],
theme: &Theme,
is_edit_mode: bool,
) {
// Split canvas area into labels and inputs
let columns = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
.split(area);
// Render labels
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: columns[0].y + i as u16,
width: columns[0].width,
height: 1,
});
}
// Create canvas states for each input field
let canvas_states: Vec<CanvasState> = inputs.iter().enumerate().map(|(i, input)| {
CanvasState {
content: Text::raw(input.as_str()),
cursor_position: if i == *current_field {
(form_state.current_cursor_pos as u16, 0)
} else {
(0, 0)
},
scroll_offset: 0,
}
}).collect();
// Render input canvases
let input_height = area.height / fields.len() as u16;
for (i, state) in canvas_states.iter().enumerate() {
let is_active = i == *current_field;
let border_style = if is_edit_mode {
if form_state.has_unsaved_changes {
Style::default().fg(theme.warning)
} else {
Style::default().fg(theme.accent)
}
} else {
Style::default().fg(theme.secondary)
};
let canvas_area = Rect {
x: columns[1].x,
y: columns[1].y + i as u16 * input_height,
width: columns[1].width,
height: input_height,
};
canvas::render_canvas(
f,
canvas_area,
state,
theme,
is_active,
is_edit_mode,
border_style,
);
}
}

View 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::config::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);
}

View File

@@ -0,0 +1,80 @@
// src/client/components/handlers/status_line.rs
use ratatui::{
widgets::Paragraph,
style::Style,
layout::Rect,
Frame,
text::{Line, Span},
};
use crate::config::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);
}

View File

@@ -0,0 +1,5 @@
// src/components/mod.rs
pub mod models;
pub mod handlers;
pub use handlers::*;

View File