tek/src/prelude.rs

108 lines
2.6 KiB
Rust

pub type Usually<T> = Result<T, Box<dyn Error>>;
pub use crate::draw::*;
pub use crate::device::*;
pub use crate::time::*;
pub use crate::layout::*;
pub use std::error::Error;
pub use std::io::{stdout, Stdout, Write};
pub use std::thread::{spawn, JoinHandle};
pub use std::time::Duration;
pub use std::sync::{
Arc,
Mutex,
atomic::{
Ordering,
AtomicBool,
AtomicUsize
},
mpsc::{self, channel, Sender, Receiver}
};
pub use crossterm::{
ExecutableCommand, QueueableCommand,
event::{Event, KeyEvent, KeyCode, KeyModifiers},
terminal::{
self,
Clear, ClearType,
EnterAlternateScreen, LeaveAlternateScreen,
enable_raw_mode, disable_raw_mode
},
};
pub use ratatui::{
prelude::{
Buffer, Rect, Style, Color, CrosstermBackend, Layout, Stylize, Direction,
Line, Constraint
},
widgets::{Widget, WidgetRef},
//style::Stylize,
};
pub use jack::{
AsyncClient,
AudioIn,
AudioOut,
Client,
ClientOptions,
ClientStatus,
ClosureProcessHandler,
Control,
Frames,
MidiIn,
MidiOut,
NotificationHandler,
Port,
PortId,
ProcessHandler,
ProcessScope,
RawMidi,
Transport,
TransportState,
TransportStatePosition
};
pub type BoxedNotificationHandler = Box<dyn Fn(AppEvent) + Send>;
pub type BoxedProcessHandler = Box<dyn FnMut(&Client, &ProcessScope)-> Control + Send>;
pub type Jack<N> = AsyncClient<N, ClosureProcessHandler<BoxedProcessHandler>>;
pub type KeyHandler<T> = &'static dyn Fn(&mut T)->Usually<bool>;
pub type KeyBinding<T> = (
KeyCode, KeyModifiers, &'static str, &'static str, KeyHandler<T>
);
#[macro_export] macro_rules! key {
($k:ident $(($char:literal))?, $m:ident, $n: literal, $d: literal, $f: expr) => {
(KeyCode::$k $(($char))?, KeyModifiers::$m, $n, $d, &$f)
};
}
#[macro_export] macro_rules! keymap {
($T:ty { $([$k:ident $(($char:literal))?, $m:ident, $n: literal, $d: literal, $f: expr]),* $(,)? }) => {
&[
$((KeyCode::$k $(($char))?, KeyModifiers::$m, $n, $d, &$f as KeyHandler<$T>)),*
] as &'static [KeyBinding<$T>]
}
}
pub use crate::{key, keymap};
pub fn handle_keymap <T> (
state: &mut T, event: &AppEvent, keymap: &[KeyBinding<T>],
) -> Usually<bool> {
match event {
AppEvent::Input(Event::Key(event)) => {
for (code, modifiers, _, _, command) in keymap.iter() {
if *code == event.code && modifiers.bits() == event.modifiers.bits() {
return command(state)
}
}
},
_ => {}
};
Ok(false)
}