add some more functions to -mers and add hooks

This commit is contained in:
Mark 2024-01-20 01:19:19 +01:00
parent b0417a72b6
commit 8620fcd4ec
8 changed files with 1079 additions and 386 deletions

View File

@ -16,7 +16,7 @@ mers_lib = { version = "0.3.2", optional = true }
musicdb-mers = { version = "0.1.0", path = "../musicdb-mers", optional = true }
[features]
default = ["gui"]
default = ["gui", "mers", "merscfg"]
# gui:
# enables the gui mode
# merscfg:

View File

@ -0,0 +1,289 @@
gen_escape_str := str -> {
"echo".run_command(("-ne", str)).try((
(s, stdout, stderr) -> if s.eq(0) stdout else ""
e -> ""
))
}
gen_color := mode -> ("\\x1b[", mode, "m").concat.gen_escape_str
// 0: reset | 1: bold, 2: dim, 3: italic, 4: underline, 9: strikethrough
// Colors: FG/BG=3x/4x (bright, if supported: 9x/10x) | 1red 2green 3yellow 4blue 5magenta 6cyan 7white 9default
clr_reset := "0".gen_color
clr_red := "31".gen_color
clr_green := "32".gen_color
clr_yellow := "33".gen_color
clr_blue := "34".gen_color
clr_magenta := "35".gen_color
clr_cyan := "36".gen_color
clr_white := "37".gen_color
clr_default := "39".gen_color
clr_dim := "2".gen_color
clr_top_bar_decorations := "2;34" // dim blue
clr_top_bar_title := "1;37".gen_color // bold white
clr_top_bar_err := "1;31".gen_color // bold red
clr_top_bar_empty := clr_dim // dim
clr_artist := "35".gen_color // magenta
clr_album := "32".gen_color // green
clr_user_input_line_decor := "2;36".gen_color // dim cyan
clr_user_input_line := "1;36".gen_color // bold cyan
clr_unknown_cmd := "31".gen_color // red
clr_buf_timestamp := "2;34".gen_color // dim blue
clr_search := "1;32".gen_color // dim green
// clear terminal and reset cursor and colors
"\\x1b[2J\\x1b[H\\x1b[0m".gen_escape_str.eprint
// save cursor, reset cursor and color
term_before_redraw := "\\x1b[s\\x1b[H\\x1b[0m".gen_escape_str
// restore cursor pos
term_put_cursor_back := "\\x1b[u".gen_escape_str
// erase rest of line in case previous version of this line was longer than new version, then add newline
term_clear_rest_of_screen := "\\x1b[0J".gen_escape_str
line_end := ("\\x1b[0K".gen_escape_str, clr_reset, "\n").concat
str_repeat := (str, count) -> {
out := ""
().loop(() -> if count.gt(0) {
&count = count.subtract(1)
&out = (out, str).concat
} else (()))
out
}
current_queue_index := [List<Int>] ().as_list
reset_cursor_pos := true
term_buf_len := 10
term_buf := ("Welcome! Type `help` for help.").as_list
bprintln := line -> {
time := "date".run_command(("+%T")).try((
(s, stdout, stderr) -> if s.eq(0) stdout.trim else ""
e -> ""
))
&term_buf.push((clr_buf_timestamp, time, " | ", clr_reset, line).concat)
// remove start of term_buf if it gets too long
if term_buf.len.gt(term_buf_len.product(2)) {
&term_buf = term_buf.enumerate.filter_map((i, v) -> if i.lt(term_buf_len) () else (v)).as_list
}
}
custom_input_handler := [()/(String, String, List<String>)] ()
update_ui := () -> {
screen := term_before_redraw
sprintln := line -> &screen = (screen, line, line_end).concat
// top bar
(top_bar_line_1, top_bar_line_2) := ().queue_get_current_song.try((
() -> ((clr_top_bar_empty, "[-]").concat, "")
id -> id.get_song.try((
() -> ((clr_top_bar_err, "[!]").concat, "")
{id: _, title: title, album: album, artist: artist, cover: _} -> {
l1 := (clr_top_bar_title, title).concat
artist := artist.get_artist
album := album.try_allow_unused((
() -> ()
id -> album.get_album
))
l2 := (artist, album).try_allow_unused((
((), ()) -> ""
({id: _, name: artist_name, cover: _, albums: _, singles: _}, ()) -> (clr_dim, "by ", clr_artist, artist_name).concat
((), {id: _, name: album_name, artist: _, cover: _, songs: _}) -> (clr_dim, "on ", clr_album, album_name).concat
({id: _, name: artist_name, cover: _, albums: _, singles: _}, {id: _, name: album_name, artist: _, cover: _, songs: _}) -> (clr_dim, "by ", clr_reset, clr_artist, artist_name, clr_reset, clr_dim, " on ", clr_reset, clr_album, album_name).concat
))
(l1, l2)
}
))
))
(if ().get_playing "⏵" else "⏸", " ", top_bar_line_1).concat.sprintln
top_bar_line_2.sprintln
// term buf
{
buf_ln := 0
().loop(() -> if buf_ln.lt(term_buf_len){
term_buf.get(term_buf.len.subtract(term_buf_len).sum(buf_ln)).try((
() -> "".sprintln
(line) -> line.sprintln
))
&buf_ln = buf_ln.sum(1)
} else (()))
}
// user input line
user_input_line := (clr_user_input_line_decor, custom_input_handler.try((() -> if current_queue_index.len.gt(0) ">> " else " > ", (_, v, _) -> v)), clr_user_input_line).concat
// print screen
(
screen, user_input_line,
if reset_cursor_pos term_clear_rest_of_screen else term_put_cursor_back
).concat.eprint
&reset_cursor_pos = false
}
if false ().update_ui // check
show_queue_recursive := max_depth_layers -> {
stack := ((current_queue_index, -1, false)).as_list
().loop(() -> {
&stack.pop.try((
() -> (())
((index, depth, show)) -> if (max_depth_layers.eq(0), depth.lt(max_depth_layers)).any {
index := [List<Int>] index
line := if show {
o := ""
i := 0
().loop(() -> if i.lt(depth) { &o = (o, " ").concat, &i = i.sum(1) } else (()))
o
} else ""
if show &line = (clr_dim, line).concat
index.queue_get_elem.try((
() -> ()
{enabled: _, song: id} -> id.get_song.try((
() -> &line = (line, clr_reset, "[!] ", id).concat
{id: _, title: title, album: _, artist: _, cover: _} -> &line = (line, clr_reset, title).concat
))
{enabled: _, loop: {total: total, done: done}} -> {
index := index
&index.push(0)
&stack.push((index, depth, false))
&line = (line, "Loop").concat
}
{enabled: _, random: ()} -> {
&line = (line, "Random").concat
index := index
&index.push(0)
&stack.push((index, depth.sum(1), true))
}
{enabled: _, shuffle: ()} -> {
&line = (line, "Shuffle").concat
index := index
&index.push(0)
&stack.push((index, depth, false))
}
{enabled: _, folder: {index: ix, length: length, name: name}} -> {
&line = (line, "[", name, "]").concat
i := length
().loop(() -> {
&i = i.subtract(1)
if i.lt(0) (()) else {
index := index
&index.push(i)
&stack.push((index, depth.sum(1), true))
}
})
}
))
if show line.bprintln
}
))
})
}
// handlers
on_resume := () -> ().update_ui
on_pause := () -> ().update_ui
on_next_song := () -> ().update_ui
on_library_changed := () -> ().update_ui
on_queue_changed := () -> ().update_ui
on_notification_received := (title, content) -> {
("Notification: ", title).concat.bprintln
content.bprintln
().update_ui
}
// add as handlers
on_resume.handle_event_resume
on_pause.handle_event_pause
on_next_song.handle_event_next_song
on_library_changed.handle_event_library_changed
on_queue_changed.handle_event_queue_changed
on_notification_received.handle_event_notification_received
&found_songs := [List<MusicDbId>] ().as_list
().loop(() -> {
().update_ui
line := ().read_line
exit := line.len.eq(0)
line := line.trim
&reset_cursor_pos = true
custom_input_handler.try((
() -> {
if (exit, line.eq("exit")).any {
(())
} else if line.eq("help") {
"===== Help =====".bprintln
"Commands:".bprintln
"- exit, pause, play".bprintln
"- next, search, add song, clear queue, queue random, queue show".bprintln
"- send notif, set buf len, clear".bprintln
} else if line.eq("pause") {
().pause
} else if line.eq("play") {
().resume
} else if line.eq("next") {
().next_song
} else if line.eq("clear") {
&term_buf = ().as_list
} else if line.eq("queue show") {
"== Queue ==".bprintln
0.show_queue_recursive
} else if line.eq("clear queue") {
().queue_clear
} else if line.eq("queue random") {
().queue_clear
(().as_list, 0).queue_add_loop
(0, 0).as_list.queue_add_random
} else if line.eq("send notif") {
&custom_input_handler = ("SN", "Notification: ", ().as_list)
} else if line.eq("set buf len") {
&custom_input_handler = ("SetBufLen", "Length (lines): ", ().as_list)
} else if line.eq("search") {
&custom_input_handler = ("Search", "Song: ", ().as_list)
} else if line.eq("add song") {
if found_songs.len.gt(0) {
&custom_input_handler = ("AddSong", ("(1-", found_songs.len, ") ").concat, ().as_list)
} else {
"Use `search` to find songs first!".bprintln
}
} else if line.len.gt(0) {
(clr_unknown_cmd, "Unknown command: ", line).concat.bprintln
}
}
(id, desc, args) -> {
&custom_input_handler = ()
args := [List<String>] args
if id.eq("SN") {
line.send_server_notification
} else if id.eq("SetBufLen") {
line.parse_int.try((
() -> (clr_unknown_cmd, "not an int").concat.bprintln
n -> &term_buf_len = n,
))
} else if id.eq("Search") {
songs := ().all_songs.filter_map({id: id, title: title, album: _, artist: _, cover: _} -> title.index_of(line).try((() -> (), n -> ((id, title))))).take(term_buf_len)
if (songs.len.gt(0), songs.len.eq(term_buf_len)).all {
&songs = songs.take(term_buf_len.subtract(1))
(clr_search, "Search: ", clr_reset, line, clr_dim, " (found more results than will be displayed)").concat.bprintln
} else {
(clr_search, "Search: ", clr_reset, line).concat.bprintln
}
&found_songs = songs.map((id, _) -> id).as_list
songs.enumerate.for_each((i, (_, song)) -> (clr_dim, i.sum(1), " ", clr_reset, song).concat.bprintln)
} else if id.eq("AddSong") {
line.parse_int.try((
() -> (clr_unknown_cmd, "not an int (must be 1-", found_songs.len, ")").concat.bprintln
n -> found_songs.get(n.subtract(1)).try((
() -> (clr_unknown_cmd, "out of range (must be 1-", found_songs.len, ")").concat.bprintln
(id) -> (current_queue_index, id).queue_add_song
))
))
} else {
("Unknown CIH ID ", id, ".").concat.eprintln
1.panic
}
}
})
clr_reset.eprintln

View File

@ -0,0 +1,22 @@
// define handlers
on_resume := () -> "Resumed".eprintln
on_pause := () -> "Paused".eprintln
on_next_song := () -> "Next song".eprintln
on_library_changed := () -> "Library changed".eprintln
on_queue_changed := () -> "Queue changed".eprintln
on_notification_received := (title, content) -> ("Notif:\n - ", title, " -\n", content).concat.eprintln
// use handlers to handle events
on_resume.handle_event_resume
on_pause.handle_event_pause
on_next_song.handle_event_next_song
on_library_changed.handle_event_library_changed
on_queue_changed.handle_event_queue_changed
on_notification_received.handle_event_notification_received
// because on_resume won't be called if playback was resumed before this client connected
if ().get_playing {
"Resumed (was playing)".eprintln
}
().loop(() -> 60.sleep)

View File

@ -80,6 +80,7 @@ pub fn main(
connection: TcpStream,
get_con: get::Client<TcpStream>,
event_sender_arc: Arc<Mutex<Option<UserEventSender<GuiEvent>>>>,
after_db_cmd: &Arc<Mutex<Option<Box<dyn FnMut(Command) + Send + Sync + 'static>>>>,
) {
let config_dir = super::get_config_file_path();
let config_file = config_dir.join("config_gui.toml");
@ -262,6 +263,7 @@ pub fn main(
#[cfg(feature = "merscfg")]
merscfg: crate::merscfg::MersCfg::new(config_dir.join("dynamic_config.mers"), database),
},
after_db_cmd,
));
}
@ -320,14 +322,18 @@ impl Gui {
scroll_pages_multiplier: f64,
#[cfg(not(feature = "merscfg"))] gui_config: GuiConfig,
#[cfg(feature = "merscfg")] mut gui_config: GuiConfig,
#[cfg(feature = "merscfg")] after_db_cmd: &Arc<
Mutex<Option<Box<dyn FnMut(Command) + Send + Sync + 'static>>>,
>,
) -> Self {
let (notif_overlay, notif_sender) = NotifOverlay::new();
let notif_sender_two = notif_sender.clone();
#[cfg(feature = "merscfg")]
match gui_config
.merscfg
.load(Arc::clone(&event_sender), notif_sender.clone())
{
match gui_config.merscfg.load(
Arc::clone(&event_sender),
notif_sender.clone(),
after_db_cmd,
) {
Err(e) => {
if !matches!(e.kind(), std::io::ErrorKind::NotFound) {
eprintln!("Couldn't load merscfg: {e}")

View File

@ -105,7 +105,12 @@ fn main() {
Arc::new(Mutex::new(None));
#[cfg(feature = "speedy2d")]
let sender = Arc::clone(&update_gui_sender);
#[cfg(any(feature = "mers", feature = "merscfg"))]
let mers_after_db_updated_action: Arc<
Mutex<Option<Box<dyn FnMut(Command) + Send + Sync + 'static>>>,
> = Arc::new(Mutex::new(None));
let con_thread = {
let mers_after_db_updated_action = Arc::clone(&mers_after_db_updated_action);
let mode = mode.clone();
let database = Arc::clone(&database);
let mut con = con.try_clone().unwrap();
@ -148,7 +153,13 @@ fn main() {
if let Some(player) = &mut player {
player.handle_command(&update);
}
database.lock().unwrap().apply_command(update);
#[cfg(any(feature = "mers", feature = "merscfg"))]
if let Some(action) = &mut *mers_after_db_updated_action.lock().unwrap() {
database.lock().unwrap().apply_command(update.clone());
action(update);
} else {
database.lock().unwrap().apply_command(update);
}
#[cfg(feature = "speedy2d")]
if let Some(v) = &*update_gui_sender.lock().unwrap() {
v.send_event(GuiEvent::Refresh).unwrap();
@ -175,6 +186,8 @@ fn main() {
))
.expect("initializing get client connection"),
sender,
#[cfg(feature = "merscfg")]
&mers_after_db_updated_action,
)
};
}
@ -191,6 +204,7 @@ fn main() {
mers_lib::prelude_compile::Config::new().bundle_std(),
&database,
&Arc::new(move |cmd: Command| cmd.to_bytes(&mut *con.lock().unwrap()).unwrap()),
&mers_after_db_updated_action,
)
.infos();
let program = mers_lib::prelude_compile::parse(&mut src, &srca)

View File

@ -139,13 +139,14 @@ impl MersCfg {
notif_sender: Sender<
Box<dyn FnOnce(&NotifOverlay) -> (Box<dyn GuiElem>, NotifInfo) + Send>,
>,
after_db_cmd: &Arc<Mutex<Option<Box<dyn FnMut(Command) + Send + Sync + 'static>>>>,
) -> mers_lib::prelude_extend_config::Config {
let cmd_es = event_sender.clone();
let cmd_ga = self.channel_gui_actions.0.clone();
musicdb_mers::add(cfg, db, &Arc::new(move |cmd| {
cmd_ga.send(cmd).unwrap();
cmd_es.send_event(GuiEvent::RefreshMers).unwrap();
}))
}), after_db_cmd)
.add_var_arc(
"is_playing".to_owned(),
Arc::clone(&self.var_is_playing),
@ -704,9 +705,10 @@ impl MersCfg {
notif_sender: Sender<
Box<dyn FnOnce(&NotifOverlay) -> (Box<dyn GuiElem>, NotifInfo) + Send>,
>,
after_db_cmd: &Arc<Mutex<Option<Box<dyn FnMut(Command) + Send + Sync + 'static>>>>,
) -> std::io::Result<Result<Result<(), (String, Option<CheckError>)>, CheckError>> {
let src = mers_lib::prelude_compile::Source::new_from_file(self.source_file.clone())?;
Ok(self.load2(src, event_sender, notif_sender))
Ok(self.load2(src, event_sender, notif_sender, after_db_cmd))
}
fn load2(
&mut self,
@ -715,6 +717,7 @@ impl MersCfg {
notif_sender: Sender<
Box<dyn FnOnce(&NotifOverlay) -> (Box<dyn GuiElem>, NotifInfo) + Send>,
>,
after_db_cmd: &Arc<Mutex<Option<Box<dyn FnMut(Command) + Send + Sync + 'static>>>>,
) -> Result<Result<(), (String, Option<CheckError>)>, CheckError> {
let srca = Arc::new(src.clone());
let (mut i1, mut i2, mut i3) = self
@ -723,6 +726,7 @@ impl MersCfg {
&self.database,
event_sender,
notif_sender,
after_db_cmd,
)
.infos();
let compiled = mers_lib::prelude_compile::parse(&mut src, &srca)?

File diff suppressed because it is too large Load Diff