diff --git a/Cargo.toml b/Cargo.toml index 1b93bd3a..fcc3c5d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,8 +11,7 @@ name = "tek" path = "src/tek.rs" [target.'cfg(target_os = "linux")'] -linker = "clang" -rustflags = ["-Clink-arg=-fuse-ld=mold", "-Clink-arg=-Wl,--no-rosegment"] +rustflags = ["-C", "link-arg=-fuse-ld=mold"] [dependencies] tek_proc = { path = "./proc" } diff --git a/Justfile b/Justfile index 87637b33..33f077c9 100644 --- a/Justfile +++ b/Justfile @@ -1,11 +1,7 @@ #export RUSTFLAGS := "--cfg procmacro2_semver_exempt -Zmacro-backtrace -Clink-arg=-fuse-ld=mold" export RUST_BACKTRACE := "1" -[default] -list: - just -l - -now +ARGS="new": +default +ARGS="new": cargo run -- {{ARGS}} doc +ARGS="": @@ -48,7 +44,7 @@ run-init: rm -rf ~/.config/tek && {{debug}} prof: - CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph -- new + CARGO_PROFILE_RELEASE_DEBUG=true cargo flamegraph -- release := "reset && cargo run --release --" release: diff --git a/proc/src/lib.rs b/proc/src/lib.rs index bb971c63..4725ca5f 100644 --- a/proc/src/lib.rs +++ b/proc/src/lib.rs @@ -48,7 +48,7 @@ attribute!(commands { impl Parse for CustomAttributeMeta { /// Parse contents of `#[command(...)]` attribute tag. fn parse (input: ParseStream) -> Result { - let meta: Expr = input.parse()?; + let meta: Expr = input.parse()?; Ok(match meta { // Struct name only Expr::Path(ExprPath { path, .. }) => Self(path, None), @@ -353,7 +353,7 @@ attribute!(command { let mut variants_filtered = item.variants.clone(); variants_filtered.clear(); for variant in item.variants.iter_mut() { - let Variant { attrs, ident, fields, discriminant: _ } = variant; + let Variant { attrs, ident, fields, discriminant } = variant; let mut attrs_filtered = attrs.clone(); attrs_filtered.clear(); for attr in attrs.iter() { @@ -396,7 +396,7 @@ attribute!(command { panic!() }; match fields { - Fields::Named(_fields) => todo!("named command fields"), + Fields::Named(fields) => todo!("named command fields"), Fields::Unnamed(fields) => { let mut params = quote! {}; let mut values = quote! {}; @@ -455,11 +455,9 @@ attribute!(keyword { impl Parse for CustomAttributeItem { fn parse (input: ParseStream) -> Result { let mut item: ItemEnum = input.parse()?; - for Variant { - attrs, ident: _, fields: _, discriminant: _ - } in item.variants.iter_mut() { + for Variant { attrs, ident, fields, discriminant } in item.variants.iter_mut() { attrs.retain(|attr|if let syn::Meta::List(MetaList { - ref path, tokens: ref _tokens, .. + ref path, ref tokens, .. }) = attr.meta && path == &Path::from(Ident::new( "keyword", Span::call_site() )) { @@ -476,7 +474,7 @@ attribute!(keyword { fn to_tokens (&self, out: &mut TokenStream2) { let Self( CustomAttributeMeta(state), - CustomAttributeItem(item, _variants) + CustomAttributeItem(item, variants) ) = self; let ident = &item.ident; let body = quote! {}; diff --git a/shell.nix b/shell.nix index 66738184..7417c55d 100755 --- a/shell.nix +++ b/shell.nix @@ -9,7 +9,6 @@ pkgs.grcov pkgs.libclang pkgs.mold - pkgs.perf pkgs.pkg-config pkgs.watchexec ]; diff --git a/src/deps.rs b/src/deps.rs deleted file mode 100644 index 540a4f68..00000000 --- a/src/deps.rs +++ /dev/null @@ -1,39 +0,0 @@ -pub extern crate atomic_float; -pub extern crate xdg; -pub extern crate tengri; -#[cfg(feature = "cli")] -pub(crate) use ::clap::{self, Parser, Subcommand}; -#[allow(unused)] -pub(crate) use ::{ - std::{ - cmp::Ord, - collections::BTreeMap, - error::Error, - ffi::OsString, - fmt::{Write, Debug, Formatter}, - fs::File, - ops::{Add, Sub, Mul, Div, Rem}, - path::{Path, PathBuf}, - sync::{Arc, RwLock, atomic::{AtomicBool, AtomicUsize, AtomicU64, Ordering::Relaxed}}, - time::Duration, - thread::{spawn, JoinHandle}, - }, - xdg::{ - BaseDirectories, - }, - tengri::{ - *, - dizzle::*, - midly::{ - Smf, TrackEventKind, MidiMessage, Error as MidiError, - num::*, - live::*, - }, - crossterm::event::{Event, KeyEvent}, - ratatui::{ - self, - prelude::{Rect, Style, Stylize, Buffer, Color::{self, *}}, - widgets::{Widget, canvas::{Canvas, Line}}, - }, - }, -}; diff --git a/src/device/arrange/scene.rs b/src/device/arrange/scene.rs index cff99a43..ce45629e 100644 --- a/src/device/arrange/scene.rs +++ b/src/device/arrange/scene.rs @@ -1,3 +1,4 @@ +use crate::*; use super::*; /// A scene consists of a set of clips to play together. @@ -83,19 +84,19 @@ pub trait SceneController: HasScene + Namespace> + Namespace { - #[command(SetSize = "size")] - fn scene_set_size (&mut self, _size: usize) -> Perhaps + #[command(SetSize = "scene/size")] + fn scene_set_size (&mut self, size: usize) -> Perhaps where Self: Namespace { todo!() } - #[command(SetZoom = "zoom")] - fn scene_set_zoom (&mut self, _size: usize) -> Perhaps + #[command(SetZoom = "scene/zoom")] + fn scene_set_zoom (&mut self, size: usize) -> Perhaps where Self: Namespace { todo!() } - #[command(SetName = "name")] + #[command(SetName = "scene/name")] fn scene_set_name (&mut self, name: Arc) -> Perhaps where Self: Namespace> { @@ -105,7 +106,7 @@ pub trait SceneController: HasScene |name|SceneCommand::SetName { name } )).transpose()?.flatten()) } - #[command(SetColor = "color")] + #[command(SetColor = "scene/color")] fn scene_set_color (&mut self, color: ItemTheme) -> Perhaps where Self: Namespace { @@ -171,9 +172,6 @@ pub trait HasScenes: AsRef> + AsMut> { } impl + Namespace + Namespace> + Namespace>> @@ -181,20 +179,10 @@ impl + Namespace + Namespace> + Namespace>> { - #[command(Add = "add")] - fn scenes_add (&mut self) -> Perhaps - where Self: HasScenes + HasJack<'static> - { - let (_index, _) = self.scenes_add_one(None, None)?; - Ok(None) - } // TODO } @@ -275,27 +263,27 @@ impl HasSceneScroll for App { } } -pub fn view_scene_name ( - select: &Selection, - editor: Option<&MidiEditor>, - index: usize, - scene: &Scene, - editing: bool -) -> impl Draw { - let h = if select.scene() == Some(index) && let Some(_editor) = editor { - 7 - } else { - Scene::DEFAULT_HEIGHT as u16 - }; - let a = east(format!("·s{index:02} "), - fg(g(255), bold(true, &scene.name))).align_w().full_w(); - let b = when(select.scene() == Some(index) && editing, south( - editor.as_ref().map(|e|e.clip_status()), - editor.as_ref().map(|e|e.edit_status())).align_nw().full_wh()); - let c = if select.scene() == Some(index) { - scene.color.light.term - } else { - scene.color.base.term - }; - bg(c, south(a, b).align_nw()).exact_wh(20, h) -} + pub fn view_scene_name ( + select: &Selection, + editor: Option<&MidiEditor>, + index: usize, + scene: &Scene, + editing: bool + ) -> impl Draw { + let h = if select.scene() == Some(index) && let Some(_editor) = editor { + 7 + } else { + Scene::DEFAULT_HEIGHT as u16 + }; + let a = east(format!("·s{index:02} "), + fg(g(255), bold(true, &scene.name))).align_w().full_w(); + let b = when(select.scene() == Some(index) && editing, south( + editor.as_ref().map(|e|e.clip_status()), + editor.as_ref().map(|e|e.edit_status())).align_nw().full_wh()); + let c = if select.scene() == Some(index) { + scene.color.light.term + } else { + scene.color.base.term + }; + bg(c, south(a, b).align_nw()).exact_wh(20, h) + } diff --git a/src/device/arrange/track.rs b/src/device/arrange/track.rs index 4dbdd540..cc10823a 100644 --- a/src/device/arrange/track.rs +++ b/src/device/arrange/track.rs @@ -104,19 +104,19 @@ pub trait TrackController: HasTrack Ok(None) } #[command(SetMute = "track/mute")] - fn track_set_mute (&mut self, _mute: Option) -> Perhaps { + fn track_set_mute (&mut self, mute: Option) -> Perhaps { todo!() } #[command(SetSolo = "track/solo")] - fn track_set_solo (&mut self, _solo: Option) -> Perhaps { + fn track_set_solo (&mut self, solo: Option) -> Perhaps { todo!() } #[command(SetSize = "track/size")] - fn track_set_size (&mut self, _size: usize) -> Perhaps { + fn track_set_size (&mut self, size: usize) -> Perhaps { todo!() } #[command(SetZoom = "track/zoom")] - fn track_set_zoom (&mut self, _zoom: usize) -> Perhaps { + fn track_set_zoom (&mut self, zoom: usize) -> Perhaps { todo!() } #[command(SetName = "track/name")] @@ -259,8 +259,6 @@ pub trait HasTracks: AsRef> + AsMut> + HasClock + HasTrack } impl + Namespace + Namespace> + Namespace> @@ -272,8 +270,6 @@ impl + Namespace + Namespace> + Namespace> @@ -283,7 +279,7 @@ pub trait TracksController: HasTracks + Namespace>>>>> { - #[command(Stop = "stop")] + #[command(Stop = "tracks/stop")] /// Stop all playing clips fn tracks_stop_all (&mut self) -> Perhaps { for track in self.tracks_mut().iter_mut() { @@ -292,7 +288,7 @@ pub trait TracksController: HasTracks Ok(None) } - #[command(Launch = "launch")] + #[command(Launch = "tracks/launch")] /// Launch multiple clips fn tracks_launch ( &mut self, clips: Option>>>> @@ -308,14 +304,6 @@ pub trait TracksController: HasTracks } Ok(None) } - - #[command(Add = "add")] - fn tracks_add (&mut self) -> Perhaps - where Self: HasScenes + HasJack<'static> - { - let (_index, _) = self.tracks_add_one(None, None, [].into(), [].into())?; - Ok(None) - } } impl< diff --git a/src/device/browse.rs b/src/device/browse.rs index be3bb3ec..ca0aa331 100644 --- a/src/device/browse.rs +++ b/src/device/browse.rs @@ -11,12 +11,6 @@ impl App { } } -impl + - Namespace + - Namespace> -> BrowseController for T {} - #[tek_proc::commands(BrowseCommand = "browse")] pub trait BrowseController: Namespace + @@ -60,13 +54,13 @@ pub trait BrowseController: pub size: Sizer, } -//pub(crate) struct EntriesIterator<'a, S: Screen> { - //pub browser: &'a Browse, - //pub offset: usize, - //pub length: usize, - //pub index: usize, - //_screen: std::marker::PhantomData -//} +pub(crate) struct EntriesIterator<'a, S: Screen> { + pub browser: &'a Browse, + pub offset: usize, + pub length: usize, + pub index: usize, + _screen: std::marker::PhantomData +} #[derive(Clone, Debug)] pub enum BrowseTarget { SaveProject, @@ -118,37 +112,40 @@ impl Browse { unreachable!() }) } - //fn tui (&self) -> impl Draw { - //iter_south_fixed(1, ||self.tui_entries(), |entry, _index|entry.origin_w().full_w()) - //} - //fn tui_entries (&self) -> EntriesIterator<'_, Tui> { - //EntriesIterator { - //offset: 0, - //index: 0, - //length: self.dirs.len() + self.files.len(), - //browser: self, - //_screen: Default::default(), - //} - //} + fn _todo_stub_path_buf (&self) -> PathBuf { todo!() } + fn _todo_stub_usize (&self) -> usize { todo!() } + fn _todo_stub_arc_str (&self) -> Arc { todo!() } + fn tui (&self) -> impl Draw { + iter_south_fixed(1, ||self.tui_entries(), |entry, _index|entry.origin_w().full_w()) + } + fn tui_entries (&self) -> EntriesIterator<'_, Tui> { + EntriesIterator { + offset: 0, + index: 0, + length: self.dirs.len() + self.files.len(), + browser: self, + _screen: Default::default(), + } + } } -//impl<'a> Iterator for EntriesIterator<'a, Tui> { - //type Item = impl Draw; - //fn next (&mut self) -> Option { - //let dirs = self.browser.dirs.len(); - //let files = self.browser.files.len(); - //let index = self.index; - //if self.index < dirs { - //self.index += 1; - //Some(bold(true, self.browser.dirs[index].1.as_str())) - //} else if self.index < dirs + files { - //self.index += 1; - //Some(bold(false, self.browser.files[index - dirs].1.as_str())) - //} else { - //None - //} - //} -//} +impl<'a> Iterator for EntriesIterator<'a, Tui> { + type Item = impl Draw; + fn next (&mut self) -> Option { + let dirs = self.browser.dirs.len(); + let files = self.browser.files.len(); + let index = self.index; + if self.index < dirs { + self.index += 1; + Some(bold(true, self.browser.dirs[index].1.as_str())) + } else if self.index < dirs + files { + self.index += 1; + Some(bold(false, self.browser.files[index - dirs].1.as_str())) + } else { + None + } + } +} impl PartialEq for BrowseTarget { fn eq (&self, other: &Self) -> bool { diff --git a/src/device/clock.rs b/src/device/clock.rs index fd7f811b..e37779ea 100644 --- a/src/device/clock.rs +++ b/src/device/clock.rs @@ -416,6 +416,18 @@ impl Clock { } } +impl Clock { + fn _todo_provide_u32 (&self) -> u32 { + todo!() + } + fn _todo_provide_opt_u32 (&self) -> Option { + todo!() + } + fn _todo_provide_f64 (&self) -> f64 { + todo!() + } +} + impl_has!(Clock: |self: Track|self.sequencer.clock); impl_default!(Timebase: Self::new(48000f64, 150f64, DEFAULT_PPQ)); diff --git a/src/device/editor.rs b/src/device/editor.rs index 632e94fb..c8dbf86a 100644 --- a/src/device/editor.rs +++ b/src/device/editor.rs @@ -236,6 +236,7 @@ impl MidiEditor { self.mode.redraw(); } } + fn _todo_opt_clip_stub (&self) -> Option>> { todo!() } fn clip_length (&self) -> usize { self.clip().as_ref().map(|p|p.read().unwrap().length).unwrap_or(1) } fn note_length (&self) -> usize { self.get_note_len() } fn note_pos (&self) -> usize { self.get_note_pos() } diff --git a/src/device/pool.rs b/src/device/pool.rs index 93e18234..8964e8c8 100644 --- a/src/device/pool.rs +++ b/src/device/pool.rs @@ -76,6 +76,8 @@ impl + Namespace + Namespace + + Namespace + + Namespace + Namespace > PoolController for T {} @@ -87,6 +89,8 @@ pub trait PoolController: HasPool + Namespace + Namespace + Namespace + + Namespace + + Namespace + Namespace { @@ -222,7 +226,7 @@ pub trait PoolController: HasPool } #[command(CropSet = "crop/set")] - fn crop_set (&mut self, _length: usize) -> Perhaps { + fn crop_set (&mut self, length: usize) -> Perhaps { if let Some(PoolMode::Length(clip, ref mut length, ref mut _focus)) = self.pool_mut().mode_mut().clone() { @@ -233,7 +237,7 @@ pub trait PoolController: HasPool clip.write().unwrap().length = *length; } *self.pool_mut().mode_mut() = None; - return Ok(old_length.map(|l|PoolCommand::CropSet { _length: l })) + return Ok(old_length.map(|length|PoolCommand::CropSet { length })) } Ok(None) } @@ -429,6 +433,12 @@ impl ClipLength { } impl Pool { + fn _todo_usize_ (&self) -> usize { todo!() } + fn _todo_bool_ (&self) -> bool { todo!() } + fn _todo_clip_ (&self) -> MidiClip { todo!() } + fn _todo_path_ (&self) -> PathBuf { todo!() } + fn _todo_color_ (&self) -> ItemColor { todo!() } + fn _todo_str_ (&self) -> Arc { todo!() } fn _clip_new (&self) -> MidiClip { self.new_clip() } fn _clip_cloned (&self) -> MidiClip { self.cloned_clip() } fn _clip_index_current (&self) -> usize { 0 } @@ -439,44 +449,44 @@ impl Pool { } impl<'a> PoolView<'a> { - //fn tui (&self) -> impl Draw { - //let Self(pool) = self; - ////let color = self.1.clip().map(|c|c.read().unwrap().color).unwrap_or_else(||g(32).into()); - ////let on_bg = |x|x;//below(Repeat(" "), bg(color.darkest.term, x)); - ////let border = |x|x;//Outer(Style::default().fg(color.dark.term).bg(color.darkest.term)).enclose(x); - ////let height = pool.clips.read().unwrap().len() as u16; - //iter( - //||pool.clips().clone().into_iter(), - //move|clip: Arc>, i: usize|{ - //let MidiClip { ref name, color, length, .. } = *clip.read().unwrap(); - //let item_height = 1; - //let _item_offset = i as u16 * item_height; - //let selected = i == pool.clip_index(); - //let b = if selected { color.light.term } else { color.base.term }; - //let f = color.lightest.term; - //let name = if false { format!(" {i:>3}") } else { format!(" {i:>3} {name}") }; - //let length = if false { String::default() } else { format!("{length} ") }; - //bg(b, below!( - //fg(f, bold(selected, name)).origin_w().full_w(), - //fg(f, bold(selected, length)).origin_e().full_w(), - //when(selected, bold(true, fg(g(255), "▶"))).origin_w().full_w(), - //when(selected, bold(true, fg(g(255), "◀"))).origin_e().full_w(), - //)).exact_h(1) - //}).origin_n().full_h().exact_w(20) - //} + fn tui (&self) -> impl Draw { + let Self(pool) = self; + //let color = self.1.clip().map(|c|c.read().unwrap().color).unwrap_or_else(||g(32).into()); + //let on_bg = |x|x;//below(Repeat(" "), bg(color.darkest.term, x)); + //let border = |x|x;//Outer(Style::default().fg(color.dark.term).bg(color.darkest.term)).enclose(x); + //let height = pool.clips.read().unwrap().len() as u16; + iter( + ||pool.clips().clone().into_iter(), + move|clip: Arc>, i: usize|{ + let MidiClip { ref name, color, length, .. } = *clip.read().unwrap(); + let item_height = 1; + let _item_offset = i as u16 * item_height; + let selected = i == pool.clip_index(); + let b = if selected { color.light.term } else { color.base.term }; + let f = color.lightest.term; + let name = if false { format!(" {i:>3}") } else { format!(" {i:>3} {name}") }; + let length = if false { String::default() } else { format!("{length} ") }; + bg(b, below!( + fg(f, bold(selected, name)).origin_w().full_w(), + fg(f, bold(selected, length)).origin_e().full_w(), + when(selected, bold(true, fg(g(255), "▶"))).origin_w().full_w(), + when(selected, bold(true, fg(g(255), "◀"))).origin_e().full_w(), + )).exact_h(1) + }).origin_n().full_h().exact_w(20) + } } impl ClipLength { - //fn tui (&self) -> impl Draw { - //use ClipLengthFocus::*; - //let bars = format!("{}", self.bars()); - //let beats = format!("{}", self.beats()); - //let ticks = format!("{:>02}", self.ticks()); - //match self.focus { - //None => east!(" ", bars, ".", beats, ".", ticks), - //Some(Bar) => east!("[", bars, "]", beats, ".", ticks), - //Some(Beat) => east!(" ", bars, "[", beats, "]", ticks), - //Some(Tick) => east!(" ", bars, ".", beats, "[", ticks), - //} - //} + fn tui (&self) -> impl Draw { + use ClipLengthFocus::*; + let bars = format!("{}", self.bars()); + let beats = format!("{}", self.beats()); + let ticks = format!("{:>02}", self.ticks()); + match self.focus { + None => east!(" ", bars, ".", beats, ".", ticks), + Some(Bar) => east!("[", bars, "]", beats, ".", ticks), + Some(Beat) => east!(" ", bars, "[", beats, "]", ticks), + Some(Tick) => east!(" ", bars, ".", beats, "[", ticks), + } + } } diff --git a/src/device/sequence.rs b/src/device/sequence.rs index 1ccc1d9d..b4ceadb6 100644 --- a/src/device/sequence.rs +++ b/src/device/sequence.rs @@ -245,12 +245,6 @@ pub trait HasMidiClip { fn clip (&self) -> Option>>; } -impl HasMidiClip for App { - fn clip (&self) -> Option>> { - None - } -} - impl> + Namespace> @@ -263,7 +257,7 @@ pub trait MidiClipController: HasMidiClip { #[command(SetColor = "clip/color")] - fn clip_set_color (&mut self, _color: Option) -> Perhaps { + fn clip_set_color (&mut self, color: Option) -> Perhaps { //(SetColor [t: usize, s: usize, c: ItemTheme] //clip.clip_set_color(t, s, c).map(|o|Self::SetColor(t, s, o))))); //("color" [a: usize, b: usize] Some(Self::SetColor(a.unwrap(), b.unwrap(), ItemTheme::random()))) @@ -271,7 +265,7 @@ pub trait MidiClipController: HasMidiClip } #[command(SetLoop = "clip/loop")] - fn clip_toggle_loop (&mut self, _looping: Option) -> Perhaps { + fn clip_toggle_loop (&mut self, looping: Option) -> Perhaps { //(SetLoop [t: usize, s: usize, l: bool] cmd_todo!("\n\rtodo: {self:?}")) //("loop" [a: usize, b: usize, c: bool] Some(Self::SetLoop(a.unwrap(), b.unwrap(), c.unwrap()))) todo!() @@ -362,6 +356,14 @@ impl PartialEq for MidiClip { impl Eq for MidiClip {} +impl MidiClip { + fn _todo_opt_bool_stub_ (&self) -> Option { todo!() } + fn _todo_bool_stub_ (&self) -> bool { todo!() } + fn _todo_usize_stub_ (&self) -> usize { todo!() } + fn _todo_arc_str_stub_ (&self) -> Arc { todo!() } + fn _todo_item_theme_stub (&self) -> ItemTheme { todo!() } + fn _todo_opt_item_theme_stub (&self) -> Option { todo!() } +} impl_has!(Sequencer: |self: Track| self.sequencer); impl_has!(Clock: |self: Sequencer| self.clock); impl_has!(Vec: |self: Sequencer| self.midi_ins); diff --git a/src/tek.edn b/src/tek.edn index 7c2f2e99..970f29af 100644 --- a/src/tek.edn +++ b/src/tek.edn @@ -18,13 +18,10 @@ (align/s (bsp/e :ports/out (bsp/e :transport :ports/in))) (align/c (bsp/s (align/x (bg (g 36) :logo)) (bg (g 24) :dialog/menu)))))) -(keys :back (@escape (back))) -(keys :confirm (@enter (dialog confirm))) -(keys :axis/y (@up (axis (dec :y))) - (@down (axis (inc :y)))) - (mode :arranger (name Arranger) (info Launch grid.) - (keys :clock :color :launch :scenes :tracks :global) + (keys (see :clock :color :launch :scenes :tracks :global) + (@tab project/edit) (@shift/I project/input/add) (@shift/O project/output/add) + (@shift/D dialog/show :dialog/device)) (mode :editor (keys :editor)) (mode :dialog (keys :dialog)) (mode :message (keys :message)) @@ -49,27 +46,6 @@ (bg (g 80) (bsp/s :scenes/names :editor)) (bg (g 90) :scenes)))))))))))))) -(keys :clock (@space clock/toggle 0) - (@shift/space clock/toggle 0)) - -(keys :color (@c color)) - -(keys :launch (@q launch)) - -(keys :scenes (@shift/S (scenes add)) - (@s (select scene)) - (@up (select scene-dec)) - (@down (select scene-inc))) - -(keys :tracks (@shift/T (tracks add)) - (@t (select track)) - (@left (select track-dec)) - (@right (select track-inc))) - -(keys :global (see :history :saveload) - (@f8 dialog :options) - (@f10 dialog :quit)) - (view :ports/out (bsp/s (align/w (text L-AUDIO-OUT)) (bsp/e (text MIDI-OUT) @@ -110,24 +86,18 @@ (view :groove/sequence (bsp/e (fill/y (align/n (bsp/s :status/v :editor-status))) (bsp/e :samples/keys :editor))) -(keys :axis/x (@left dec :x) - (@right inc :x)) -(keys :axis/x2 (@shift/left dec :x2) - (@shift/right inc :x2)) -(keys :axis/y2 (@shift/up dec :y2) - (@shift/down inc :y2)) -(keys :axis/z (@minus dec :z) - (@equal inc :z)) -(keys :axis/z2 (@underscore dec :z2) - (@plus inc :z2)) -(keys :axis/i (@comma dec :i) - (@period inc :z)) -(keys :axis/i2 (@lt dec :i2) - (@gt inc :z2)) -(keys :axis/w (@openbracket dec :w) - (@closebracket inc :w)) -(keys :axis/w2 (@openbrace dec :w2) - (@closebrace inc :w2)) +(keys :back (@escape (back))) +(keys :confirm (@enter :confirm)) +(keys :axis/x (@left dec :x) (@right inc :x)) +(keys :axis/x2 (@shift/left dec :x2) (@shift/right inc :x2)) +(keys :axis/y (@up (dec :y)) (@down (inc :y))) +(keys :axis/y2 (@shift/up dec :y2) (@shift/down inc :y2)) +(keys :axis/z (@minus dec :z) (@equal inc :z)) +(keys :axis/z2 (@underscore dec :z2) (@plus inc :z2)) +(keys :axis/i (@comma dec :i) (@period inc :z)) +(keys :axis/i2 (@lt dec :i2) (@gt inc :z2)) +(keys :axis/w (@openbracket dec :w) (@closebracket inc :w)) +(keys :axis/w2 (@openbrace dec :w2) (@closebrace inc :w2)) (keys :focus) (keys :editor (see :axis/i :axis/i2 :axis/y :page :editor/view :editor/add :editor/del)) @@ -147,11 +117,19 @@ (@down sampler/select :sample/below) (@left sampler/select :sample/to/left) (@right sampler/select :sample/to/right)) +(keys :tracks (@t select :select/track) + (@shift/T project/track/add) + (@left select :select/track/dec) + (@right select :select/track/inc)) (keys :track (see :color :launch :axis/z :axis/z2 :delete) (@r toggle :rec) (@m toggle :mon) (@p toggle :play) (@P toggle :solo)) +(keys :scenes (@s select :select/scene) + (@shift/S project/scene/add) + (@up select :select/scene/dec) + (@down select :select/scene/inc)) (keys :scene (see :color :launch :axis/z :axis/z2 :delete)) (keys :help (@f1 dialog :help)) (keys :page (@pgup page/up) @@ -166,6 +144,13 @@ (@r redo 1)) (keys :saveload (@f6 dialog :save) (@f9 dialog :load)) +(keys :color (@c color)) +(keys :launch (@q launch)) +(keys :clock (@space clock/toggle 0) + (@shift/space clock/toggle 0)) +(keys :global (see :history :saveload) + (@f8 dialog :options) + (@f10 dialog :quit)) (keys :clip (see :color :launch :axis/z :axis/z2 :delete) (@l toggle :loop)) (keys :sequencer (see :color :launch) diff --git a/src/tek.rs b/src/tek.rs index 52563b1c..0e25d92b 100644 --- a/src/tek.rs +++ b/src/tek.rs @@ -1,6 +1,44 @@ #![allow(clippy::unit_arg)] -//#![feature(impl_trait_in_assoc_type)] // Used by EntriesIterator; TODO remove -mod deps; pub use self::deps::*; +#![feature(impl_trait_in_assoc_type)] // Used by EntriesIterator; TODO remove +pub extern crate atomic_float; +pub extern crate xdg; +pub extern crate tengri; +#[cfg(feature = "cli")] +pub(crate) use ::clap::{self, Parser, Subcommand}; +#[allow(unused)] +pub(crate) use ::{ + std::{ + cmp::Ord, + collections::BTreeMap, + error::Error, + ffi::OsString, + fmt::{Write, Debug, Formatter}, + fs::File, + ops::{Add, Sub, Mul, Div, Rem}, + path::{Path, PathBuf}, + sync::{Arc, RwLock, atomic::{AtomicBool, AtomicUsize, AtomicU64, Ordering::Relaxed}}, + time::Duration, + thread::{spawn, JoinHandle}, + }, + xdg::{ + BaseDirectories, + }, + tengri::{ + *, + dizzle::*, + midly::{ + Smf, TrackEventKind, MidiMessage, Error as MidiError, + num::*, + live::*, + }, + crossterm::event::{Event, KeyEvent}, + ratatui::{ + self, + prelude::{Rect, Style, Stylize, Buffer, Color::{self, *}}, + widgets::{Widget, canvas::{Canvas, Line}}, + }, + }, +}; /// Banner. pub(crate) const HEADER: &'static str = r#" @@ -50,27 +88,6 @@ fn run_new_plain (config: Config) -> Usually<()> { })?) } - //pub fn tui ( - //app: Arc>, - //jack: Jack, - //sync_lead: &bool, - //sync_follow: &bool, - //) -> Usually<()> { - //// Run the [Tui] and [Jack] threads with the [App] state. - //Tui::run_main(&jack.run(move|jack|{ - //// Between jack init and app's first cycle: - ////jack.sync_lead(*sync_lead, |mut state|{ - ////let clock = app.write().unwrap().clock(); - ////clock.playhead.update_from_sample(state.position.frame() as f64); - ////state.position.bbt = Some(clock.bbt()); - ////state.position - ////})?; - ////jack.sync_follow(*sync_follow)?; - //// FIXME: They don't work properly. - //Ok(app) - //})?)? - //} - #[cfg(feature = "cli")] pub mod cli { use crate::*; @@ -157,7 +174,7 @@ fn run_new_plain (config: Config) -> Usually<()> { #[arg(short='n', long)] name: Option, /// Whether to attempt to become transport master #[arg(short='Y', long, default_value_t = false)] sync_lead: bool, - /// Whether to sync to external transport master + /// Whether to sync to external transport master #[arg(short='y', long, default_value_t = true)] sync_follow: bool, /// Initial tempo in beats per minute #[arg(short='b', long, default_value = None)] bpm: Option, @@ -221,8 +238,14 @@ fn run_new_plain (config: Config) -> Usually<()> { .chain( connect_audio_outs(&jack, &"R".to_string(), &right_to, None)?.into_iter() )); + //&jack, Clock::new(&jack, *bpm)?, &lf, <, &rf, &rt, &mf, &mt, &mfr, &mtr)?; proj.tracks_add_many(tracks.unwrap_or(0), None, [].into(), [].into())?; proj.scenes_add_many(scenes.unwrap_or(0))?; + //if matches!(self, Action::Status) { + //// Show status and exit + //tek_print_status(&proj); + //return Ok(()) + //} Ok(proj) } } @@ -302,14 +325,11 @@ mod config { } impl Config { - /// Default configuration directory. const CONFIG_DIR: &'static str = "tek"; - /// Default configuration subdirectory. const CONFIG_SUB: &'static str = "v0"; - /// Default configuration file name. const CONFIG: &'static str = "tek.edn"; - /// Default configuration contents. const DEFAULTS: &'static str = include_str!("tek.edn"); + /// Create a new app configuration from a set of XDG base directories, pub fn new (dirs: Option) -> Self { Self { @@ -320,20 +340,20 @@ mod config { ..Default::default() } } - /// Create, initialize, and watch a new configuration. + pub fn watched (callback: impl FnOnce(Arc)->T) -> Usually { let config = Self::init_new(None)?; Self::watch(config.clone(), None)?; let result = callback(config); Ok(result) } - /// Create and initialize a new configuration. + pub fn init_new (_dirs: Option) -> Usually> { let config = Arc::new(Self::new(None)); config.init()?; Ok(config) } - /// Watch a config's file for changes. + pub fn watch (config: Arc, poll: Option) -> Usually<()> { let handler = { let config = config.clone(); @@ -363,18 +383,19 @@ mod config { Err(format!("no config path").into()) } } - /// Find the config file. + fn find_file (&self) -> Option { self.dirs.find_config_file(Self::CONFIG) } - /// Place config file in default location. + fn place_file (&self) -> Result { self.dirs.place_config_file(Self::CONFIG) } - /// Get path to config file. + fn get_file (&self) -> Option { self.dirs.get_config_file(Self::CONFIG) } + /// Write initial contents of configuration. pub fn init (&self) -> Usually<()> { //println!("\r\ninit {}", quanta::Clock::new().raw()); @@ -384,9 +405,13 @@ mod config { Ok(()) }) } + /// Write initial contents of a configuration file. - pub fn load Usually<()>> ( - &self, path: &str, defaults: &str, mut each: F + pub fn load ( + &self, + path: &str, + defaults: &str, + mut each: impl FnMut(&Self, &str)->Usually<()> ) -> Usually<()> { self.stamp.store(quanta::Clock::new().raw(), Relaxed); if self.find_file().is_none() { @@ -401,18 +426,19 @@ mod config { return Err(format!("{path}: not found").into()) }) } + /// Add statements to configuration from [Dsl] source. pub fn add (&self, dsl: impl Language) -> Usually<&Self> { dsl.each(|item|self.add_one(item))?; Ok(self) } - /// Make this configuration empty. + fn clear (&self) { *self.modes.0.write().unwrap() = Default::default(); *self.views.write().unwrap() = Default::default(); *self.binds.write().unwrap() = Default::default(); } - /// Add one entry to the configuration. + fn add_one (&self, item: impl Language) -> Usually<()> { if let Some(expr) = item.expr()? { let head = expr.head()?; @@ -432,35 +458,74 @@ mod config { return Err(format!("Config::load: expected expr, got: {item:?}").into()) } } - /// Get a mode by name. + pub fn get_mode (&self, mode: impl AsRef) -> Option>>> { self.modes.get(mode) } - /// Print the configuration. + pub fn print (&self) { - print_config(self) + use ::ansi_term::Color::*; + println!("{:?}", self.dirs); + for (k, v) in self.views.read().unwrap().iter() { + println!("{} {} {v}", Green.paint("VIEW"), Green.bold().paint(format!("{k:<16}"))); + } + for (k, v) in self.binds.read().unwrap().iter() { + println!("{} {}", Green.paint("BIND"), Green.bold().paint(format!("{k:<16}"))); + for (k, v) in v.0.iter() { + print!("{} ", &Yellow.paint(match &k.0 { + Event::Key(KeyEvent { modifiers, .. }) => + format!("{:>16}", format!("{modifiers}")), + _ => unimplemented!() + })); + print!("{}", &Yellow.bold().paint(match &k.0 { + Event::Key(KeyEvent { code, .. }) => + format!("{:<10}", format!("{code}")), + _ => unimplemented!() + })); + for v in v.iter() { + print!(" => {:?}", v.commands); + print!(" {}", v.condition.as_ref().map(|x|format!("{x:?}")).unwrap_or_default()); + println!(" {}", v.description.as_ref().map(|x|x.as_ref()).unwrap_or_default()); + //println!(" {:?}", v.source); + } + } + } + self.modes.for_each(|k, v|{ + println!(); + for v in v.name.iter() { print!("{}", Green.bold().paint(format!("{v} "))); } + for v in v.info.iter() { print!("\n{}", Green.paint(format!("{v}"))); } + print!("\n{} {}", Blue.paint("TOOL"), Green.bold().paint(format!("{k:<16}"))); + print!("\n{}", Blue.paint("KEYS")); + for v in v.keys.iter() { print!("{}", Green.paint(format!(" {v}"))); } + println!(); + v.modes.for_each(|k, v|{ + print!("{} {} {:?}", Blue.paint("MODE"), Green.bold().paint(format!("{k:<16}")), v.name); + print!( " INFO={:?}", v.info); + print!( " VIEW={:?}", v.view); + println!(" KEYS={:?}", v.keys); + }); + print!("{}", Blue.paint("VIEW")); + for v in v.view.iter() { print!("{}", Green.paint(format!(" {v}"))); } + println!(); + }); } } impl Modes { - /// Register a mode. pub fn add (&self, name: &impl AsRef, body: &impl Language) -> Usually<()> { let mut mode = Mode::default(); body.each(|item|mode.add(item))?; self.0.write().unwrap().insert(name.as_ref().into(), Arc::new(mode)); Ok(()) } - /// Get a mode by name. pub fn get (&self, name: impl AsRef) -> Option>>> { self.0.read().unwrap().get(name.as_ref()).cloned() } - /// Run something for each mode. pub fn for_each (&self, mut ator: impl FnMut(&str, &Mode>)->T) { for (k, v) in self.0.read().unwrap().iter() { let _ = ator(k.as_ref(), v.as_ref()); } } - /// Count modes. pub fn len (&self) -> usize { self.0.read().unwrap().len() } @@ -497,24 +562,34 @@ mod config { } else { return Err(format!("Mode::add: unexpected: {dsl:?}").into()); }) + + //DslParse(dsl, ||Err(format!("Mode::add: unexpected: {dsl:?}").into())) + //.word(|word|self.add_view(word)) + //.expr(|expr|expr.head(|head|{ + ////println!("Mode::add: {head} {:?}", expr.tail()); + //let tail = expr.tail()?.map(|x|x.trim()).unwrap_or(""); + //match head { + //"name" => self.add_name(tail), + //"info" => self.add_info(tail), + //"keys" => self.add_keys(tail)?, + //"mode" => self.add_mode(tail)?, + //_ => self.add_view(tail), + //}; + //})) } - /// Add a name to the mode. + fn add_name (&mut self, dsl: impl Language) -> Perhaps<()> { Ok(dsl.src()?.map(|src|self.name.push(src.into()))) } - /// Add a description to the mode. fn add_info (&mut self, dsl: impl Language) -> Perhaps<()> { Ok(dsl.src()?.map(|src|self.info.push(src.into()))) } - /// Add a view definition to the mode. fn add_view (&mut self, dsl: impl Language) -> Perhaps<()> { Ok(dsl.src()?.map(|src|self.view.push(src.into()))) } - /// Add a keyboard input bindin to the mode. fn add_keys (&mut self, dsl: impl Language) -> Perhaps<()> { Ok(Some(dsl.each(|expr|{ self.keys.push(expr.trim().into()); Ok(()) })?)) } - /// Add a submode to the mode. fn add_mode (&mut self, dsl: impl Language) -> Perhaps<()> { Ok(Some(if let Some(id) = dsl.head()? { self.modes.add(&id, &dsl.tail())?; @@ -564,27 +639,27 @@ mod app { /// let _ = app.project.h_scenes(); /// ``` #[derive(Default, Debug)] - #[namespace(MidiClip)] - #[namespace(ItemColor)] + #[namespace(Arc<[Connect]> App::get_arc_array_connect)] + #[namespace(Arc App::get_arc_str)] + #[namespace(Color App::get_color)] + #[namespace(ControlAxis App::get_axis)] + #[namespace(Dialog App::get_dialog)] #[namespace(ItemTheme)] + #[namespace(Option>> App::get_clip)] + #[namespace(Option> App::get_opt_arc_array_connect)] + #[namespace(Option> App::get_opt_arc_str)] + #[namespace(Option App::get_opt_itemtheme)] + #[namespace(Option>>>> App::get_opt_vec_opt_arc_rwlock_midiclip)] + #[namespace(Option App::get_opt_u16)] + #[namespace(Option App::get_opt_u7)] + #[namespace(Option App::get_opt_usize)] + #[namespace(Option App::get_opt_bool)] + #[namespace(Selection App::get_selection)] + #[namespace(bool App::get_bool)] + #[namespace(u16 App::get_u16)] #[namespace(isize)] #[namespace(u8)] - #[namespace(Arc<[Connect]> get_arc_array_connect)] - #[namespace(Arc get_arc_str)] - #[namespace(Color get_color)] - #[namespace(ControlAxis get_axis)] - #[namespace(Option> get_opt_arc_array_connect)] - #[namespace(Option> get_opt_arc_str)] - #[namespace(Option get_opt_itemtheme)] - #[namespace(Option>>>> get_opt_vec_opt_arc_rwlock_midiclip)] - #[namespace(Option get_opt_u16)] - #[namespace(Option get_opt_usize)] - #[namespace(Option get_opt_bool)] - #[namespace(bool get_bool)] - #[namespace(u16 get_u16)] - #[namespace(PathBuf get_path_buf)] - #[namespace(usize get_usize)] - #[namespace(Dialog App::get_dialog)] + #[namespace(usize App::get_usize)] pub struct App { /// Exit flag pub exit: Exit, @@ -623,7 +698,10 @@ mod app { /// let tek = tek::App::new(None, proj, conf, "hello"); /// ``` pub fn new ( - exit: Option, project: Arrangement, config: Arc, mode: impl AsRef + exit: Option, + project: Arrangement, + config: Arc, + mode: impl AsRef ) -> Self { App { exit: exit.unwrap_or_default(), @@ -636,144 +714,153 @@ mod app { ..Default::default() } } - } - fn get_arc_str (_: &App, src: impl Language) -> Perhaps> { - Ok(src.src()?.map(|x|x.into())) - } - fn get_u16 (state: &App, src: impl Language) -> Perhaps { - Ok(Some(match src.word()? { - Some(":w/sidebar") => state.project.w_sidebar(state.editor().is_some()), - Some(":h/sample-detail") => 6.max(state.size.h() as u16 * 3 / 9), - _ => return try_to_u16(src) - })) - } - fn get_usize (state: &App, src: impl Language) -> Perhaps { - Ok(Some(match src.word()? { - Some(":scene-count") => state.scenes().len(), - Some(":track-count") => state.tracks().len(), - Some(":device-kind") => state.dialog.device_kind().unwrap_or(0), - Some(":device-kind/next") => state.dialog.device_kind_next().unwrap_or(0), - Some(":device-kind/prev") => state.dialog.device_kind_prev().unwrap_or(0), - _ => return try_to_usize(src) - })) - } - fn get_bool (state: &App, src: impl Language) -> Perhaps { - src.word()?.map(|word|Ok(match word { - "Y" => true, - "N" => false, - ":mode/editor" => state.project.editor.is_some(), - ":focused/dialog" => !matches!(state.dialog, Dialog::None), - ":focused/message" => matches!(state.dialog, Dialog::Message(..)), - ":focused/add_device" => matches!(state.dialog, Dialog::Device(..)), - ":focused/browser" => state.dialog.browser().is_some(), - ":focused/pool/import" => matches!(state.pool.mode, Some(PoolMode::Import(..))), - ":focused/pool/export" => matches!(state.pool.mode, Some(PoolMode::Export(..))), - ":focused/pool/rename" => matches!(state.pool.mode, Some(PoolMode::Rename(..))), - ":focused/pool/length" => matches!(state.pool.mode, Some(PoolMode::Length(..))), - ":focused/clip" => !state.editor_focused() && matches!(state.selection(), Selection::TrackClip{..}), - ":focused/track" => !state.editor_focused() && matches!(state.selection(), Selection::Track(..)), - ":focused/scene" => !state.editor_focused() && matches!(state.selection(), Selection::Scene(..)), - ":focused/mix" => !state.editor_focused() && matches!(state.selection(), Selection::Mix), - _ => return Err(format!("not bool: {word}").into()) - })).transpose() - } - #[allow(unused)] - fn get_selection (state: &App, src: impl Language) -> Perhaps { - src.word()?.map(|word|Ok(match word { - ":select/scene" => state.selection().select_scene(state.tracks().len()), - ":select/scene/next" => state.selection().select_scene_next(state.scenes().len()), - ":select/scene/prev" => state.selection().select_scene_prev(), - ":select/track" => state.selection().select_track(state.tracks().len()), - ":select/track/next" => state.selection().select_track_next(state.tracks().len()), - ":select/track/prev" => state.selection().select_track_prev(), - _ => return Err(format!("not selection: {word}").into()) - })).transpose() - } - fn get_color (_: &App, src: impl Language) -> Perhaps { - if let Some(expr) = src.expr()? { - match (expr.head()?, expr.tail()?) { - (Some("g"), Some(tail)) => { - let n = try_to_u8(tail.head().map_err(Into::into))? - .ok_or(LanguageError::Domain("not gray"))?; - Ok(Some(Color::Rgb(n, n, n))) - }, - (Some("rgb"), Some(tail)) => { - let r = try_to_u8(tail.head().map_err(Into::into))? - .ok_or(LanguageError::Domain("not red"))?; - let g = try_to_u8(tail.tail().head().map_err(Into::into))? - .ok_or(LanguageError::Domain("not green"))?; - let b = try_to_u8(tail.tail().tail().head().map_err(Into::into))? - .ok_or(LanguageError::Domain("not blue"))?; - Ok(Some(Color::Rgb(r, g, b))) - }, - (Some(_), _) => return Err(format!("not a color expression: {expr}").into()), - (None, _) => return Err(format!("not a color expression: {expr}").into()), - } - } else if let Ok(Some(sym)) = src.word() { - Ok(match sym { - ":color/bg" => Some(Color::Rgb(28, 32, 36)), - ":color/fg" => Some(Color::Rgb(98, 92, 96)), - _ => return Err(format!("not a color: {sym}").into()) - }) - } else { - return Err(format!("not a color: {:?}", src.src()?).into()) + fn get_arc_str (&self, src: impl Language) -> Perhaps> { + Ok(src.src()?.map(|x|x.into())) } - } - #[allow(unused)] - fn get_opt_u7 (state: &App, src: impl Language) -> Perhaps> { - src.word()?.map(|word|Ok(match word { - ":editor/pitch" => Some(( - state.editor().as_ref().map(|e|e.get_note_pos()).unwrap() as u8 - ).into()), - _ => return Err(format!("unknown midi note: {word}").into()) - })).transpose() - } - fn get_opt_usize (state: &App, src: impl Language) -> Perhaps> { - src.word()?.map(|word|Ok(match word { - ":selected/scene" => state.selection().scene(), - ":selected/track" => state.selection().track(), - _ => return Err(format!("unknown opt: {word}").into()) - })).transpose() - } - #[allow(unused)] - fn get_clip (state: &App, src: impl Language) -> Perhaps>>> { - src.word()?.map(|word|Ok(match word { - ":selected/clip" if let Selection::TrackClip { track, scene } = state.selection() => - state.scenes()[*scene].clips[*track].clone(), - _ => return Err(format!("not a clip: {word}").into()) - })).transpose() - } - fn get_axis (_: &App, src: impl Language) -> Perhaps { - Ok(src.word()?.map(|word|Ok(match word { - ":x" => ControlAxis::X, - ":y" => ControlAxis::Y, - _ => return Err(format!("unknown axis {word}")) - })).transpose()?) - } - fn get_opt_u16 (_: &App, _src: impl Language) -> Perhaps> { - Ok(None) - } - fn get_opt_itemtheme (_: &App, _: impl Language) -> Perhaps> { - todo!() - } - fn get_opt_vec_opt_arc_rwlock_midiclip (_: &App, _: impl Language) -> Perhaps>>>>> { - todo!() - } - fn get_opt_arc_array_connect (_: &App, _: impl Language) -> Perhaps>> { - todo!() - } - fn get_arc_array_connect (_: &App, _: impl Language) -> Perhaps> { - todo!() - } - fn get_opt_arc_str (_: &App, _: impl Language) -> Perhaps>> { - todo!() - } - fn get_opt_bool (_: &App, _: impl Language) -> Perhaps> { - todo!() - } - fn get_path_buf (_: &App, _: impl Language) -> Perhaps { - todo!() + + fn get_u16 (&self, src: impl Language) -> Perhaps { + Ok(Some(match src.word()? { + Some(":w/sidebar") => self.project.w_sidebar(self.editor().is_some()), + Some(":h/sample-detail") => 6.max(self.size.h() as u16 * 3 / 9), + _ => return try_to_u16(src) + })) + } + + fn get_usize (&self, src: impl Language) -> Perhaps { + Ok(Some(match src.word()? { + Some(":scene-count") => self.scenes().len(), + Some(":track-count") => self.tracks().len(), + Some(":device-kind") => self.dialog.device_kind().unwrap_or(0), + Some(":device-kind/next") => self.dialog.device_kind_next().unwrap_or(0), + Some(":device-kind/prev") => self.dialog.device_kind_prev().unwrap_or(0), + _ => return try_to_usize(src) + })) + } + + fn get_bool (&self, src: impl Language) -> Perhaps { + src.word()?.map(|word|Ok(match word { + "Y" => true, + "N" => false, + ":mode/editor" => self.project.editor.is_some(), + ":focused/dialog" => !matches!(self.dialog, Dialog::None), + ":focused/message" => matches!(self.dialog, Dialog::Message(..)), + ":focused/add_device" => matches!(self.dialog, Dialog::Device(..)), + ":focused/browser" => self.dialog.browser().is_some(), + ":focused/pool/import" => matches!(self.pool.mode, Some(PoolMode::Import(..))), + ":focused/pool/export" => matches!(self.pool.mode, Some(PoolMode::Export(..))), + ":focused/pool/rename" => matches!(self.pool.mode, Some(PoolMode::Rename(..))), + ":focused/pool/length" => matches!(self.pool.mode, Some(PoolMode::Length(..))), + ":focused/clip" => !self.editor_focused() && matches!(self.selection(), Selection::TrackClip{..}), + ":focused/track" => !self.editor_focused() && matches!(self.selection(), Selection::Track(..)), + ":focused/scene" => !self.editor_focused() && matches!(self.selection(), Selection::Scene(..)), + ":focused/mix" => !self.editor_focused() && matches!(self.selection(), Selection::Mix), + _ => return Err(format!("not bool: {word}").into()) + })).transpose() + } + + fn get_selection (&self, src: impl Language) -> Perhaps { + src.word()?.map(|word|Ok(match word { + ":select/scene" => self.selection().select_scene(self.tracks().len()), + ":select/scene/next" => self.selection().select_scene_next(self.scenes().len()), + ":select/scene/prev" => self.selection().select_scene_prev(), + ":select/track" => self.selection().select_track(self.tracks().len()), + ":select/track/next" => self.selection().select_track_next(self.tracks().len()), + ":select/track/prev" => self.selection().select_track_prev(), + _ => return Err(format!("not selection: {word}").into()) + })).transpose() + } + + fn get_color (&self, src: impl Language) -> Perhaps { + if let Some(expr) = src.expr()? { + match (expr.head()?, expr.tail()?) { + (Some("g"), Some(tail)) => { + let n = try_to_u8(tail.head().map_err(Into::into))? + .ok_or(LanguageError::Domain("not gray"))?; + Ok(Some(Color::Rgb(n, n, n))) + }, + (Some("rgb"), Some(tail)) => { + let r = try_to_u8(tail.head().map_err(Into::into))? + .ok_or(LanguageError::Domain("not red"))?; + let g = try_to_u8(tail.tail().head().map_err(Into::into))? + .ok_or(LanguageError::Domain("not green"))?; + let b = try_to_u8(tail.tail().tail().head().map_err(Into::into))? + .ok_or(LanguageError::Domain("not blue"))?; + Ok(Some(Color::Rgb(r, g, b))) + }, + (Some(_), _) => return Err(format!("not a color expression: {expr}").into()), + (None, _) => return Err(format!("not a color expression: {expr}").into()), + } + } else if let Ok(Some(sym)) = src.word() { + Ok(match sym { + ":color/bg" => Some(Color::Rgb(28, 32, 36)), + ":color/fg" => Some(Color::Rgb(98, 92, 96)), + _ => return Err(format!("not a color: {sym}").into()) + }) + } else { + return Err(format!("not a color: {:?}", src.src()?).into()) + } + } + + fn get_opt_u7 (&self, src: impl Language) -> Perhaps> { + src.word()?.map(|word|Ok(match word { + ":editor/pitch" => Some(( + self.editor().as_ref().map(|e|e.get_note_pos()).unwrap() as u8 + ).into()), + _ => return Err(format!("unknown midi note: {word}").into()) + })).transpose() + } + + fn get_opt_u16 (&self, _src: impl Language) -> Perhaps> { + Ok(None) + } + + fn get_opt_usize (&self, src: impl Language) -> Perhaps> { + src.word()?.map(|word|Ok(match word { + ":selected/scene" => self.selection().scene(), + ":selected/track" => self.selection().track(), + _ => return Err(format!("unknown opt: {word}").into()) + })).transpose() + } + + fn get_clip (&self, src: impl Language) -> Perhaps>>> { + src.word()?.map(|word|Ok(match word { + ":selected/clip" if let Selection::TrackClip { track, scene } = self.selection() => + self.scenes()[*scene].clips[*track].clone(), + _ => return Err(format!("not a clip: {word}").into()) + })).transpose() + } + + fn get_axis (&self, src: impl Language) -> Perhaps { + Ok(src.word()?.map(|word|Ok(match word { + ":x" => ControlAxis::X, + ":y" => ControlAxis::Y, + _ => return Err(format!("unknown axis {word}")) + })).transpose()?) + } + + fn get_opt_itemtheme (&self, src: impl Language) -> Perhaps> { + todo!() + } + fn get_opt_vec_opt_arc_rwlock_midiclip (&self, src: impl Language) -> Perhaps>>>>> { + todo!() + } + fn get_opt_arc_array_connect (&self, src: impl Language) -> Perhaps>> { + todo!() + } + + fn get_arc_array_connect (&self, src: impl Language) -> Perhaps> { + todo!() + } + + fn get_opt_arc_str (&self, src: impl Language) -> Perhaps>> { + todo!() + } + + fn get_opt_bool (&self, src: impl Language) -> Perhaps> { + todo!() + } + } } @@ -820,20 +907,27 @@ mod bind { /// let condition = tek::Condition(std::sync::Arc::new(Box::new(||{true}))); /// ``` #[derive(Clone)] - pub struct Condition(pub Arcbool + Send + Sync>>); + pub struct Condition( + pub Arcbool + Send + Sync>> + ); + + struct AppDispatch; tui_keys!(self: App, input { - let name = self.mode.as_ref(); - let mode = name.and_then(|m|self.config.get_mode(m)).as_ref().map(Arc::clone); - if let Some(mode) = mode { + + if let Some(mode) = self.mode.as_ref().and_then( + |m|self.config.get_mode(m) + ).as_ref().map(Arc::clone) { let binds = self.config.binds.clone(); for id in mode.keys.iter() { if let Some(event_map) = binds.read().unwrap().get(id.as_ref()) && let Some(bindings) = event_map.query(input) { for binding in bindings { for command in binding.commands.iter() { - if Self::command::(self, command)? { - break + for namespace in App::commands() { + if namespace(self, command)? { + break + } } } } @@ -843,7 +937,45 @@ mod bind { Ok(()) }); + #[derive(Clone, Debug)] + pub enum AppCommand { + Axis(AxisCommand), + Browse(BrowseCommand), + Dialog(DialogCommand), + MidiClip(MidiClipCommand), + Pool(PoolCommand), + Scene(SceneCommand), + Scenes(ScenesCommand), + Track(TrackCommand), + Tracks(TracksCommand), + } + + impl_from!(AppCommand: |x: AxisCommand| AppCommand::Axis(x)); + impl_from!(AppCommand: |x: BrowseCommand| AppCommand::Browse(x)); + impl_from!(AppCommand: |x: DialogCommand| AppCommand::Dialog(x)); + impl_from!(AppCommand: |x: MidiClipCommand| AppCommand::MidiClip(x)); + impl_from!(AppCommand: |x: PoolCommand| AppCommand::Pool(x)); + impl_from!(AppCommand: |x: SceneCommand| AppCommand::Scene(x)); + impl_from!(AppCommand: |x: ScenesCommand| AppCommand::Scenes(x)); + impl_from!(AppCommand: |x: TrackCommand| AppCommand::Track(x)); + impl_from!(AppCommand: |x: TracksCommand| AppCommand::Tracks(x)); + impl App { + fn commands () -> impl Iterator Usually> { + [ + Self::command::, + //Self::command::, + Self::command::, + //Self::command::, + //Self::command::, + Self::command::, + Self::command::, + Self::command::, + Self::command::, + ] + .into_iter() + } + fn command < T: Dispatch + Clone + Into > (state: &mut Self, src: &str) -> Usually where @@ -865,55 +997,6 @@ mod bind { } } - impl_from!(AppCommand: |x: AxisCommand| AppCommand::Axis { command: x }); - impl_from!(AppCommand: |x: BrowseCommand| AppCommand::Browse { command: x }); - impl_from!(AppCommand: |x: DialogCommand| AppCommand::Dialog { command: x }); - impl_from!(AppCommand: |x: MidiClipCommand| AppCommand::MidiClip { command: x }); - impl_from!(AppCommand: |x: PoolCommand| AppCommand::Pool { command: x }); - impl_from!(AppCommand: |x: SceneCommand| AppCommand::Scene { command: x }); - impl_from!(AppCommand: |x: ScenesCommand| AppCommand::Scenes { command: x }); - impl_from!(AppCommand: |x: TrackCommand| AppCommand::Track { command: x }); - impl_from!(AppCommand: |x: TracksCommand| AppCommand::Tracks { command: x }); - #[tek_proc::commands(AppCommand)] - impl App { - #[command(Axis = "axis")] - pub fn command_axis (&mut self, command: AxisCommand) -> Perhaps { - Ok(command.dispatch(self)?.map(Into::into)) - } - #[command(Browse = "browse")] - pub fn command_browse (&mut self, command: BrowseCommand) -> Perhaps { - Ok(command.dispatch(self)?.map(Into::into)) - } - #[command(Dialog = "dialog")] - pub fn command_dialog (&mut self, command: DialogCommand) -> Perhaps { - Ok(command.dispatch(self)?.map(Into::into)) - } - #[command(MidiClip = "clip")] - pub fn command_midi_clip (&mut self, command: MidiClipCommand) -> Perhaps { - Ok(command.dispatch(self)?.map(Into::into)) - } - #[command(Pool = "pool")] - pub fn command_pool (&mut self, command: PoolCommand) -> Perhaps { - Ok(command.dispatch(self)?.map(Into::into)) - } - #[command(Scene = "scene")] - pub fn command_scene (&mut self, command: SceneCommand) -> Perhaps { - Ok(command.dispatch(self)?.map(Into::into)) - } - #[command(Scenes = "scenes")] - pub fn command_scenes (&mut self, command: ScenesCommand) -> Perhaps { - Ok(command.dispatch(self)?.map(Into::into)) - } - #[command(Track = "track")] - pub fn command_track (&mut self, command: TrackCommand) -> Perhaps { - Ok(command.dispatch(self)?.map(Into::into)) - } - #[command(Tracks = "tracks")] - pub fn command_tracks (&mut self, command: TracksCommand) -> Perhaps { - Ok(command.dispatch(self)?.map(Into::into)) - } - } - #[tek_proc::commands(DialogCommand)] impl App { /// Cancel current dialog @@ -938,12 +1021,17 @@ mod bind { /// ``` /// let _ = tek::App::default().set_dialog(tek::Dialog::welcome()); /// ``` - #[command(SetDialog = "seg")] + #[command(SetDialog = "dialog")] pub fn set_dialog (&mut self, dialog: Dialog) -> Perhaps { let mut dialog = dialog.clone(); std::mem::swap(&mut self.dialog, &mut dialog); Ok(Some(DialogCommand::SetDialog { dialog })) } + + //#[command(Tracks = "tracks")] + //pub fn command_tracks (&mut self, command: TracksCommand) -> Perhaps { + //todo!() + //} } #[tek_proc::commands(AxisCommand)] @@ -1294,6 +1382,27 @@ mod device { #[cfg(feature = "plugin")] pub use self::plugin::*; } + //pub fn tui ( + //app: Arc>, + //jack: Jack, + //sync_lead: &bool, + //sync_follow: &bool, + //) -> Usually<()> { + //// Run the [Tui] and [Jack] threads with the [App] state. + //Tui::run_main(&jack.run(move|jack|{ + //// Between jack init and app's first cycle: + ////jack.sync_lead(*sync_lead, |mut state|{ + ////let clock = app.write().unwrap().clock(); + ////clock.playhead.update_from_sample(state.position.frame() as f64); + ////state.position.bbt = Some(clock.bbt()); + ////state.position + ////})?; + ////jack.sync_follow(*sync_follow)?; + //// FIXME: They don't work properly. + //Ok(app) + //})?)? + //} + pub use self::draw::*; mod draw { use crate::*; @@ -1570,53 +1679,6 @@ mod draw { } -pub fn print_config (config: &Config) { - use ::ansi_term::Color::*; - println!("{:?}", config.dirs); - for (k, v) in config.views.read().unwrap().iter() { - println!("{} {} {v}", Green.paint("VIEW"), Green.bold().paint(format!("{k:<16}"))); - } - for (k, v) in config.binds.read().unwrap().iter() { - println!("{} {}", Green.paint("BIND"), Green.bold().paint(format!("{k:<16}"))); - for (k, v) in v.0.iter() { - print!("{} ", &Yellow.paint(match &k.0 { - Event::Key(KeyEvent { modifiers, .. }) => - format!("{:>16}", format!("{modifiers}")), - _ => unimplemented!() - })); - print!("{}", &Yellow.bold().paint(match &k.0 { - Event::Key(KeyEvent { code, .. }) => - format!("{:<10}", format!("{code}")), - _ => unimplemented!() - })); - for v in v.iter() { - print!(" => {:?}", v.commands); - print!(" {}", v.condition.as_ref().map(|x|format!("{x:?}")).unwrap_or_default()); - println!(" {}", v.description.as_ref().map(|x|x.as_ref()).unwrap_or_default()); - //println!(" {:?}", v.source); - } - } - } - config.modes.for_each(|k, v|{ - println!(); - for v in v.name.iter() { print!("{}", Green.bold().paint(format!("{v} "))); } - for v in v.info.iter() { print!("\n{}", Green.paint(format!("{v}"))); } - print!("\n{} {}", Blue.paint("TOOL"), Green.bold().paint(format!("{k:<16}"))); - print!("\n{}", Blue.paint("KEYS")); - for v in v.keys.iter() { print!("{}", Green.paint(format!(" {v}"))); } - println!(); - v.modes.for_each(|k, v|{ - print!("{} {} {:?}", Blue.paint("MODE"), Green.bold().paint(format!("{k:<16}")), v.name); - print!( " INFO={:?}", v.info); - print!( " VIEW={:?}", v.view); - println!(" KEYS={:?}", v.keys); - }); - print!("{}", Blue.paint("VIEW")); - for v in v.view.iter() { print!("{}", Green.paint(format!(" {v}"))); } - println!(); - }); -} - pub fn print_status (project: &Arrangement) { println!("Name: {:?}", &project.name); println!("JACK: {:?}", &project.jack);