use super::*;
pub(super) struct Scripted {
keys: std::collections::VecDeque<crate::app::Key>,
pub(super) answered: Vec<String>,
}
thread_local! {
pub(super) static SCRIPT: std::cell::RefCell<Option<Scripted>> = const { std::cell::RefCell::new(None) };
}
pub(crate) fn scripted() -> bool {
SCRIPT.with(|s| s.borrow().is_some())
}
pub(crate) fn next_scripted() -> Option<crate::app::Key> {
SCRIPT.with(|s| s.borrow_mut().as_mut().and_then(|s| s.keys.pop_front()))
}
impl From<crate::app::Key> for Key {
fn from(key: crate::app::Key) -> Key {
use crate::app::{Key as K, NamedKey};
match key {
K::Char(' ') => Key::Space,
K::Char(c) => Key::Char(c),
K::Ctrl('c') => Key::Interrupt,
K::Ctrl(c) => Key::Ctrl(c),
K::Enter => Key::Enter,
K::Tab => Key::Tab,
K::Backspace => Key::Backspace,
K::Delete => Key::Delete,
K::Esc => Key::Esc,
K::Up => Key::Up,
K::Down => Key::Down,
K::Left => Key::Left,
K::Right => Key::Right,
K::Home => Key::Home,
K::End => Key::End,
K::PageUp => Key::PageUp,
K::PageDown => Key::PageDown,
K::Alt('b') | K::Mod(_, NamedKey::Left) => Key::WordLeft,
K::Alt('f') | K::Mod(_, NamedKey::Right) => Key::WordRight,
K::Mod(_, NamedKey::Backspace) => Key::Ctrl('w'),
K::BackTab | K::F(_) | K::Alt(_) | K::Mod(..) => Key::Other,
}
}
}
pub fn script(keys: impl IntoIterator<Item = crate::app::Key>) -> Script {
let keys = keys.into_iter().collect();
SCRIPT.with(|s| {
*s.borrow_mut() = Some(Scripted {
keys,
answered: Vec::new(),
})
});
Script {
_thread: std::marker::PhantomData,
}
}
pub fn typed(text: &str) -> Vec<crate::app::Key> {
text.chars().map(crate::app::Key::Char).collect()
}
#[derive(Debug)]
#[must_use = "the script stops when this is dropped"]
pub struct Script {
_thread: std::marker::PhantomData<*const ()>,
}
impl Script {
pub fn answered(&self) -> Vec<String> {
SCRIPT.with(|s| s.borrow().as_ref().map(|s| s.answered.clone()).unwrap_or_default())
}
pub fn remaining(&self) -> usize {
SCRIPT.with(|s| s.borrow().as_ref().map_or(0, |s| s.keys.len()))
}
}
impl Drop for Script {
fn drop(&mut self) {
SCRIPT.with(|s| *s.borrow_mut() = None);
}
}