Compare commits

..

2 commits

Author SHA1 Message Date
3dada45ea9 refactor config load
Some checks failed
/ build (push) Has been cancelled
2025-08-10 21:50:33 +03:00
fcfb7a0915 slightly closer to scripted 2025-08-10 21:05:14 +03:00
6 changed files with 121 additions and 120 deletions

View file

@ -109,83 +109,64 @@ impl Config {
return Err(format!("{path}: not found").into()) return Err(format!("{path}: not found").into())
}) })
} }
pub fn load_defs <D: Dsl> (&mut self, dsl: D) -> Usually<()> { pub fn load_defs (&mut self, dsl: impl Dsl) -> Usually<()> {
dsl.each(|item|{ dsl.each(|item|{
println!("{item:?}"); println!("{item:?}");
Ok(match item.exp().head() { match item.exp().head() {
Ok(Some("keys")) if let Some(id) = item.exp().tail().head()? => { Ok(Some("keys")) if let Some(id) = item.exp().tail().head()? =>
self.binds.write().unwrap().insert(id.into(), { self.load_bind(id.into(), item),
let mut map = EventMap::new(); Ok(Some("mode")) if let Some(id) = item.exp().tail().head()? =>
item.exp().tail().tail()?.each(|item|Ok({ self.load_mode(id.into(), item),
if let Ok(Some(sym)) = item.exp().head().sym() {
map.add(TuiEvent::from_dsl(item.exp()?.head()?)?, Binding {
command: item.exp()?.tail()?.unwrap_or_default().into(),
condition: None,
description: None,
source: None
});
} else if item.exp().head() == Ok(Some("see")) {
// TODO
} else {
return Err(format!("load_defs: unexpected: {item:?}").into())
}
}))?;
map
});
},
Ok(Some("mode")) if let Some(id) = item.exp().tail().head()? => {
self.modes.write().unwrap().insert(id.into(), {
let mut mode = Mode::default();
item.exp().tail().tail()?.each(|item|Ok(if let Ok(Some(exp)) = item.exp() {
match exp.head()? {
Some("name") => mode.name.push(
exp.tail()?.map(|x|x.trim()).unwrap_or("").into()
),
Some("info") => mode.info.push(
exp.tail()?.map(|x|x.trim()).unwrap_or("").into()
),
Some("keys") => if let Some(tail) = exp.tail()? {
tail.each(|keys|Ok(mode.keys.push(keys.trim().into())))?;
} else {
return Err(format!("load_view: empty keys: {exp}").into())
},
Some("mode") => if let (Some(name), Some(tail)) = (
exp.tail()?.head()?, exp.tail()?.tail()?,
) {
let mut submode: Mode<Arc<str>> = Default::default();
tail.each(|item|Ok(if let Ok(Some(exp)) = item.exp() {
match exp.head()? {
Some("keys") => if let Some(tail) = exp.tail()? {
tail.each(|keys|Ok(mode.keys.push(keys.trim().into())))?;
} else {
return Err(format!("load_view: empty keys: {exp}").into())
},
_ => {
return Err(format!("load_view: unexpected in mode {name}: {item:?}").into())
}
}
} else if let Ok(Some(sym)) = item.sym() {
// TODO
} else {
return Err(format!("load_view: unexpected in mode {name}: {item:?}").into())
}))?;
mode.modes.insert(name.trim().into(), submode);
} else {
return Err(format!("load_view: empty mode: {exp}").into())
},
Some(_) => mode.view.push(exp.into()),
None => return Err(format!("load_view: empty: {exp}").into())
}
} else if let Ok(Some(sym)) = item.sym() {
mode.view.push(sym.into());
} else {
return Err(format!("load_view: unexpected: {dsl:?}").into())
}))?;
mode.into()
});
},
_ => return Err(format!("load_defs: unexpected: {item:?}").into()) _ => return Err(format!("load_defs: unexpected: {item:?}").into())
}) }
})
}
pub fn load_bind (&mut self, id: Arc<str>, item: impl Dsl) -> Usually<()> {
let mut map = EventMap::new();
item.exp().tail().tail()?.each(|item|Self::load_bind_one(&mut map, item))?;
self.binds.write().unwrap().insert(id, map);
Ok(())
}
fn load_bind_one (map: &mut EventMap<Option<TuiEvent>, Arc<str>>, item: impl Dsl) -> Usually<()> {
if let Ok(Some(sym)) = item.exp().head().sym() {
map.add(TuiEvent::from_dsl(item.exp()?.head()?)?, Binding {
command: item.exp()?.tail()?.unwrap_or_default().into(),
condition: None,
description: None,
source: None
});
} else if item.exp().head() == Ok(Some("see")) {
// TODO
} else {
return Err(format!("load_defs: unexpected: {item:?}").into())
}
Ok(())
}
pub fn load_mode (&mut self, id: Arc<str>, item: impl Dsl) -> Usually<()> {
let mut mode = Mode::default();
item.exp().tail().tail()?.each(|item|Self::load_mode_one(&mut mode, item))?;
self.modes.write().unwrap().insert(id.into(), Arc::new(mode));
Ok(())
}
pub fn load_mode_one (mode: &mut Mode<Arc<str>>, item: impl Dsl) -> Usually<()> {
Ok(if let Ok(Some(key)) = item.exp().head() {
match key {
"name" => mode.name.push(item.exp()?.tail()?.map(|x|x.trim()).unwrap_or("").into()),
"info" => mode.info.push(item.exp()?.tail()?.map(|x|x.trim()).unwrap_or("").into()),
"keys" => mode.keys.push(item.exp()?.tail()?.map(|x|x.trim()).unwrap_or("").into()),
"mode" => if let Some(id) = item.exp()?.tail()?.head()? {
let mut submode = Mode::default();
Self::load_mode_one(&mut submode, item.exp()?.tail()?.tail()?)?;
mode.modes.insert(id.into(), submode);
} else {
return Err(format!("load_mode_one: incomplete: {item:?}").into());
},
_ => mode.view.push(item.exp()?.unwrap().into()),
}
} else if let Ok(Some(sym)) = item.sym() {
mode.view.push(sym.into());
} else {
return Err(format!("load_mode_one: unexpected: {item:?}").into());
}) })
} }
} }

View file

@ -1,6 +1,4 @@
use crate::*; use crate::*;
#[derive(Debug)]
pub enum AppCommand {}
handle!(TuiIn:|self: App, input|{ handle!(TuiIn:|self: App, input|{
panic!("wat: {:?}", self.mode); panic!("wat: {:?}", self.mode);
for keys in self.mode.keys.iter() { for keys in self.mode.keys.iter() {
@ -91,6 +89,9 @@ dsl_sym!(|app: App| -> Option<Arc<RwLock<MidiClip>>> {
None None
} }
}); });
#[derive(Debug)]
pub enum AppCommand {
}
dsl_exp!(|app: App| -> AppCommand { dsl_exp!(|app: App| -> AppCommand {
["stop-all"] => app.project.stop_all(), ["stop-all"] => app.project.stop_all(),
["enqueue", clip?: Option<Arc<RwLock<MidiClip>>>] => todo!(), ["enqueue", clip?: Option<Arc<RwLock<MidiClip>>>] => todo!(),

View file

@ -5,10 +5,10 @@ pub struct DslNs<'t, T: 't>(pub &'t [(&'t str, T)]);
/// Namespace where keys are symbols. /// Namespace where keys are symbols.
pub trait DslSymNs<'t, T: 't>: 't { pub trait DslSymNs<'t, T: 't>: 't {
const NS: DslNs<'t, fn (&'t Self)->T>; const SYMS: DslNs<'t, fn (&'t Self)->T>;
fn from_sym <D: Dsl> (&'t self, dsl: D) -> Usually<T> { fn from_sym <D: Dsl> (&'t self, dsl: D) -> Usually<T> {
if let Some(dsl) = dsl.sym()? { if let Some(dsl) = dsl.sym()? {
for (sym, get) in Self::NS.0 { for (sym, get) in Self::SYMS.0 {
if dsl == *sym { if dsl == *sym {
return Ok(get(self)) return Ok(get(self))
} }
@ -21,19 +21,22 @@ pub trait DslSymNs<'t, T: 't>: 't {
#[macro_export] macro_rules! dsl_sym ( #[macro_export] macro_rules! dsl_sym (
(|$state:ident:$State:ty| -> $type:ty {$($lit:literal => $exp:expr),* $(,)?})=>{ (|$state:ident:$State:ty| -> $type:ty {$($lit:literal => $exp:expr),* $(,)?})=>{
impl<'t> DslSymNs<'t, $type> for $State { impl<'t> DslSymNs<'t, $type> for $State {
const NS: DslNs<'t, fn (&'t $State)->$type> = const SYMS: DslNs<'t, fn (&'t $State)->$type> =
DslNs(&[$(($lit, |$state: &$State|$exp)),*]); } }); DslNs(&[$(($lit, |$state: &$State|$exp)),*]); } });
pub trait DslExpNs<'t, T: 't>: 't { const NS: DslNs<'t, fn (&'t Self, &str)->T>; } pub trait DslExpNs<'t, T: 't>: 't {
const EXPS: DslNs<'t, fn (&'t Self, &str)->T>;
}
#[macro_export] macro_rules! dsl_exp ( #[macro_export] macro_rules! dsl_exp (
(|$state:ident:$State:ty|->$type:ty { $( (|$state:ident:$State:ty|->$type:ty { $(
[$key:literal $(/ $sub:ident: $Sub:ty)? $(, $arg:ident $(?)? :$argtype:ty)*] => $body:expr [$key:literal $(/ $sub:ident: $Sub:ty)? $(, $arg:ident $(?)? :$argtype:ty)*] => $body:expr
),* $(,)? }) => { ),* $(,)? }) => {
impl<'t> DslExpNs<'t, $type> for $State { impl<'t> DslExpNs<'t, $type> for $State {
const NS: DslNs<'t, fn (&'t $State, &str)->$type> = const EXPS: DslNs<'t, fn (&'t $State, &str)->$type> =
DslNs(&[]); } }); DslNs(&[]); } });
pub type DslCb = fn (&App) -> Box<dyn Render<TuiOut>>; pub type DslCb = fn (&App) -> Box<dyn Render<TuiOut>>;
impl<'t, D: Dsl> std::ops::Index<D> for DslNs<'t, DslCb> { impl<'t, D: Dsl> std::ops::Index<D> for DslNs<'t, DslCb> {
type Output = DslCb; type Output = DslCb;
fn index (&self, index: D) -> &Self::Output { fn index (&self, index: D) -> &Self::Output {
@ -47,6 +50,7 @@ impl<'t, D: Dsl> std::ops::Index<D> for DslNs<'t, DslCb> {
&(view_nil as DslCb) &(view_nil as DslCb)
} }
} }
fn view_nil (_: &App) -> Box<dyn Render<TuiOut>> {
pub fn view_nil (_: &App) -> Box<dyn Render<TuiOut>> {
Box::new(Fill::xy("·")) Box::new(Fill::xy("·"))
} }

View file

@ -1,22 +1,37 @@
use crate::*; use crate::*;
content!(TuiOut:|self: App|VIEW[":view"](self));//if let Ok(Some(view)) = VIEW[":view"] { view(self) } else { panic!() }); content!(TuiOut:|self: App|Stack::above(|add|{
pub const VIEW: DslNs<'static, DslCb> = DslNs(&[ for dsl in self.mode.view.iter() { add(&self.view(dsl.as_ref())); }
(":view", |state|VIEW[":view/menu"](state)), }));
(":view/menu", |state|{ impl App {
let selected = state.dialog.menu_selected(); fn view <D: Dsl> (&self, index: D) -> Box<dyn Render<TuiOut>> {
let outputs = VIEW[":view/ports/outs"](state); if let Ok(Some(symbol)) = index.src() {
let inputs = VIEW[":view/ports/ins"](state); for (key, value) in Self::SYMS.0.iter() {
if symbol == *key {
return value(self)
}
}
}
view_nil(self)
}
}
dsl_sym!(|app: App| -> Box<dyn Render<TuiOut>> {
":view" => app.view(":view/menu"),
":view/menu" => {
let selected = app.dialog.menu_selected();
let outputs = app.view(":view/ports/outs");
let inputs = app.view(":view/ports/ins");
Box::new(Tui::bg(Rgb(0,0,0), Bsp::s(outputs, Bsp::s( Box::new(Tui::bg(Rgb(0,0,0), Bsp::s(outputs, Bsp::s(
Fill::x(Fixed::y(3, Tui::bg(Rgb(33,33,33), Tui::bold(true, "tek 0.3.0-rc0")))), Fill::x(Fixed::y(3, Tui::bg(Rgb(33,33,33), Tui::bold(true, "tek 0.3.0-rc0")))),
Bsp::n(inputs, Bsp::n( Bsp::n(inputs, Bsp::n(
Fill::x(Fixed::y(3, Tui::bg(Rgb(33,33,33), Bsp::e(Tui::fg(Rgb(255,192,48), "[Enter]"), " new session")))), Fill::x(Fixed::y(3, Tui::bg(Rgb(33,33,33), Bsp::e(Tui::fg(Rgb(255,192,48), "[Enter]"), " new session")))),
Fill::y(Align::n(Fill::x(VIEW[":view/profiles"](state)))))))))) }), Fill::y(Align::n(Fill::x(app.view(":view/profiles"))))))))))
(":view/ports/outs", |state|Box::new(Fill::x(Fixed::y(3, },
Bsp::a(Fill::x(Align::w(" L AUDIO OUTS")), Bsp::a("MIDI OUT", Fill::x(Align::e("AUDIO OUTS R ")))))))), ":view/ports/outs" => Box::new(Fill::x(Fixed::y(3,
(":view/ports/ins", |state|Box::new(Fill::x(Fixed::y(3, Bsp::a(Fill::x(Align::w(" L AUDIO OUTS")), Bsp::a("MIDI OUT", Fill::x(Align::e("AUDIO OUTS R "))))))),
Bsp::a(Fill::x(Align::w(" L AUDIO INS")), Bsp::a("MIDI INS", Fill::x(Align::e("AUDIO INS R ")))))))), ":view/ports/ins" => Box::new(Fill::x(Fixed::y(3,
(":view/profiles", |state: &App|Box::new({ Bsp::a(Fill::x(Align::w(" L AUDIO ISYMS")), Bsp::a("MIDI ISYMS", Fill::x(Align::e("AUDIO ISYMS R "))))))),
let modes = state.config.modes.clone(); ":view/profiles" => Box::new({
let modes = app.config.modes.clone();
Stack::south(move|add: &mut dyn FnMut(&dyn Render<TuiOut>)|{ Stack::south(move|add: &mut dyn FnMut(&dyn Render<TuiOut>)|{
for (index, (id, profile)) in modes.read().unwrap().iter().enumerate() { for (index, (id, profile)) in modes.read().unwrap().iter().enumerate() {
let bg = if index == 0 { Rgb(64,64,64) } else { Rgb(32,32,32) }; let bg = if index == 0 { Rgb(64,64,64) } else { Rgb(32,32,32) };
@ -26,34 +41,33 @@ pub const VIEW: DslNs<'static, DslCb> = DslNs(&[
Fill::x(Bsp::a( Fill::x(Bsp::a(
Fill::x(Align::w(Tui::fg(Rgb(224,192,128), name))), Fill::x(Align::w(Tui::fg(Rgb(224,192,128), name))),
Fill::x(Align::e(Tui::fg(Rgb(224,128,32), &id))))), Fill::x(Align::e(Tui::fg(Rgb(224,128,32), &id))))),
Fill::x(Align::w(info)))))); } })})), Fill::x(Align::w(info)))))); } })}),
(":view/browse", |state: &App|{ ":view/browse" => {
let browser = state.dialog.browser().cloned().unwrap(); let browser = app.dialog.browser().cloned().unwrap();
Box::new(Bsp::s( Box::new(Bsp::s(Padding::xy(3, 1, app.view(":view/browse-title")),
Padding::xy(3, 1, VIEW[":view/browse-title"](state)), Outer(true, Style::default().fg(Tui::g(96)))
Outer(true, Style::default().fg(Tui::g(96))) .enclose(Fill::xy(browser)))) },
.enclose(Fill::xy(browser)))) }), ":view/browse/title" => {
(":view/browse-title", |state: &App|{ let target = app.dialog.browser_target().unwrap();
let target = state.dialog.browser_target().unwrap(); Box::new(Fill::x(Align::w(FieldV(Default::default(), match target {
Box::new(Fill::x(Align::w(FieldV(Default::default(), match target { BrowserTarget::SaveProject => "Save project:",
BrowserTarget::SaveProject => "Save project:", BrowserTarget::LoadProject => "Load project:",
BrowserTarget::LoadProject => "Load project:", BrowserTarget::ImportSample(_) => "Import sample:",
BrowserTarget::ImportSample(_) => "Import sample:", BrowserTarget::ExportSample(_) => "Export sample:",
BrowserTarget::ExportSample(_) => "Export sample:", BrowserTarget::ImportClip(_) => "Import clip:",
BrowserTarget::ImportClip(_) => "Import clip:", BrowserTarget::ExportClip(_) => "Export clip:",
BrowserTarget::ExportClip(_) => "Export clip:", }, Shrink::x(3, Fixed::y(1, Tui::fg(Tui::g(96), RepeatH("🭻"))))))))},
}, Shrink::x(3, Fixed::y(1, Tui::fg(Tui::g(96), RepeatH("🭻"))))))))}), ":view/device" => {
(":view/device", |state: &App|{ let selected = app.dialog.device_kind().unwrap();
let selected = state.dialog.device_kind().unwrap();
Box::new(Bsp::s(Tui::bold(true, "Add device"), Map::south(1, Box::new(Bsp::s(Tui::bold(true, "Add device"), Map::south(1,
move||device_kinds().iter(), move||device_kinds().iter(),
move|label: &&'static str, i|{ move|label: &&'static str, i|{
let bg = if i == selected { Rgb(64,128,32) } else { Rgb(0,0,0) }; let bg = if i == selected { Rgb(64,128,32) } else { Rgb(0,0,0) };
let lb = if i == selected { "[ " } else { " " }; let lb = if i == selected { "[ " } else { " " };
let rb = if i == selected { " ]" } else { " " }; let rb = if i == selected { " ]" } else { " " };
Fill::x(Tui::bg(bg, Bsp::e(lb, Bsp::w(rb, "FIXME device name")))) }))) }), Fill::x(Tui::bg(bg, Bsp::e(lb, Bsp::w(rb, "FIXME device name")))) }))) },
//(":view/options", view_options), //(":view/options", view_options),
]); });
fn wrap_dialog (dialog: impl Content<TuiOut>) -> impl Content<TuiOut> { fn wrap_dialog (dialog: impl Content<TuiOut>) -> impl Content<TuiOut> {
Fixed::xy(70, 23, Tui::fg_bg(Rgb(255,255,255), Rgb(16,16,16), Bsp::b( Fixed::xy(70, 23, Tui::fg_bg(Rgb(255,255,255), Rgb(16,16,16), Bsp::b(
Repeat(" "), Outer(true, Style::default().fg(Tui::g(96))).enclose(dialog)))) Repeat(" "), Outer(true, Style::default().fg(Tui::g(96))).enclose(dialog))))
@ -72,8 +86,8 @@ impl ScenesView for App {
//Bsp::s("", //Bsp::s("",
//Map::south(1, //Map::south(1,
//move||state.config.binds.layers.iter() //move||app.config.binds.layers.iter()
//.filter_map(|a|(a.0)(state).then_some(a.1)) //.filter_map(|a|(a.0)(app).then_some(a.1))
//.flat_map(|a|a) //.flat_map(|a|a)
//.filter_map(|x|if let Value::Exp(_, iter)=x.value{ Some(iter) } else { None }) //.filter_map(|x|if let Value::Exp(_, iter)=x.value{ Some(iter) } else { None })
//.skip(offset) //.skip(offset)

View file

@ -79,6 +79,7 @@ impl Cli {
config, config,
color: ItemTheme::random(), color: ItemTheme::random(),
dialog: Dialog::Menu(0), dialog: Dialog::Menu(0),
mode: Mode { view: vec![":view/menu".into()], ..Default::default() },
project: Arrangement { project: Arrangement {
name: Default::default(), name: Default::default(),
color: ItemTheme::random(), color: ItemTheme::random(),

2
deps/tengri vendored

@ -1 +1 @@
Subproject commit 7fd6c91643cbcfece56ebc14500c6a1ab775fc9e Subproject commit ab1afa219f520138ff1a089de4223e52298b1d0e