Skip to content
Next Next commit
Add struct Theme
  • Loading branch information
adaschma committed Jun 21, 2024
commit 5e6f91fe6976509656d1a15d195e71ddddb26ab8
39 changes: 24 additions & 15 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,12 @@ pub struct Reedline {
// Highlight the edit buffer
highlighter: Box<dyn Highlighter>,

// Style used for visual selection
visual_selection_style: Style,

// Showcase hints based on various strategies (history, language-completion, spellcheck, etc)
hinter: Option<Box<dyn Hinter>>,
hide_hints: bool,

// Use ansi coloring or not
use_ansi_coloring: bool,
theme: Theme,

// Engine Menus
menus: Vec<ReedlineMenu>,
Expand Down Expand Up @@ -182,6 +179,20 @@ impl Drop for Reedline {
}
}

struct Theme {
visual_selection: Style,
use_ansi_coloring: bool,
}

impl Default for Theme {
fn default() -> Self {
Self {
visual_selection: Style::new().on(Color::LightGray),
use_ansi_coloring: true,
}
}
}

impl Reedline {
const FILTERED_ITEM_ID: HistoryItemId = HistoryItemId(i64::MAX);

Expand All @@ -191,7 +202,6 @@ impl Reedline {
let history = Box::<FileBackedHistory>::default();
let painter = Painter::new(std::io::BufWriter::new(std::io::stderr()));
let buffer_highlighter = Box::<ExampleHighlighter>::default();
let visual_selection_style = Style::new().on(Color::LightGray);
let completer = Box::<DefaultCompleter>::default();
let hinter = None;
let validator = None;
Expand Down Expand Up @@ -219,11 +229,10 @@ impl Reedline {
quick_completions: false,
partial_completions: false,
highlighter: buffer_highlighter,
visual_selection_style,
hinter,
hide_hints: false,
validator,
use_ansi_coloring: true,
theme: Theme::default(),
menus: Vec::new(),
buffer_editor: None,
cursor_shapes: None,
Expand Down Expand Up @@ -356,7 +365,7 @@ impl Reedline {
/// and in the command line syntax highlighting.
#[must_use]
pub fn with_ansi_colors(mut self, use_ansi_coloring: bool) -> Self {
self.use_ansi_coloring = use_ansi_coloring;
self.theme.use_ansi_coloring = use_ansi_coloring;
self
}

Expand Down Expand Up @@ -385,7 +394,7 @@ impl Reedline {
/// A builder that configures the style used for visual selection
#[must_use]
pub fn with_visual_selection_style(mut self, style: Style) -> Self {
self.visual_selection_style = style;
self.theme.visual_selection = style;
self
}

Expand Down Expand Up @@ -1682,7 +1691,7 @@ impl Reedline {
let res_string = self.history_cursor.string_at_cursor().unwrap_or_default();

// Highlight matches
let res_string = if self.use_ansi_coloring {
let res_string = if self.theme.use_ansi_coloring {
let match_highlighter = SimpleMatchHighlighter::new(substring);
let styled = match_highlighter.highlight(&res_string, 0);
styled.render_simple()
Expand All @@ -1704,7 +1713,7 @@ impl Reedline {
&lines,
self.prompt_edit_mode(),
None,
self.use_ansi_coloring,
self.theme.use_ansi_coloring,
&self.cursor_shapes,
)?;
}
Expand All @@ -1723,13 +1732,13 @@ impl Reedline {
.highlighter
.highlight(buffer_to_paint, cursor_position_in_buffer);
if let Some((from, to)) = self.editor.get_selection() {
styled_text.style_range(from, to, self.visual_selection_style);
styled_text.style_range(from, to, self.theme.visual_selection);
}

let (before_cursor, after_cursor) = styled_text.render_around_insertion_point(
cursor_position_in_buffer,
prompt,
self.use_ansi_coloring,
self.theme.use_ansi_coloring,
);

let hint: String = if self.hints_active() {
Expand All @@ -1738,7 +1747,7 @@ impl Reedline {
buffer_to_paint,
cursor_position_in_buffer,
self.history.as_ref(),
self.use_ansi_coloring,
self.theme.use_ansi_coloring,
)
})
} else {
Expand Down Expand Up @@ -1781,7 +1790,7 @@ impl Reedline {
&lines,
self.prompt_edit_mode(),
menu,
self.use_ansi_coloring,
self.theme.use_ansi_coloring,
&self.cursor_shapes,
)
}
Expand Down