mirror of
https://codeberg.org/unspeaker/tek.git
synced 2026-10-08 18:56:42 +02:00
This commit is contained in:
parent
7fcab73b04
commit
62f30daac6
6 changed files with 295 additions and 195 deletions
52
src/tek.rs
52
src/tek.rs
|
|
@ -877,58 +877,14 @@ pub use self::bind::*;
|
|||
mod bind {
|
||||
use crate::*;
|
||||
|
||||
/// Define an enum containing commands, and implement [Command] trait for over given `State`.
|
||||
#[macro_export] macro_rules! def_command (
|
||||
($Command:ident: |$state:ident: $State:ty| {
|
||||
// FIXME: support attrs (docstrings)
|
||||
$($Variant:ident$({$($arg:ident:$Arg:ty),+ $(,)?})?=>$body:expr),* $(,)?
|
||||
})=>{
|
||||
#[derive(Debug)] pub enum $Command {
|
||||
// FIXME: support attrs (docstrings)
|
||||
$($Variant $({ $($arg: $Arg),* })?),*
|
||||
}
|
||||
impl ::tengri::dizzle::Act<$State> for $Command {
|
||||
fn act (&self, $state: &mut $State) -> Perhaps<Self> {
|
||||
match self {
|
||||
$(Self::$Variant $({ $($arg),* })? => $body,)*
|
||||
_ => unimplemented!("Act<{}>: {self:?}", stringify!($State)),
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
pub fn swap_value <T: Clone + PartialEq, U> (
|
||||
target: &mut T, value: &T, returned: impl Fn(T)->U
|
||||
) -> Perhaps<U> {
|
||||
if *target == *value {
|
||||
Ok(None)
|
||||
} else {
|
||||
let mut value = value.clone();
|
||||
std::mem::swap(target, &mut value);
|
||||
Ok(Some(returned(value)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn toggle_bool <U> (
|
||||
target: &mut bool, value: &Option<bool>, returned: impl Fn(Option<bool>)->U
|
||||
) -> Perhaps<U> {
|
||||
let mut value = value.unwrap_or(!*target);
|
||||
if value == *target {
|
||||
Ok(None)
|
||||
} else {
|
||||
std::mem::swap(target, &mut value);
|
||||
Ok(Some(returned(Some(value))))
|
||||
}
|
||||
}
|
||||
|
||||
tui_keys!(self: App, input {
|
||||
let commands = tek_commands_collect(self, input)?;
|
||||
let results = tek_commands_execute(self, commands)?;
|
||||
let commands = collect_commands(self, input)?;
|
||||
let results = execute_commands(self, commands)?;
|
||||
self.history.extend(results.into_iter());
|
||||
Ok(())
|
||||
});
|
||||
|
||||
fn tek_commands_collect (app: &App, input: &TuiEvent)
|
||||
fn collect_commands (app: &App, input: &TuiEvent)
|
||||
-> Usually<Vec<AppCommand>>
|
||||
{
|
||||
let mut commands = vec![];
|
||||
|
|
@ -953,7 +909,7 @@ mod bind {
|
|||
Ok(commands)
|
||||
}
|
||||
|
||||
fn tek_commands_execute (app: &mut App, commands: Vec<AppCommand>)
|
||||
fn execute_commands (app: &mut App, commands: Vec<AppCommand>)
|
||||
-> Usually<Vec<(AppCommand, Option<AppCommand>)>>
|
||||
{
|
||||
let mut history = vec![];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue