term: move output to end; move from_dsl

This commit is contained in:
facile pop culture reference 2026-06-28 04:56:47 +03:00
parent 7613c65500
commit dd7091bda1
3 changed files with 46 additions and 43 deletions

View file

@ -1,40 +0,0 @@
#[cfg(feature = "term")]
impl crate::term::TuiKey {
#[cfg(feature = "lang")]
pub fn from_dsl (dsl: impl Language) -> Usually<Self> {
if let Some(word) = dsl.word()? {
let word = word.trim();
Ok(if word == ":char" {
Self(None, KeyModifiers::NONE)
} else if word.chars().nth(0) == Some('@') {
let mut key = None;
let mut modifiers = KeyModifiers::NONE;
let mut tokens = word[1..].split(Self::SPLIT).peekable();
while let Some(token) = tokens.next() {
if tokens.peek().is_some() {
match token {
"ctrl" | "Ctrl" | "c" | "C" => modifiers |= KeyModifiers::CONTROL,
"alt" | "Alt" | "m" | "M" => modifiers |= KeyModifiers::ALT,
"shift" | "Shift" | "s" | "S" => {
modifiers |= KeyModifiers::SHIFT;
// + TODO normalize character case, BackTab, etc.
},
_ => panic!("unknown modifier {token}"),
}
} else {
key = if token.len() == 1 {
Some(KeyCode::Char(token.chars().next().unwrap()))
} else {
Some(named_key(token).unwrap_or_else(||panic!("unknown character {token}")))
}
}
}
Self(key, modifiers)
} else {
return Err(format!("TuiKey: unexpected: {word}").into())
})
} else {
return Err(format!("TuiKey: unspecified").into())
}
}
}

View file

@ -97,13 +97,18 @@ use ::{
} }
/// Spawn the TUI output thread which writes colored characters to the terminal. /// Spawn the TUI output thread which writes colored characters to the terminal.
///
/// ```
/// let state = Arc::new(RwLock::new(()));
/// Exit::run(|exit|tui_output(exit, state, sleep, stdout()))
/// ```
pub fn tui_output < pub fn tui_output <
W: Write + Send + Sync + 'static, T: View<Tui> + Send + Sync + 'static W: Write + Send + Sync + 'static, T: View<Tui> + Send + Sync + 'static
> ( > (
output: W,
exited: &Arc<AtomicBool>, exited: &Arc<AtomicBool>,
state: &Arc<RwLock<T>>, state: &Arc<RwLock<T>>,
sleep: Duration sleep: Duration,
output: W,
) -> Usually<Task> { ) -> Usually<Task> {
let state = state.clone(); let state = state.clone();
stdout().execute(EnterAlternateScreen)?; stdout().execute(EnterAlternateScreen)?;

View file

@ -2,7 +2,7 @@ use crate::task::Task;
use ::std::sync::{Arc, RwLock, atomic::{AtomicBool, Ordering::*}}; use ::std::sync::{Arc, RwLock, atomic::{AtomicBool, Ordering::*}};
use ::std::time::Duration; use ::std::time::Duration;
use ::dizzle::{Usually, Apply, impl_from}; use ::dizzle::{Language, Symbol, Usually, Apply, impl_from};
use ::crossterm::event::{ use ::crossterm::event::{
read, Event, KeyEvent, KeyCode, KeyModifiers, KeyEventKind, KeyEventState read, Event, KeyEvent, KeyCode, KeyModifiers, KeyEventKind, KeyEventState
}; };
@ -120,4 +120,42 @@ impl TuiKey {
_ => return None, _ => return None,
}) })
} }
#[cfg(feature = "lang")]
pub fn from_dsl (dsl: impl Language) -> Usually<Self> {
if let Some(word) = dsl.word()? {
let word = word.trim();
Ok(if word == ":char" {
Self(None, KeyModifiers::NONE)
} else if word.chars().nth(0) == Some('@') {
let mut key = None;
let mut modifiers = KeyModifiers::NONE;
let mut tokens = word[1..].split(Self::SPLIT).peekable();
while let Some(token) = tokens.next() {
if tokens.peek().is_some() {
match token {
"ctrl" | "Ctrl" | "c" | "C" => modifiers |= KeyModifiers::CONTROL,
"alt" | "Alt" | "m" | "M" => modifiers |= KeyModifiers::ALT,
"shift" | "Shift" | "s" | "S" => {
modifiers |= KeyModifiers::SHIFT;
// + TODO normalize character case, BackTab, etc.
},
_ => panic!("unknown modifier {token}"),
}
} else {
key = Some(if token.len() == 1 {
KeyCode::Char(token.chars().next().unwrap())
} else {
Self::named(token).unwrap_or_else(||panic!("unknown character {token}"))
})
}
}
Self(key, modifiers)
} else {
return Err(format!("TuiKey: unexpected: {word}").into())
})
} else {
return Err(format!("TuiKey: unspecified").into())
}
}
} }