#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextInput {
pub value: String,
pub cursor: usize,
pub placeholder: Option<String>,
}
impl TextInput {
#[must_use]
pub const fn new() -> Self {
Self {
value: String::new(),
cursor: 0,
placeholder: None,
}
}
#[must_use]
pub fn with_value(value: impl Into<String>) -> Self {
let value = value.into();
let cursor = value.len();
Self {
value,
cursor,
placeholder: None,
}
}
#[must_use]
pub fn placeholder(mut self, p: impl Into<String>) -> Self {
self.placeholder = Some(p.into());
self
}
#[must_use]
pub const fn len(&self) -> usize {
self.value.len()
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.value.is_empty()
}
pub fn insert(&mut self, c: &str) {
if !self.value.is_char_boundary(self.cursor) {
return;
}
self.value.insert_str(self.cursor, c);
self.cursor += c.len();
}
pub fn backspace(&mut self) {
if self.cursor == 0 {
return;
}
let prev = self.value[..self.cursor]
.char_indices()
.next_back()
.map_or(0, |(i, _)| i);
self.value.replace_range(prev..self.cursor, "");
self.cursor = prev;
}
pub fn delete(&mut self) {
if self.cursor >= self.value.len() {
return;
}
if !self.value.is_char_boundary(self.cursor) {
return;
}
let next = self.value[self.cursor..]
.chars()
.next()
.map_or(self.value.len(), |c| self.cursor + c.len_utf8());
self.value.replace_range(self.cursor..next, "");
}
pub fn move_left(&mut self) {
if self.cursor == 0 {
return;
}
let prev = self.value[..self.cursor]
.char_indices()
.last()
.map_or(0, |(i, _)| i);
self.cursor = prev;
}
pub fn move_right(&mut self) {
if self.cursor >= self.value.len() {
return;
}
if !self.value.is_char_boundary(self.cursor) {
return;
}
let next = self.value[self.cursor..]
.chars()
.next()
.map_or(self.value.len(), |c| self.cursor + c.len_utf8());
self.cursor = next;
}
pub const fn move_home(&mut self) {
self.cursor = 0;
}
pub const fn move_end(&mut self) {
self.cursor = self.value.len();
}
}
impl Default for TextInput {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Checkbox {
pub checked: bool,
pub label: String,
}
impl Checkbox {
#[must_use]
pub fn new(label: impl Into<String>) -> Self {
Self {
checked: false,
label: label.into(),
}
}
#[must_use]
pub fn with_checked(label: impl Into<String>, checked: bool) -> Self {
Self {
checked,
label: label.into(),
}
}
pub const fn set(&mut self, checked: bool) {
self.checked = checked;
}
pub const fn toggle(&mut self) {
self.checked = !self.checked;
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RadioGroup {
pub options: Vec<String>,
pub selected: usize,
}
impl RadioGroup {
#[must_use]
pub fn new<I, S>(options: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
let options: Vec<String> = options.into_iter().map(Into::into).collect();
Self {
options,
selected: 0,
}
}
#[must_use]
pub const fn len(&self) -> usize {
self.options.len()
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.options.is_empty()
}
#[must_use]
pub const fn selected(&self) -> usize {
self.selected
}
#[must_use]
pub fn selected_label(&self) -> Option<&str> {
self.options.get(self.selected).map(String::as_str)
}
pub const fn prev(&mut self) {
if self.options.is_empty() {
return;
}
self.selected = if self.selected == 0 {
self.options.len() - 1
} else {
self.selected - 1
};
}
pub const fn next(&mut self) {
if self.options.is_empty() {
return;
}
self.selected = (self.selected + 1) % self.options.len();
}
pub fn select(&mut self, idx: usize) {
if self.options.is_empty() {
return;
}
self.selected = idx.min(self.options.len() - 1);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn textinput_insert_backspace() {
let mut ti = TextInput::new();
ti.insert("h");
ti.insert("i");
assert_eq!(ti.value, "hi");
assert_eq!(ti.cursor, 2);
ti.backspace();
assert_eq!(ti.value, "h");
assert_eq!(ti.cursor, 1);
}
#[test]
fn textinput_move_end_moves_to_len() {
let mut ti = TextInput::with_value("hi");
ti.cursor = 0;
ti.move_end();
assert_eq!(ti.cursor, 2);
}
#[test]
fn textinput_delete() {
let mut ti = TextInput::with_value("hi");
ti.cursor = 0;
ti.delete();
assert_eq!(ti.value, "i");
}
#[test]
fn checkbox_toggle() {
let mut c = Checkbox::new("foo");
assert!(!c.checked);
c.toggle();
assert!(c.checked);
c.toggle();
assert!(!c.checked);
}
#[test]
fn radiogroup_next_wraps() {
let mut rg = RadioGroup::new(["a", "b", "c"]);
rg.select(2);
rg.next();
assert_eq!(rg.selected(), 0);
}
#[test]
fn radiogroup_prev_wraps() {
let mut rg = RadioGroup::new(["a", "b", "c"]);
rg.select(0);
rg.prev();
assert_eq!(rg.selected(), 2);
}
}