81 lines
2.5 KiB
Rust
81 lines
2.5 KiB
Rust
// 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);
|
|
}
|