Skip to content

Commit

Permalink
rm: builder for core::listbox
Browse files Browse the repository at this point in the history
  • Loading branch information
ynqa committed Feb 15, 2024
1 parent 4621faf commit a816a27
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 118 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/bk/select.rs → examples/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use promkit::{error::Result, preset::Select};
fn main() -> Result {
let mut p = Select::new(0..100)
.title("What number do you like?")
.lines(5)
.window_size(5)
.prompt()?;
println!("result: {:?}", p.run()?);
Ok(())
Expand Down
2 changes: 0 additions & 2 deletions src/core/listbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use std::{fmt, iter::FromIterator};

mod render;
pub use render::Renderer;
mod build;
pub use build::Builder;

use crate::core::cursor::Cursor;

Expand Down
63 changes: 0 additions & 63 deletions src/core/listbox/build.rs

This file was deleted.

42 changes: 35 additions & 7 deletions src/core/listbox/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,45 @@ use crate::{
event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers},
style::ContentStyle,
},
error::Result,
grapheme::{trim, Graphemes},
listbox::Listbox,
pane::Pane,
render::{AsAny, Renderable},
render::{AsAny, Renderable, State},
};

#[derive(Clone)]
pub struct Renderer {
pub listbox: Listbox,

pub style: ContentStyle,
/// Style for selected line.
pub active_item_style: ContentStyle,
/// Style for un-selected line.
pub inactive_item_style: ContentStyle,

/// Symbol for selected line.
pub cursor: String,
pub cursor_style: ContentStyle,
pub lines: Option<usize>,

/// Window size.
pub window_size: Option<usize>,
}

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

impl Renderable for Renderer {
Expand All @@ -30,23 +55,26 @@ impl Renderable for Renderer {
.enumerate()
.map(|(i, item)| {
if i == self.listbox.position() {
Graphemes::new_with_style(format!("{}{}", self.cursor, item), self.cursor_style)
Graphemes::new_with_style(
format!("{}{}", self.cursor, item),
self.active_item_style,
)
} else {
Graphemes::new_with_style(
format!(
"{}{}",
" ".repeat(Graphemes::new(self.cursor.clone()).widths()),
item
),
self.style,
self.inactive_item_style,
)
}
})
.collect::<Vec<Graphemes>>();

let trimed = matrix.iter().map(|row| trim(width as usize, row)).collect();

Pane::new(trimed, self.listbox.position(), self.lines)
Pane::new(trimed, self.listbox.position(), self.window_size)
}

/// Default key bindings for item picker.
Expand Down
4 changes: 2 additions & 2 deletions src/preset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub use checkbox::Checkbox;
// pub use readline::Readline;
// mod password;
// pub use password::Password;
// mod select;
// pub use select::Select;
mod select;
pub use select::Select;
// mod queryselect;
// pub use queryselect::QuerySelect;
// mod tree;
Expand Down
69 changes: 50 additions & 19 deletions src/preset/select.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,83 @@
use std::fmt::Display;

use crate::{
crossterm::style::{Attribute, Attributes, Color, ContentStyle},
error::Result,
listbox,
preset::theme::select::Theme,
render::{Renderable, State},
style::Style,
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,

/// Symbol for selected line.
pub cursor: String,
}

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(),
cursor: String::from("❯ "),
}
}
}

pub struct Select {
title_builder: text::Builder,
listbox_builder: listbox::Builder,
title: String,
listbox: listbox::Listbox,
theme: Theme,
window_size: Option<usize>,
}

impl Select {
pub fn new<T: Display, I: IntoIterator<Item = T>>(items: I) -> Self {
Self {
title_builder: Default::default(),
listbox_builder: listbox::Builder::new(items),
title: Default::default(),
listbox: listbox::Listbox::from_iter(items),
theme: Default::default(),
window_size: Default::default(),
}
.theme(Theme::default())
}

pub fn theme(mut self, theme: Theme) -> Self {
self.title_builder = self.title_builder.style(theme.title_style);
self.listbox_builder = self
.listbox_builder
.cursor(theme.cursor)
.style(theme.item_style)
.cursor_style(theme.cursor_style);
pub fn title<T: AsRef<str>>(mut self, text: T) -> Self {
self.title = text.as_ref().to_string();
self
}

pub fn title<T: AsRef<str>>(mut self, text: T) -> Self {
self.title_builder = self.title_builder.text(text);
pub fn theme(mut self, theme: Theme) -> Self {
self.theme = theme;
self
}

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

pub fn prompt(self) -> Result<Prompt<String>> {
Prompt::try_new(
vec![
self.title_builder.build_state()?,
self.listbox_builder.build_state()?,
State::<text::Renderer>::try_new(self.title, self.theme.title_style)?,
State::<listbox::Renderer>::try_new(
self.listbox,
self.theme.active_item_style,
self.theme.inactive_item_style,
self.theme.cursor,
self.window_size,
)?,
],
|_, _| Ok(true),
|renderables: &Vec<Box<dyn Renderable + 'static>>| -> Result<String> {
Expand Down
24 changes: 0 additions & 24 deletions src/preset/theme/select.rs

This file was deleted.

0 comments on commit a816a27

Please sign in to comment.