Skip to content

Commit

Permalink
refactor: use renderer instead of intermediate structures
Browse files Browse the repository at this point in the history
  • Loading branch information
ynqa committed Feb 16, 2024
1 parent dab8645 commit b924b4f
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 397 deletions.
23 changes: 1 addition & 22 deletions src/core/checkbox/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use crate::{
event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers},
style::ContentStyle,
},
error::Result,
grapheme::{trim, Grapheme, Graphemes},
pane::Pane,
render::{AsAny, Renderable, State},
render::{AsAny, Renderable},
};

use super::Checkbox;
Expand All @@ -31,26 +30,6 @@ pub struct Renderer {
pub window_size: Option<usize>,
}

impl State<Renderer> {
pub fn try_new(
checkbox: Checkbox,
cursor: String,
mark: char,
active_item_style: ContentStyle,
inactive_item_style: ContentStyle,
window_size: Option<usize>,
) -> Result<Box<State<Renderer>>> {
Ok(Box::new(State::<Renderer>::new(Renderer {
checkbox,
cursor,
mark,
active_item_style,
inactive_item_style,
window_size,
})))
}
}

impl Renderable for Renderer {
fn make_pane(&self, width: u16) -> Pane {
let f = |idx: usize, item: &String| -> String {
Expand Down
21 changes: 1 addition & 20 deletions src/core/listbox/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use crate::{
event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers},
style::ContentStyle,
},
error::Result,
grapheme::{trim, Graphemes},
pane::Pane,
render::{AsAny, Renderable, State},
render::{AsAny, Renderable},
};

use super::Listbox;
Expand All @@ -29,24 +28,6 @@ pub struct Renderer {
pub window_size: Option<usize>,
}

impl State<Renderer> {
pub fn try_new(
listbox: Listbox,
cursor: String,
active_item_style: ContentStyle,
inactive_item_style: ContentStyle,
window_size: Option<usize>,
) -> Result<Box<State<Renderer>>> {
Ok(Box::new(State::<Renderer>::new(Renderer {
listbox,
cursor,
active_item_style,
inactive_item_style,
window_size,
})))
}
}

impl Renderable for Renderer {
fn make_pane(&self, width: u16) -> Pane {
let matrix = self
Expand Down
9 changes: 1 addition & 8 deletions src/core/text/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use std::any::Any;

use crate::{
crossterm::{event::Event, style::ContentStyle},
error::Result,
grapheme::{matrixify, Graphemes},
pane::Pane,
render::{AsAny, Renderable, State},
render::{AsAny, Renderable},
};

#[derive(Clone)]
Expand All @@ -16,12 +15,6 @@ pub struct Renderer {
pub style: ContentStyle,
}

impl State<Renderer> {
pub fn try_new(text: String, style: ContentStyle) -> Result<Box<State<Renderer>>> {
Ok(Box::new(State::<Renderer>::new(Renderer { text, style })))
}
}

impl Renderable for Renderer {
fn make_pane(&self, width: u16) -> Pane {
Pane::new(
Expand Down
34 changes: 2 additions & 32 deletions src/core/text_editor/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::{
event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers},
style::ContentStyle,
},
error::Result,
grapheme::{matrixify, Graphemes},
pane::Pane,
render::{AsAny, Renderable, State},
Expand All @@ -32,40 +31,11 @@ pub struct Renderer {
pub inactive_char_style: ContentStyle,

/// Edit mode: insert or overwrite.
pub mode: Mode,
pub edit_mode: Mode,
/// Window size.
pub window_size: Option<usize>,
}

impl State<Renderer> {
#![allow(clippy::too_many_arguments)]
pub fn try_new(
texteditor: TextEditor,
history: Option<History>,
suggest: Suggest,
ps: String,
mask: Option<char>,
ps_style: ContentStyle,
active_char_style: ContentStyle,
inactive_char_style: ContentStyle,
mode: Mode,
window_size: Option<usize>,
) -> Result<Box<State<Renderer>>> {
Ok(Box::new(State::<Renderer>::new(Renderer {
texteditor,
history,
suggest,
ps,
ps_style,
active_char_style,
inactive_char_style,
mode,
mask,
window_size,
})))
}
}

impl Renderable for Renderer {
fn make_pane(&self, width: u16) -> Pane {
let mut buf = Graphemes::default();
Expand Down Expand Up @@ -199,7 +169,7 @@ impl Renderable for Renderer {
modifiers: KeyModifiers::SHIFT,
kind: KeyEventKind::Press,
state: KeyEventState::NONE,
}) => match self.mode {
}) => match self.edit_mode {
Mode::Insert => self.texteditor.insert(*ch),
Mode::Overwrite => self.texteditor.overwrite(*ch),
},
Expand Down
23 changes: 1 addition & 22 deletions src/core/tree/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use crate::{
event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers},
style::ContentStyle,
},
error::Result,
grapheme::{trim, Graphemes},
pane::Pane,
render::{AsAny, Renderable, State},
render::{AsAny, Renderable},
tree::{NodeWithDepth, Tree},
};

Expand All @@ -30,26 +29,6 @@ pub struct Renderer {
pub window_size: Option<usize>,
}

impl State<Renderer> {
pub fn try_new(
tree: Tree,
folded_symbol: String,
unfolded_symbol: String,
active_item_style: ContentStyle,
inactive_item_style: ContentStyle,
window_size: Option<usize>,
) -> Result<Box<State<Renderer>>> {
Ok(Box::new(State::<Renderer>::new(Renderer {
tree,
folded_symbol,
unfolded_symbol,
active_item_style,
inactive_item_style,
window_size,
})))
}
}

impl Renderable for Renderer {
fn make_pane(&self, width: u16) -> crate::pane::Pane {
let symbol = |item: &NodeWithDepth| -> &str {
Expand Down
83 changes: 34 additions & 49 deletions src/preset/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,86 +9,71 @@ use crate::{
text, Prompt,
};

pub struct Theme {
/// Style for title (enabled if you set title).
pub title_style: ContentStyle,

/// Style for selected item.
pub active_item_style: ContentStyle,
/// Style for un-selected item.
pub inactive_item_style: ContentStyle,
}

impl Default for Theme {
fn default() -> Self {
Self {
title_style: Style::new()
.attrs(Attributes::from(Attribute::Bold))
.build(),
active_item_style: Style::new().fgc(Color::DarkCyan).build(),
inactive_item_style: Style::new().build(),
}
}
}

pub struct Checkbox {
title: String,
checkbox: checkbox::Checkbox,
theme: Theme,
cursor: String,
mark: char,
window_size: Option<usize>,
title_renderer: text::Renderer,
checkbox_renderer: checkbox::Renderer,
}

impl Checkbox {
pub fn new<T: Display, I: IntoIterator<Item = T>>(items: I) -> Self {
Self {
title: Default::default(),
checkbox: checkbox::Checkbox::from_iter(items),
theme: Default::default(),
cursor: String::from("❯ "),
mark: '■',
window_size: Default::default(),
title_renderer: text::Renderer {
text: Default::default(),
style: Style::new()
.attrs(Attributes::from(Attribute::Bold))
.build(),
},
checkbox_renderer: checkbox::Renderer {
checkbox: checkbox::Checkbox::from_iter(items),
cursor: String::from("❯ "),
mark: '■',
active_item_style: Style::new().fgc(Color::DarkCyan).build(),
inactive_item_style: Style::new().build(),
window_size: Default::default(),
},
}
}

pub fn title<T: AsRef<str>>(mut self, text: T) -> Self {
self.title = text.as_ref().to_string();
self.title_renderer.text = text.as_ref().to_string();
self
}

pub fn theme(mut self, theme: Theme) -> Self {
self.theme = theme;
pub fn title_style(mut self, style: ContentStyle) -> Self {
self.title_renderer.style = style;
self
}

pub fn cursor<T: AsRef<str>>(mut self, cursor: T) -> Self {
self.cursor = cursor.as_ref().to_string();
self.checkbox_renderer.cursor = cursor.as_ref().to_string();
self
}

pub fn mark(mut self, mark: char) -> Self {
self.mark = mark;
self.checkbox_renderer.mark = mark;
self
}

pub fn active_item_style(mut self, style: ContentStyle) -> Self {
self.checkbox_renderer.active_item_style = style;
self
}

pub fn inactive_item_style(mut self, style: ContentStyle) -> Self {
self.checkbox_renderer.inactive_item_style = style;
self
}

pub fn window_size(mut self, window_size: usize) -> Self {
self.window_size = Some(window_size);
self.checkbox_renderer.window_size = Some(window_size);
self
}

pub fn prompt(self) -> Result<Prompt<Vec<String>>> {
Prompt::try_new(
vec![
State::<text::Renderer>::try_new(self.title, self.theme.title_style)?,
State::<checkbox::Renderer>::try_new(
self.checkbox,
self.cursor,
self.mark,
self.theme.active_item_style,
self.theme.inactive_item_style,
self.window_size,
)?,
Box::new(State::<text::Renderer>::new(self.title_renderer)),
Box::new(State::<checkbox::Renderer>::new(self.checkbox_renderer)),
],
|_, _| Ok(true),
|renderables: &Vec<Box<dyn Renderable + 'static>>| -> Result<Vec<String>> {
Expand Down
Loading

0 comments on commit b924b4f

Please sign in to comment.