mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
This commit is contained in:
parent
b52c1f5828
commit
24ac52d807
10 changed files with 238 additions and 203 deletions
|
|
@ -2,6 +2,7 @@
|
|||
mod tui_engine; pub use self::tui_engine::*;
|
||||
mod tui_content; pub use self::tui_content::*;
|
||||
pub(crate) use ::tengri_core::*;
|
||||
#[cfg(feature = "dsl")] pub use ::tengri_dsl::*;
|
||||
pub use ::tengri_input as input; pub(crate) use ::tengri_input::*;
|
||||
pub use ::tengri_output as output; pub(crate) use ::tengri_output::*;
|
||||
pub(crate) use atomic_float::AtomicF64;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ use std::time::Duration;
|
|||
|
||||
mod tui_buffer; pub use self::tui_buffer::*;
|
||||
mod tui_input; pub use self::tui_input::*;
|
||||
mod tui_event; pub use self::tui_event::*;
|
||||
mod tui_output; pub use self::tui_output::*;
|
||||
mod tui_perf; pub use self::tui_perf::*;
|
||||
|
||||
|
|
|
|||
147
tui/src/tui_engine/tui_event.rs
Normal file
147
tui/src/tui_engine/tui_event.rs
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
use crate::*;
|
||||
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd)] pub struct TuiEvent(Event);
|
||||
impl Ord for TuiEvent {
|
||||
fn cmp (&self, other: &Self) -> std::cmp::Ordering {
|
||||
self.partial_cmp(other)
|
||||
.unwrap_or_else(||format!("{:?}", self).cmp(&format!("{other:?}"))) // FIXME perf
|
||||
}
|
||||
}
|
||||
impl TuiEvent {
|
||||
pub fn from_crossterm (event: Event) -> Self {
|
||||
Self(event)
|
||||
}
|
||||
pub fn from_dsl (dsl: impl Dsl) -> Perhaps<Self> {
|
||||
Ok(TuiKey::from_dsl(dsl)?.to_crossterm().map(Self))
|
||||
}
|
||||
}
|
||||
pub struct TuiKey(Option<KeyCode>, KeyModifiers);
|
||||
impl TuiKey {
|
||||
const SPLIT: char = '/';
|
||||
pub fn from_dsl (dsl: impl Dsl) -> Usually<Self> {
|
||||
if let Some(symbol) = dsl.sym()? {
|
||||
let symbol = symbol.trim();
|
||||
Ok(if symbol == ":char" {
|
||||
Self(None, KeyModifiers::NONE)
|
||||
} else if symbol.chars().nth(0) == Some('@') {
|
||||
let mut key = None;
|
||||
let mut modifiers = KeyModifiers::NONE;
|
||||
let mut tokens = symbol[1..].split(Self::SPLIT).peekable();
|
||||
while let Some(token) = tokens.next() {
|
||||
println!("{token}");
|
||||
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: {symbol}").into())
|
||||
})
|
||||
} else {
|
||||
return Err(format!("TuiKey: unspecified").into())
|
||||
}
|
||||
}
|
||||
pub fn to_crossterm (&self) -> Option<Event> {
|
||||
self.0.map(|code|Event::Key(KeyEvent {
|
||||
code,
|
||||
modifiers: self.1,
|
||||
kind: KeyEventKind::Press,
|
||||
state: KeyEventState::NONE,
|
||||
}))
|
||||
}
|
||||
}
|
||||
pub fn named_key (token: &str) -> Option<KeyCode> {
|
||||
use KeyCode::*;
|
||||
Some(match token {
|
||||
"up" => Up,
|
||||
"down" => Down,
|
||||
"left" => Left,
|
||||
"right" => Right,
|
||||
"esc" | "escape" => Esc,
|
||||
"enter" | "return" => Enter,
|
||||
"delete" | "del" => Delete,
|
||||
"backspace" => Backspace,
|
||||
"tab" => Tab,
|
||||
"space" => Char(' '),
|
||||
"comma" => Char(','),
|
||||
"period" => Char('.'),
|
||||
"plus" => Char('+'),
|
||||
"minus" | "dash" => Char('-'),
|
||||
"equal" | "equals" => Char('='),
|
||||
"underscore" => Char('_'),
|
||||
"backtick" => Char('`'),
|
||||
"lt" => Char('<'),
|
||||
"gt" => Char('>'),
|
||||
"cbopen" | "openbrace" => Char('{'),
|
||||
"cbclose" | "closebrace" => Char('}'),
|
||||
"bropen" | "openbracket" => Char('['),
|
||||
"brclose" | "closebracket" => Char(']'),
|
||||
"pgup" | "pageup" => PageUp,
|
||||
"pgdn" | "pagedown" => PageDown,
|
||||
"f1" => F(1),
|
||||
"f2" => F(2),
|
||||
"f3" => F(3),
|
||||
"f4" => F(4),
|
||||
"f5" => F(5),
|
||||
"f6" => F(6),
|
||||
"f7" => F(7),
|
||||
"f8" => F(8),
|
||||
"f9" => F(9),
|
||||
"f10" => F(10),
|
||||
"f11" => F(11),
|
||||
"f12" => F(12),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
//let token = token.as_ref();
|
||||
//if token.len() < 2 {
|
||||
//Self { valid: false, key: None, mods: KeyModifiers::NONE }
|
||||
//} else if token.chars().next() != Some('@') {
|
||||
//Self { valid: false, key: None, mods: KeyModifiers::NONE }
|
||||
//} else {
|
||||
//Self { valid: true, key: None, mods: KeyModifiers::NONE }.next(&token[1..])
|
||||
//}
|
||||
//}
|
||||
//pub fn build (self) -> Option<Event> {
|
||||
//if self.valid && self.key.is_some() {
|
||||
//Some(Event::Key(KeyEvent::new(self.key.unwrap(), self.mods)))
|
||||
//} else {
|
||||
//None
|
||||
//}
|
||||
//}
|
||||
//fn next (mut self, token: &str) -> Self {
|
||||
//let mut tokens = token.split('-').peekable();
|
||||
//while let Some(token) = tokens.next() {
|
||||
//if tokens.peek().is_some() {
|
||||
//match token {
|
||||
//"ctrl" | "Ctrl" | "c" | "C" => self.mods |= KeyModifiers::CONTROL,
|
||||
//"alt" | "Alt" | "m" | "M" => self.mods |= KeyModifiers::ALT,
|
||||
//"shift" | "Shift" | "s" | "S" => {
|
||||
//self.mods |= KeyModifiers::SHIFT;
|
||||
//// + TODO normalize character case, BackTab, etc.
|
||||
//},
|
||||
//_ => panic!("unknown modifier {token}"),
|
||||
//}
|
||||
//} else {
|
||||
//self.key = if token.len() == 1 {
|
||||
//Some(KeyCode::Char(token.chars().next().unwrap()))
|
||||
//} else {
|
||||
//Some(Self::named_key(token).unwrap_or_else(||panic!("unknown character {token}")))
|
||||
//}
|
||||
//}
|
||||
//}
|
||||
//self
|
||||
//}
|
||||
|
|
@ -17,23 +17,6 @@ impl Input for TuiIn {
|
|||
fn is_done (&self) -> bool { self.exited.fetch_and(true, Relaxed) }
|
||||
fn done (&self) { self.exited.store(true, Relaxed); }
|
||||
}
|
||||
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd)]
|
||||
pub struct TuiEvent(Event);
|
||||
impl Ord for TuiEvent {
|
||||
fn cmp (&self, other: &Self) -> std::cmp::Ordering {
|
||||
self.partial_cmp(other) .unwrap_or_else(||format!("{:?}", self).cmp(&format!("{other:?}"))) // FIXME perf
|
||||
}
|
||||
}
|
||||
impl From<Event> for TuiEvent {
|
||||
fn from (event: Event) -> Self {
|
||||
Self(event)
|
||||
}
|
||||
}
|
||||
impl From<Arc<str>> for TuiEvent {
|
||||
fn from (x: Arc<str>) -> Self {
|
||||
TuiEvent(TuiKey::new(x.as_ref()).build().unwrap_or_else(||panic!("invalid key: {x}")))
|
||||
}
|
||||
}
|
||||
impl TuiIn {
|
||||
/// Spawn the input thread.
|
||||
pub fn run_input <T: Handle<TuiIn> + Send + Sync + 'static> (
|
||||
|
|
@ -60,7 +43,7 @@ impl TuiIn {
|
|||
},
|
||||
_ => {
|
||||
let exited = exited.clone();
|
||||
let event = event.into();
|
||||
let event = TuiEvent::from_crossterm(event);
|
||||
if let Err(e) = state.write().unwrap().handle(&TuiIn { exited, event }) {
|
||||
panic!("{e}")
|
||||
}
|
||||
|
|
@ -70,105 +53,3 @@ impl TuiIn {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
//#[cfg(feature = "dsl")]
|
||||
//impl DslInput for TuiIn {
|
||||
//fn matches_dsl (&self, token: &str) -> bool {
|
||||
//if let Some(event) = TuiKey::new(token).build() {
|
||||
//&event == self.event()
|
||||
//} else {
|
||||
//false
|
||||
//}
|
||||
//}
|
||||
//}
|
||||
|
||||
pub struct TuiKey {
|
||||
valid: bool,
|
||||
key: Option<KeyCode>,
|
||||
mods: KeyModifiers,
|
||||
}
|
||||
|
||||
impl TuiKey {
|
||||
pub fn new (token: impl AsRef<str>) -> Self {
|
||||
let token = token.as_ref();
|
||||
if token.len() < 2 {
|
||||
Self { valid: false, key: None, mods: KeyModifiers::NONE }
|
||||
} else if token.chars().next() != Some('@') {
|
||||
Self { valid: false, key: None, mods: KeyModifiers::NONE }
|
||||
} else {
|
||||
Self { valid: true, key: None, mods: KeyModifiers::NONE }.next(&token[1..])
|
||||
}
|
||||
}
|
||||
pub fn build (self) -> Option<Event> {
|
||||
if self.valid && self.key.is_some() {
|
||||
Some(Event::Key(KeyEvent::new(self.key.unwrap(), self.mods)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
fn next (mut self, token: &str) -> Self {
|
||||
let mut tokens = token.split('-').peekable();
|
||||
while let Some(token) = tokens.next() {
|
||||
if tokens.peek().is_some() {
|
||||
match token {
|
||||
"ctrl" | "Ctrl" | "c" | "C" => self.mods |= KeyModifiers::CONTROL,
|
||||
"alt" | "Alt" | "m" | "M" => self.mods |= KeyModifiers::ALT,
|
||||
"shift" | "Shift" | "s" | "S" => {
|
||||
self.mods |= KeyModifiers::SHIFT;
|
||||
// + TODO normalize character case, BackTab, etc.
|
||||
},
|
||||
_ => panic!("unknown modifier {token}"),
|
||||
}
|
||||
} else {
|
||||
self.key = if token.len() == 1 {
|
||||
Some(KeyCode::Char(token.chars().next().unwrap()))
|
||||
} else {
|
||||
Some(Self::named_key(token).unwrap_or_else(||panic!("unknown character {token}")))
|
||||
}
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
fn named_key (token: &str) -> Option<KeyCode> {
|
||||
use KeyCode::*;
|
||||
Some(match token {
|
||||
"up" => Up,
|
||||
"down" => Down,
|
||||
"left" => Left,
|
||||
"right" => Right,
|
||||
"esc" | "escape" => Esc,
|
||||
"enter" | "return" => Enter,
|
||||
"delete" | "del" => Delete,
|
||||
"tab" => Tab,
|
||||
"space" => Char(' '),
|
||||
"comma" => Char(','),
|
||||
"period" => Char('.'),
|
||||
"plus" => Char('+'),
|
||||
"minus" | "dash" => Char('-'),
|
||||
"equal" | "equals" => Char('='),
|
||||
"underscore" => Char('_'),
|
||||
"backtick" => Char('`'),
|
||||
"lt" => Char('<'),
|
||||
"gt" => Char('>'),
|
||||
"cbopen" | "openbrace" => Char('{'),
|
||||
"cbclose" | "closebrace" => Char('}'),
|
||||
"bropen" | "openbracket" => Char('['),
|
||||
"brclose" | "closebracket" => Char(']'),
|
||||
"pgup" | "pageup" => PageUp,
|
||||
"pgdn" | "pagedown" => PageDown,
|
||||
"f1" => F(1),
|
||||
"f2" => F(2),
|
||||
"f3" => F(3),
|
||||
"f4" => F(4),
|
||||
"f5" => F(5),
|
||||
"f6" => F(6),
|
||||
"f7" => F(7),
|
||||
"f8" => F(8),
|
||||
"f9" => F(9),
|
||||
"f10" => F(10),
|
||||
"f11" => F(11),
|
||||
"f12" => F(12),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ impl Output for TuiOut {
|
|||
type Area = [Self::Unit;4];
|
||||
#[inline] fn area (&self) -> [u16;4] { self.area }
|
||||
#[inline] fn area_mut (&mut self) -> &mut [u16;4] { &mut self.area }
|
||||
#[inline] fn place <T: Render<Self> + ?Sized> (&mut self, area: [u16;4], content: &T) {
|
||||
#[inline] fn place <'t, T: Render<Self> + ?Sized> (&mut self, area: [u16;4], content: &'t T) {
|
||||
let last = self.area();
|
||||
*self.area_mut() = area;
|
||||
content.render(self);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue