cli: no need for separate crate
Some checks failed
/ build (push) Has been cancelled

This commit is contained in:
🪞👃🪞 2025-08-30 04:01:50 +03:00
parent 7f03116cb3
commit e987aa697d
16 changed files with 107 additions and 135 deletions

View file

@ -29,4 +29,8 @@ cli = ["clap"]
host = ["tek_device/lv2"]
[lib]
path = "app.rs"
path = "tek.rs"
[[bin]]
name = "tek"
path = "tek_cli.rs"

View file

@ -1,13 +1,13 @@
#![allow(unused, clippy::unit_arg)]
#![feature(adt_const_params, associated_type_defaults, if_let_guard, impl_trait_in_assoc_type,
type_alias_impl_trait, trait_alias, type_changing_struct_update, closure_lifetime_binder)]
#[cfg(test)] mod app_test;
mod app_bind; pub use self::app_bind::*;
mod app_data; pub use self::app_data::*;
mod app_deps; pub use self::app_deps::*;
mod app_jack; pub use self::app_jack::*;
mod app_menu; pub use self::app_menu::*;
mod app_view; pub use self::app_view::*;
#[cfg(test)] mod tek_test;
mod tek_bind; pub use self::tek_bind::*;
mod tek_data; pub use self::tek_data::*;
mod tek_deps; pub use self::tek_deps::*;
mod tek_jack; pub use self::tek_jack::*;
mod tek_menu; pub use self::tek_menu::*;
mod tek_view; pub use self::tek_view::*;
/// Total state
#[derive(Default, Debug)]
pub struct App {

132
crates/app/tek_cli.rs Normal file
View file

@ -0,0 +1,132 @@
pub(crate) use tek::*;
pub(crate) use clap::{self, Parser, Subcommand};
/// Application entrypoint.
pub fn main () -> Usually<()> {
Cli::parse().run()
}
#[derive(Debug, Parser)]
#[command(name = "tek", version, about = Some(HEADER), long_about = Some(HEADER))]
pub struct Cli {
/// Pre-defined configuration modes.
///
/// TODO: Replace these with scripted configurations.
#[command(subcommand)] mode: Option<LaunchMode>,
/// Name of JACK client
#[arg(short='n', long)] name: Option<String>,
/// Whether to attempt to become transport master
#[arg(short='S', long, default_value_t = false)] sync_lead: bool,
/// Whether to sync to external transport master
#[arg(short='s', long, default_value_t = true)] sync_follow: bool,
/// Initial tempo in beats per minute
#[arg(short='b', long, default_value = None)] bpm: Option<f64>,
/// Whether to include a transport toolbar (default: true)
#[arg(short='t', long, default_value_t = true)] show_clock: bool,
/// MIDI outs to connect to (multiple instances accepted)
#[arg(short='I', long)] midi_from: Vec<String>,
/// MIDI outs to connect to (multiple instances accepted)
#[arg(short='i', long)] midi_from_re: Vec<String>,
/// MIDI ins to connect to (multiple instances accepted)
#[arg(short='O', long)] midi_to: Vec<String>,
/// MIDI ins to connect to (multiple instances accepted)
#[arg(short='o', long)] midi_to_re: Vec<String>,
/// Audio outs to connect to left input
#[arg(short='l', long)] left_from: Vec<String>,
/// Audio outs to connect to right input
#[arg(short='r', long)] right_from: Vec<String>,
/// Audio ins to connect from left output
#[arg(short='L', long)] left_to: Vec<String>,
/// Audio ins to connect from right output
#[arg(short='R', long)] right_to: Vec<String>,
}
/// Application modes
#[derive(Debug, Clone, Subcommand)]
pub enum LaunchMode {
/// Create a new session instead of loading the previous one.
New,
}
impl Cli {
pub fn run (&self) -> Usually<()> {
let name = self.name.as_ref().map_or("tek", |x|x.as_str());
let tracks = vec![];
let scenes = vec![];
let empty = &[] as &[&str];
let left_froms = Connect::collect(&self.left_from, empty, empty);
let left_tos = Connect::collect(&self.left_to, empty, empty);
let right_froms = Connect::collect(&self.right_from, empty, empty);
let right_tos = Connect::collect(&self.right_to, empty, empty);
let _audio_froms = &[left_froms.as_slice(), right_froms.as_slice()];
let _audio_tos = &[left_tos.as_slice(), right_tos.as_slice()];
let mut config = Config::new(None);
config.init()?;
Tui::new()?.run(&Jack::new_run(&name, move|jack|{
let app = App {
jack: jack.clone(),
color: ItemTheme::random(),
dialog: Dialog::welcome(),
mode: config.modes.clone().read().unwrap().get(":menu").cloned().unwrap(),
config,
project: Arrangement {
name: Default::default(),
color: ItemTheme::random(),
jack: jack.clone(),
clock: Clock::new(&jack, self.bpm)?,
tracks,
scenes,
selection: Selection::TrackClip { track: 0, scene: 0 },
midi_ins: {
let mut midi_ins = vec![];
for (index, connect) in self.midi_froms().iter().enumerate() {
midi_ins.push(jack.midi_in(&format!("M/{index}"), &[connect.clone()])?);
}
midi_ins
},
midi_outs: {
let mut midi_outs = vec![];
for (index, connect) in self.midi_tos().iter().enumerate() {
midi_outs.push(jack.midi_out(&format!("{index}/M"), &[connect.clone()])?);
};
midi_outs
},
..Default::default()
},
..Default::default()
};
jack.sync_lead(self.sync_lead, |mut state|{
let clock = app.clock();
clock.playhead.update_from_sample(state.position.frame() as f64);
state.position.bbt = Some(clock.bbt());
state.position
})?;
jack.sync_follow(self.sync_follow)?;
Ok(app)
})?)
}
fn midi_froms (&self) -> Vec<Connect> {
Connect::collect(&self.midi_from, &[] as &[&str], &self.midi_from_re)
}
fn midi_tos (&self) -> Vec<Connect> {
Connect::collect(&self.midi_to, &[] as &[&str], &self.midi_to_re)
}
}
/// CLI header
const HEADER: &'static str = r#"
~ ~~~~ ~ ~ ~~ ~ ~ ~ ~~ ~ ~ ~ ~ ~~~~~~ ~ ~~~
~~ ~ ~< ~ v0.3.0, 2025 sum(m)er @ the nose of the cat. ~
~~~ ~ ~ ~~~ ~ ~ ~ ~ ~~~ ~~~ ~ ~~ ~~ ~~ ~ ~~
On first run, Tek will create configuration and state dirs:
* [x] ~/.config/tek - config
* [ ] ~/.local/share/tek - projects
* [ ] ~/.local/lib/tek - plugins
* [ ] ~/.cache/tek - cache
~"#;
#[cfg(test)] #[test] fn test_cli () {
use clap::CommandFactory;
Cli::command().debug_assert();
//let jack = Jack::default();
}

View file

@ -5,7 +5,7 @@ content!(TuiOut:|self: App|Fill::xy(Stack::above(|add|{
})));
impl App {
fn view <D: Dsl> (&self, index: D) -> Box<dyn Render<TuiOut>> {
fn view <'t, D: Dsl> (&'t self, index: D) -> TuiBox<'t> {
match index.src() {
Ok(Some(src)) => render_dsl(self, src),
Ok(None) => Box::new(Tui::fg(Color::Rgb(192, 192, 192), "empty view")),
@ -17,10 +17,12 @@ impl App {
}
}
type TuiBox<'t> = Box<dyn Render<TuiOut> + 't>;
fn render_dsl <'t> (
state: &'t impl DslNs<'t, Box<dyn Render<TuiOut>>>,
state: &'t impl DslNs<'t, TuiBox<'t>>,
src: &str
) -> Box<dyn Render<TuiOut>> {
) -> TuiBox<'t> {
let err: Option<Box<dyn Error>> = match state.from(&src) {
Ok(Some(value)) => return value, Ok(None) => None, Err(e) => Some(e),
};
@ -41,51 +43,51 @@ fn render_dsl <'t> (
})
}
impl<'t> DslNs<'t, Box<dyn Render<TuiOut>>> for App {
dsl_exprs!(|app| -> Box<dyn Render<TuiOut>> {
impl<'t> DslNs<'t, TuiBox<'t>> for App {
dsl_exprs!(|app| -> TuiBox<'t> {
"text" (tail: Arc<str>) => Box::new(tail),
"fg" (color: Color, x: Box<dyn Render<TuiOut>>) => Box::new(Tui::fg(color, x)),
"bg" (color: Color, x: Box<dyn Render<TuiOut>>) => Box::new(Tui::bg(color, x)),
"fg/bg" (fg: Color, bg: Color, x: Box<dyn Render<TuiOut>>) => Box::new(Tui::fg_bg(fg, bg, x)),
"fg" (color: Color, x: TuiBox<'t>) => Box::new(Tui::fg(color, x)),
"bg" (color: Color, x: TuiBox<'t>) => Box::new(Tui::bg(color, x)),
"fg/bg" (fg: Color, bg: Color, x: TuiBox<'t>) => Box::new(Tui::fg_bg(fg, bg, x)),
"either" (cond: bool, a: Box<dyn Render<TuiOut>>, b: Box<dyn Render<TuiOut>>) =>
"either" (cond: bool, a: TuiBox<'t>, b: TuiBox<'t>) =>
Box::new(Either(cond, a, b)),
"bsp/n" (a: Box<dyn Render<TuiOut>>, b: Box<dyn Render<TuiOut>>) => Box::new(Bsp::n(a, b)),
"bsp/s" (a: Box<dyn Render<TuiOut>>, b: Box<dyn Render<TuiOut>>) => Box::new(Bsp::s(a, b)),
"bsp/e" (a: Box<dyn Render<TuiOut>>, b: Box<dyn Render<TuiOut>>) => Box::new(Bsp::e(a, b)),
"bsp/w" (a: Box<dyn Render<TuiOut>>, b: Box<dyn Render<TuiOut>>) => Box::new(Bsp::w(a, b)),
"bsp/a" (a: Box<dyn Render<TuiOut>>, b: Box<dyn Render<TuiOut>>) => Box::new(Bsp::a(a, b)),
"bsp/b" (a: Box<dyn Render<TuiOut>>, b: Box<dyn Render<TuiOut>>) => Box::new(Bsp::b(a, b)),
"bsp/n" (a: TuiBox<'t>, b: TuiBox<'t>) => Box::new(Bsp::n(a, b)),
"bsp/s" (a: TuiBox<'t>, b: TuiBox<'t>) => Box::new(Bsp::s(a, b)),
"bsp/e" (a: TuiBox<'t>, b: TuiBox<'t>) => Box::new(Bsp::e(a, b)),
"bsp/w" (a: TuiBox<'t>, b: TuiBox<'t>) => Box::new(Bsp::w(a, b)),
"bsp/a" (a: TuiBox<'t>, b: TuiBox<'t>) => Box::new(Bsp::a(a, b)),
"bsp/b" (a: TuiBox<'t>, b: TuiBox<'t>) => Box::new(Bsp::b(a, b)),
"align/nw" (x: Box<dyn Render<TuiOut>>) => Box::new(Align::nw(x)),
"align/ne" (x: Box<dyn Render<TuiOut>>) => Box::new(Align::ne(x)),
"align/n" (x: Box<dyn Render<TuiOut>>) => Box::new(Align::n(x)),
"align/s" (x: Box<dyn Render<TuiOut>>) => Box::new(Align::s(x)),
"align/e" (x: Box<dyn Render<TuiOut>>) => Box::new(Align::e(x)),
"align/w" (x: Box<dyn Render<TuiOut>>) => Box::new(Align::w(x)),
"align/x" (x: Box<dyn Render<TuiOut>>) => Box::new(Align::x(x)),
"align/y" (x: Box<dyn Render<TuiOut>>) => Box::new(Align::y(x)),
"align/c" (x: Box<dyn Render<TuiOut>>) => Box::new(Align::c(x)),
"align/nw" (x: TuiBox<'t>) => Box::new(Align::nw(x)),
"align/ne" (x: TuiBox<'t>) => Box::new(Align::ne(x)),
"align/n" (x: TuiBox<'t>) => Box::new(Align::n(x)),
"align/s" (x: TuiBox<'t>) => Box::new(Align::s(x)),
"align/e" (x: TuiBox<'t>) => Box::new(Align::e(x)),
"align/w" (x: TuiBox<'t>) => Box::new(Align::w(x)),
"align/x" (x: TuiBox<'t>) => Box::new(Align::x(x)),
"align/y" (x: TuiBox<'t>) => Box::new(Align::y(x)),
"align/c" (x: TuiBox<'t>) => Box::new(Align::c(x)),
"fill/x" (x: Box<dyn Render<TuiOut>>) => Box::new(Fill::x(x)),
"fill/y" (x: Box<dyn Render<TuiOut>>) => Box::new(Fill::y(x)),
"fill/xy" (x: Box<dyn Render<TuiOut>>) => Box::new(Fill::xy(x)),
"fill/x" (x: TuiBox<'t>) => Box::new(Fill::x(x)),
"fill/y" (x: TuiBox<'t>) => Box::new(Fill::y(x)),
"fill/xy" (x: TuiBox<'t>) => Box::new(Fill::xy(x)),
"fixed/x" (x: u16, c: Box<dyn Render<TuiOut>>) => Box::new(Fixed::x(x, c)),
"fixed/y" (y: u16, c: Box<dyn Render<TuiOut>>) => Box::new(Fixed::y(y, c)),
"fixed/xy" (x: u16, y: u16, c: Box<dyn Render<TuiOut>>) => Box::new(Fixed::xy(x, y, c)),
"fixed/x" (x: u16, c: TuiBox<'t>) => Box::new(Fixed::x(x, c)),
"fixed/y" (y: u16, c: TuiBox<'t>) => Box::new(Fixed::y(y, c)),
"fixed/xy" (x: u16, y: u16, c: TuiBox<'t>) => Box::new(Fixed::xy(x, y, c)),
"min/x" (x: u16, c: Box<dyn Render<TuiOut>>) => Box::new(Min::x(x, c)),
"min/y" (y: u16, c: Box<dyn Render<TuiOut>>) => Box::new(Min::y(y, c)),
"min/xy" (x: u16, y: u16, c: Box<dyn Render<TuiOut>>) => Box::new(Min::xy(x, y, c)),
"min/x" (x: u16, c: TuiBox<'t>) => Box::new(Min::x(x, c)),
"min/y" (y: u16, c: TuiBox<'t>) => Box::new(Min::y(y, c)),
"min/xy" (x: u16, y: u16, c: TuiBox<'t>) => Box::new(Min::xy(x, y, c)),
"max/x" (x: u16, c: Box<dyn Render<TuiOut>>) => Box::new(Max::x(x, c)),
"max/y" (y: u16, c: Box<dyn Render<TuiOut>>) => Box::new(Max::y(y, c)),
"max/xy" (x: u16, y: u16, c: Box<dyn Render<TuiOut>>) => Box::new(Max::xy(x, y, c)),
"max/x" (x: u16, c: TuiBox<'t>) => Box::new(Max::x(x, c)),
"max/y" (y: u16, c: TuiBox<'t>) => Box::new(Max::y(y, c)),
"max/xy" (x: u16, y: u16, c: TuiBox<'t>) => Box::new(Max::xy(x, y, c)),
});
dsl_words!(|app| -> Box<dyn Render<TuiOut>> {
dsl_words!(|app| -> TuiBox<'t> {
":logo" => Box::new(Fixed::xy(32, 7, Tui::bold(true, Tui::fg(Rgb(240,200,180), Stack::south(|add|{
add(&Fixed::y(1, ""));
add(&Fixed::y(1, ""));
@ -93,16 +95,16 @@ impl<'t> DslNs<'t, Box<dyn Render<TuiOut>>> for App {
add(&Fixed::y(1, Bsp::e("~~~~ ║ ~ ╟─╌ ~╟─< ~~ ", Bsp::e(Tui::fg(Rgb(230,100,40), "v0.3.0"), " ~~"))));
add(&Fixed::y(1, "~~~~ ╨ ~ ╙──╜ ╨ ╜ ~~~~~~~~~~~~"));
}))))),
":meters/input" => Box::new("Input Meters"),
":meters/output" => Box::new("Output Meters"),
":meters/input" => Box::new(Tui::bg(Rgb(30, 30, 30), Fill::y(Align::s("Input Meters")))),
":meters/output" => Box::new(Tui::bg(Rgb(30, 30, 30), Fill::y(Align::s("Output Meters")))),
":status" => Box::new("Status Bar"),
":tracks/names" => Box::new("Track Names"),
":tracks/inputs" => Box::new("Track Inputs"),
":tracks/devices" => Box::new("Track Devices"),
":tracks/outputs" => Box::new("Track Outputs"),
":tracks/names" => Box::new(app.project.view_track_names(app.color.clone())),//Tui::bg(Rgb(40, 40, 40), Fill::x(Align::w("Track Names")))),
":tracks/inputs" => Box::new(Tui::bg(Rgb(40, 40, 40), Fill::x(Align::w("Track Inputs")))),
":tracks/devices" => Box::new(Tui::bg(Rgb(40, 40, 40), Fill::x(Align::w("Track Devices")))),
":tracks/outputs" => Box::new(Tui::bg(Rgb(40, 40, 40), Fill::x(Align::w("Track Outputs")))),
":scenes/names" => Box::new("Scene Names"),
":editor" => Box::new("Editor"),
":scenes" => Box::new("Editor"),
":scenes" => Box::new("Scenes"),
":dialog/menu" => Box::new(if let Dialog::Menu(selected, items) = &app.dialog {
let items = items.clone();
let selected = *selected;
@ -165,7 +167,7 @@ impl<'t> DslNs<'t, Box<dyn Render<TuiOut>>> for App {
Fill::x(Tui::bg(bg, Bsp::e(lb, Bsp::w(rb, "FIXME device name")))) }))) },
});
/// Resolve an expression if known.
fn from_expr <D: Dsl> (&self, dsl: &D) -> Perhaps<Box<dyn Render<TuiOut>>> {
fn from_expr <D: Dsl> (&self, dsl: &D) -> Perhaps<TuiBox<'t>> {
if let Some(head) = dsl.expr().head()? {
for (key, value) in Self::EXPRS.0.iter() {
if head == *key {
@ -176,7 +178,7 @@ impl<'t> DslNs<'t, Box<dyn Render<TuiOut>>> for App {
return Ok(None)
}
/// Resolve a symbol if known.
fn from_word <D: Dsl> (&self, dsl: &D) -> Perhaps<Box<dyn Render<TuiOut>>> {
fn from_word <D: Dsl> (&self, dsl: &D) -> Perhaps<TuiBox<'t>> {
if let Some(dsl) = dsl.word()? {
let views = self.config.views.read().unwrap();
if let Some(view) = views.get(dsl) {
@ -194,7 +196,7 @@ impl<'t> DslNs<'t, Box<dyn Render<TuiOut>>> for App {
}
}
pub fn view_nil (_: &App) -> Box<dyn Render<TuiOut>> {
pub fn view_nil (_: &App) -> TuiBox {
Box::new(Fill::xy("·"))
}