use ratatui::{
buffer::Buffer,
layout::{Rect, Size},
style::Style,
text::{Line, Span, Text},
widgets::Widget,
};
use crate::{config::Theme, model::VariableCompletion};
const DEFAULT_STYLE: Style = Style::new();
#[derive(Clone)]
pub struct VariableCompletionWidget<'a>(Text<'a>, Size);
impl<'a> VariableCompletionWidget<'a> {
pub fn new(
completion: &'a VariableCompletion,
theme: &Theme,
is_highlighted: bool,
is_discarded: bool,
plain_style: bool,
full_content: bool,
) -> Self {
let mut line_style = DEFAULT_STYLE;
if is_highlighted && let Some(bg_color) = theme.highlight {
line_style = line_style.bg(bg_color.into());
}
let (primary_style, secondary_style) = match (plain_style, is_discarded, is_highlighted) {
(_, true, false) => (theme.secondary, theme.secondary),
(_, true, true) => (theme.highlight_secondary, theme.highlight_secondary),
(true, false, false) => (theme.primary, theme.primary),
(true, false, true) => (theme.highlight_primary, theme.highlight_primary),
(false, false, false) => (theme.primary, theme.secondary),
(false, false, true) => (theme.highlight_primary, theme.highlight_secondary),
};
let mut parts = vec![
Span::styled(&completion.variable, primary_style),
Span::styled(": ", primary_style),
Span::styled(&completion.suggestions_provider, secondary_style),
];
if full_content {
parts.insert(0, Span::styled("$ ", primary_style));
if !completion.is_global() {
parts.insert(1, Span::styled("(", primary_style));
parts.insert(2, Span::styled(&completion.root_cmd, primary_style));
parts.insert(3, Span::styled(") ", primary_style));
}
}
let text = Text::from(vec![Line::from(parts)]).style(line_style);
let width = text.width() as u16;
let height = text.height() as u16;
VariableCompletionWidget(text, Size::new(width, height))
}
pub fn size(&self) -> Size {
self.1
}
}
impl<'a> Widget for VariableCompletionWidget<'a> {
fn render(self, area: Rect, buf: &mut Buffer)
where
Self: Sized,
{
self.0.render(area, buf);
}
}