better buffer list

This commit is contained in:
filipriec
2025-04-14 23:35:56 +02:00
parent 20f9fae141
commit 3ad8dc6490
2 changed files with 60 additions and 28 deletions

View File

@@ -3,16 +3,17 @@
use crate::config::colors::themes::Theme;
use crate::state::app::state::AppState;
use ratatui::{
layout::{Alignment, Rect}, // Added Alignment
style::{Style, Stylize},
text::{Line, Span}, // Added Span
widgets::{Block, Borders, Paragraph}, // Changed widgets
layout::{Alignment, Rect},
style::{Modifier, Style, Stylize},
text::{Line, Span},
widgets::Paragraph, // Removed Block and Borders
Frame,
};
use unicode_width::UnicodeWidthStr;
pub fn render_buffer_list(
f: &mut Frame,
area: Rect,
area: Rect, // Still 1 line high
theme: &Theme,
app_state: &AppState,
) {
@@ -28,34 +29,65 @@ pub fn render_buffer_list(
} else if app_state.ui.show_admin {
current_buffer_name = "Admin Panel";
} else if app_state.ui.show_form {
// Potentially make this more specific later (e.g., show table name)
current_buffer_name = "Data Form";
}
// Add more conditions if other views exist
// Create the text line for the buffer name with padding
let buffer_text = Line::from(vec![
Span::raw(" "), // Left padding
Span::styled(
current_buffer_name,
Style::default().fg(theme.bg).bg(theme.highlight), // Style active tab
),
Span::raw(" "), // Right padding
]);
// --- 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
// Create the paragraph centered within the block
let paragraph = Paragraph::new(buffer_text)
.alignment(Alignment::Left) // Align text left, padding handles spacing
.block(
Block::default()
// Keep only the bottom border to act as a separator
.borders(Borders::BOTTOM)
.border_style(Style::default().fg(theme.secondary)),
)
// Set the background for the entire area (behind the text/block)
.style(Style::default().bg(theme.bg));
let active_style = Style::default()
.fg(theme.bg) // Text on active background
.bg(theme.highlight); // Active background
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
let inactive_style = Style::default()
.fg(theme.fg) // Default text color
.bg(theme.bg); // Default background
// --- Create Spans ---
let buffer_text = format!(" {} ", current_buffer_name); // Add padding
// 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
// Span 2: The active buffer text
let buffer_span = Span::styled(buffer_text.clone(), active_style);
// Span 3: The right separator pointing away from the active buffer
let right_sep = Span::styled(
RIGHT_SEPARATOR,
separator_style_active_to_inactive,
);
// 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(
" ".repeat(remaining_width as usize),
inactive_style, // Filler uses 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 paragraph = Paragraph::new(buffer_line).alignment(Alignment::Left);
// Render the paragraph widget
f.render_widget(paragraph, area);
}

View File

@@ -44,7 +44,7 @@ pub fn render_ui(
// Adjust layout based on whether buffer list is shown
let constraints = if app_state.ui.show_buffer_list {
vec![
Constraint::Length(2), // Buffer list
Constraint::Length(1), // Buffer list
Constraint::Min(1), // Main content
Constraint::Length(1), // Status line
Constraint::Length(1), // Command line