use std::borrow::Cow;
use ratatui::text::Text;
use crate::prelude::*;
use super::*;
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct Checkbox {
pub name: Cow<'static, str>,
pub value: bool,
}
impl Field for Checkbox {
type Value = bool;
type Builder = Builder;
fn name(&self) -> &str {
&self.name
}
fn input(&mut self, key: KeyEvent) -> InputResult {
if let KeyCode::Up | KeyCode::Down = key.code {
InputResult::Ignored
} else {
self.value = !self.value;
InputResult::Updated
}
}
fn format(&self, _focused: bool) -> Text {
match self.value {
true => "✓",
false => "𐄂",
}.into()
}
fn value(&self) -> &Self::Value {
&self.value
}
fn into_value(self) -> Self::Value {
self.value
}
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct Builder<const NAME: bool = false>(Checkbox);
impl Default for Builder {
fn default() -> Self {
Self(Checkbox {
name: Default::default(),
value: false,
})
}
}
impl<const NAME: bool> Builder<NAME> {
pub fn name(self, name: impl Into<Cow<'static, str>>) -> Builder<true> {
let name = name.into();
Builder(Checkbox{ name, ..self.0 })
}
pub fn value(self, value: bool) -> Self {
Builder(Checkbox{ value, ..self.0 })
}
}
impl Build for Builder<true> {
type Field = Checkbox;
fn build(self) -> Checkbox {
self.0
}
}
#[cfg(test)]
mod tests {
use crate::{prelude::*, field::*};
#[test]
fn input() {
let test = |key_code: KeyCode, expected: InputResult| {
let mut checkbox = Checkbox::builder()
.name("")
.value(false)
.build();
let actual = checkbox.input(key_code.into());
assert_eq!(actual, expected);
};
test(KeyCode::Char('a'), InputResult::Updated);
test(KeyCode::Char('b'), InputResult::Updated);
test(KeyCode::Char('1'), InputResult::Updated);
test(KeyCode::Enter, InputResult::Updated);
test(KeyCode::Esc, InputResult::Updated);
test(KeyCode::Up, InputResult::Ignored);
test(KeyCode::Down, InputResult::Ignored);
}
}