history of buffers implemented now

This commit is contained in:
filipriec
2025-04-14 23:58:58 +02:00
parent 3ad8dc6490
commit aa8887318f
3 changed files with 110 additions and 57 deletions

View File

@@ -4,90 +4,91 @@ use crate::config::colors::themes::Theme;
use crate::state::app::state::AppState;
use ratatui::{
layout::{Alignment, Rect},
style::{Modifier, Style, Stylize},
style::{Style, Stylize},
text::{Line, Span},
widgets::Paragraph, // Removed Block and Borders
widgets::Paragraph,
Frame,
};
use unicode_width::UnicodeWidthStr;
pub fn render_buffer_list(
f: &mut Frame,
area: Rect, // Still 1 line high
area: Rect,
theme: &Theme,
app_state: &AppState,
) {
let mut current_buffer_name = "*scratch*"; // Default
// Determine the active buffer name based on UI state
if app_state.ui.show_intro {
current_buffer_name = "Intro";
} else if app_state.ui.show_register {
current_buffer_name = "Register";
} else if app_state.ui.show_login {
current_buffer_name = "Login";
} else if app_state.ui.show_admin {
current_buffer_name = "Admin Panel";
} else if app_state.ui.show_form {
current_buffer_name = "Data Form";
}
// Add more conditions if other views exist
// --- Style Definitions ---
// Use Powerline symbols if available, otherwise fallback
// Ensure your terminal font supports these for the best look!
const RIGHT_SEPARATOR: &str = ""; // U+E0B0
// const RIGHT_SEPARATOR: &str = ">"; // Fallback
const LEFT_SEPARATOR: &str = ""; // U+E0B2
// const LEFT_SEPARATOR: &str = "<"; // Fallback
let active_style = Style::default()
.fg(theme.bg) // Text on active background
.bg(theme.highlight); // Active background
.fg(theme.bg)
.bg(theme.highlight);
let separator_style_active_to_inactive = Style::default()
.fg(theme.highlight) // Separator color matches the background it comes from
.bg(theme.bg); // Background matches the area it points into
.fg(theme.highlight)
.bg(theme.bg);
let separator_style_inactive_to_active = Style::default()
.fg(theme.highlight)
.bg(theme.bg);
let separator_style_inactive_to_inactive = Style::default()
.fg(theme.fg)
.bg(theme.bg);
let inactive_style = Style::default()
.fg(theme.fg) // Default text color
.bg(theme.bg); // Default background
.fg(theme.fg)
.bg(theme.bg);
// --- Create Spans ---
let buffer_text = format!(" {} ", current_buffer_name); // Add padding
let mut spans = Vec::new();
let history = &app_state.ui.buffer_history;
let history_len = history.len();
let mut current_width = 0;
// Span 1: Left edge (no separator needed if it's the first element)
// If you had multiple buffers, the *first* one wouldn't have a left separator.
// let left_sep = Span::raw(""); // Example for first buffer
for (i, view) in history.iter().enumerate() {
let is_active = i == history_len - 1;
let buffer_name = view.display_name();
let buffer_text = format!(" {} ", buffer_name);
let text_width = UnicodeWidthStr::width(buffer_text.as_str());
let separator_width = UnicodeWidthStr::width(RIGHT_SEPARATOR);
// Span 2: The active buffer text
let buffer_span = Span::styled(buffer_text.clone(), active_style);
let needed_width = if i > 0 { separator_width } else { 0 } + text_width + separator_width;
if current_width + needed_width > area.width as usize {
break;
}
// Span 3: The right separator pointing away from the active buffer
let right_sep = Span::styled(
RIGHT_SEPARATOR,
separator_style_active_to_inactive,
);
if i > 0 {
let sep_style = if is_active {
separator_style_inactive_to_active
} else {
separator_style_inactive_to_inactive
};
spans.push(Span::styled(LEFT_SEPARATOR, sep_style));
current_width += separator_width;
}
// Span 4: Filler for the rest of the line
let text_width = UnicodeWidthStr::width(buffer_text.as_str());
// Account for the width of the separator character (usually 1)
let separator_width = UnicodeWidthStr::width(RIGHT_SEPARATOR);
let total_used_width = (text_width + separator_width) as u16;
let remaining_width = area.width.saturating_sub(total_used_width);
let filler_span = Span::styled(
let text_style = if is_active { active_style } else { inactive_style };
spans.push(Span::styled(buffer_text, text_style));
current_width += text_width;
let sep_style = if is_active {
separator_style_active_to_inactive
} else {
separator_style_inactive_to_inactive
};
spans.push(Span::styled(RIGHT_SEPARATOR, sep_style));
current_width += separator_width;
}
let remaining_width = area.width.saturating_sub(current_width as u16);
spans.push(Span::styled(
" ".repeat(remaining_width as usize),
inactive_style, // Filler uses inactive style
);
inactive_style,
));
// --- Combine into Line ---
// Order: Buffer Text -> Right Separator -> Filler
// (If multiple buffers: LeftSep -> Text -> RightSep -> LeftSep -> Text -> ...)
let buffer_line = Line::from(vec![buffer_span, right_sep, filler_span]);
// --- Render ---
let buffer_line = Line::from(spans);
let paragraph = Paragraph::new(buffer_line).alignment(Alignment::Left);
f.render_widget(paragraph, area);
}