use std::{cell::RefCell, fmt::Display};
use promkit_widgets::{
checkbox,
text::{self, Text},
};
use crate::{
crossterm::style::{Attribute, Attributes, Color, ContentStyle},
switch::ActiveKeySwitcher,
Prompt,
};
pub mod keymap;
pub mod render;
pub struct Checkbox {
keymap: ActiveKeySwitcher<keymap::Keymap>,
title_state: text::State,
checkbox_state: checkbox::State,
}
impl Checkbox {
pub fn new<T: Display, I: IntoIterator<Item = T>>(items: I) -> Self {
Self {
title_state: text::State {
style: ContentStyle {
attributes: Attributes::from(Attribute::Bold),
..Default::default()
},
..Default::default()
},
checkbox_state: checkbox::State {
checkbox: checkbox::Checkbox::from_displayable(items),
cursor: String::from("❯ "),
active_mark: '☒',
inactive_mark: '☐',
active_item_style: ContentStyle {
foreground_color: Some(Color::DarkCyan),
..Default::default()
},
inactive_item_style: ContentStyle::default(),
lines: Default::default(),
},
keymap: ActiveKeySwitcher::new("default", self::keymap::default),
}
}
pub fn new_with_checked<T: Display, I: IntoIterator<Item = (T, bool)>>(items: I) -> Self {
Self {
title_state: text::State {
style: ContentStyle {
attributes: Attributes::from(Attribute::Bold),
..Default::default()
},
..Default::default()
},
checkbox_state: checkbox::State {
checkbox: checkbox::Checkbox::new_with_checked(items),
cursor: String::from("❯ "),
active_mark: '☒',
inactive_mark: '☐',
active_item_style: ContentStyle {
foreground_color: Some(Color::DarkCyan),
..Default::default()
},
inactive_item_style: ContentStyle::default(),
lines: Default::default(),
},
keymap: ActiveKeySwitcher::new("default", self::keymap::default),
}
}
pub fn title<T: AsRef<str>>(mut self, text: T) -> Self {
self.title_state.text = Text::from(text);
self
}
pub fn title_style(mut self, style: ContentStyle) -> Self {
self.title_state.style = style;
self
}
pub fn cursor<T: AsRef<str>>(mut self, cursor: T) -> Self {
self.checkbox_state.cursor = cursor.as_ref().to_string();
self
}
pub fn active_mark(mut self, mark: char) -> Self {
self.checkbox_state.active_mark = mark;
self
}
pub fn active_item_style(mut self, style: ContentStyle) -> Self {
self.checkbox_state.active_item_style = style;
self
}
pub fn inactive_item_style(mut self, style: ContentStyle) -> Self {
self.checkbox_state.inactive_item_style = style;
self
}
pub fn checkbox_lines(mut self, lines: usize) -> Self {
self.checkbox_state.lines = Some(lines);
self
}
pub fn register_keymap<K: AsRef<str>>(mut self, key: K, handler: keymap::Keymap) -> Self {
self.keymap = self.keymap.register(key, handler);
self
}
pub fn prompt(self) -> anyhow::Result<Prompt<render::Renderer>> {
Ok(Prompt {
renderer: render::Renderer {
keymap: RefCell::new(self.keymap),
title_state: self.title_state,
checkbox_state: self.checkbox_state,
},
})
}
}