Add ui module and ability to update its state
This commit is contained in:
@@ -4,6 +4,7 @@ import musicplayer/input/input
|
|||||||
import musicplayer/input/key.{type Key}
|
import musicplayer/input/key.{type Key}
|
||||||
import musicplayer/mpv/mpv
|
import musicplayer/mpv/mpv
|
||||||
import musicplayer/musicplayer
|
import musicplayer/musicplayer
|
||||||
|
import musicplayer/ui/ui
|
||||||
|
|
||||||
pub fn main() -> Nil {
|
pub fn main() -> Nil {
|
||||||
let input_keys_name: Name(Key) = process.new_name("input_keys")
|
let input_keys_name: Name(Key) = process.new_name("input_keys")
|
||||||
@@ -15,13 +16,16 @@ pub fn main() -> Nil {
|
|||||||
// inject input
|
// inject input
|
||||||
let input_inject_name: Name(Key) = process.new_name("input_inject_keys")
|
let input_inject_name: Name(Key) = process.new_name("input_inject_keys")
|
||||||
|
|
||||||
|
// TODO should input.new just return the inject_subject?
|
||||||
input.new(input_keys_name, input_stream_name, input_inject_name)
|
input.new(input_keys_name, input_stream_name, input_inject_name)
|
||||||
|
|
||||||
|
let assert Ok(ui) = ui.new()
|
||||||
let assert Ok(mpv) = mpv.new()
|
let assert Ok(mpv) = mpv.new()
|
||||||
|
|
||||||
let exit = process.new_subject()
|
let exit = process.new_subject()
|
||||||
let assert Ok(_) =
|
let assert Ok(_) =
|
||||||
musicplayer.new(
|
musicplayer.new(
|
||||||
|
ui,
|
||||||
mpv,
|
mpv,
|
||||||
input_keys_name,
|
input_keys_name,
|
||||||
input_stream_name,
|
input_stream_name,
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ pub fn new(
|
|||||||
read_input(input_keys, input_stream, input_inject)
|
read_input(input_keys, input_stream, input_inject)
|
||||||
})
|
})
|
||||||
|
|
||||||
echo "waiting for input"
|
|
||||||
key.start_raw_shell()
|
key.start_raw_shell()
|
||||||
Nil
|
Nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,9 +7,11 @@ import gleam/string
|
|||||||
import musicplayer/control.{type Control}
|
import musicplayer/control.{type Control}
|
||||||
import musicplayer/input/key.{type Key}
|
import musicplayer/input/key.{type Key}
|
||||||
import musicplayer/mpv/control as mpv_control
|
import musicplayer/mpv/control as mpv_control
|
||||||
|
import musicplayer/ui/control as ui_control
|
||||||
|
|
||||||
type State(mpv, input_inject, exit) {
|
type State(ui, mpv, input_inject, exit) {
|
||||||
State(
|
State(
|
||||||
|
ui: Subject(ui_control.Control),
|
||||||
mpv: Subject(mpv_control.Control),
|
mpv: Subject(mpv_control.Control),
|
||||||
input_inject: Subject(Key),
|
input_inject: Subject(Key),
|
||||||
exit: Subject(Nil),
|
exit: Subject(Nil),
|
||||||
@@ -17,6 +19,7 @@ type State(mpv, input_inject, exit) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(
|
pub fn new(
|
||||||
|
ui: Subject(ui_control.Control),
|
||||||
mpv: Subject(mpv_control.Control),
|
mpv: Subject(mpv_control.Control),
|
||||||
input_keys_name: Name(Key),
|
input_keys_name: Name(Key),
|
||||||
input_stream_name: Name(List(String)),
|
input_stream_name: Name(List(String)),
|
||||||
@@ -28,7 +31,7 @@ pub fn new(
|
|||||||
let input_inject = process.named_subject(input_inject_name)
|
let input_inject = process.named_subject(input_inject_name)
|
||||||
|
|
||||||
case
|
case
|
||||||
actor.new(State(mpv, input_inject, exit))
|
actor.new(State(ui, mpv, input_inject, exit))
|
||||||
|> actor.on_message(handle_message)
|
|> actor.on_message(handle_message)
|
||||||
|> actor.start
|
|> actor.start
|
||||||
{
|
{
|
||||||
@@ -51,9 +54,9 @@ pub fn new(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn handle_message(
|
fn handle_message(
|
||||||
state: State(mpv, input_inject, exit),
|
state: State(ui, mpv, input_inject, exit),
|
||||||
control: Control,
|
control: Control,
|
||||||
) -> actor.Next(State(mpv, input_inject, exit), Control) {
|
) -> actor.Next(State(ui, mpv, input_inject, exit), Control) {
|
||||||
case control {
|
case control {
|
||||||
control.Search -> {
|
control.Search -> {
|
||||||
process.send(state.input_inject, key.Continue([key.input_introducer]))
|
process.send(state.input_inject, key.Continue([key.input_introducer]))
|
||||||
@@ -70,9 +73,21 @@ fn handle_message(
|
|||||||
mpv_control.GetPlaybackTime(reply_to)
|
mpv_control.GetPlaybackTime(reply_to)
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
Error(err) -> echo "! could not get playbackTime: " <> err.details
|
Error(err) ->
|
||||||
|
process.send(
|
||||||
|
state.ui,
|
||||||
|
ui_control.UpdateState(
|
||||||
|
"playback time: N/A (err: " <> err.details <> ")",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
Ok(mpv_control.PlaybackTime(data: playback_time)) ->
|
Ok(mpv_control.PlaybackTime(data: playback_time)) ->
|
||||||
echo "playbacktime from mpv: " <> float.to_string(playback_time)
|
process.send(
|
||||||
|
state.ui,
|
||||||
|
ui_control.UpdateState(
|
||||||
|
"playback time: " <> float.to_string(playback_time),
|
||||||
|
),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
actor.continue(state)
|
actor.continue(state)
|
||||||
@@ -81,6 +96,9 @@ fn handle_message(
|
|||||||
// Close `mpv` socket
|
// Close `mpv` socket
|
||||||
process.call(state.mpv, 1000, fn(reply_to) { mpv_control.Exit(reply_to) })
|
process.call(state.mpv, 1000, fn(reply_to) { mpv_control.Exit(reply_to) })
|
||||||
|
|
||||||
|
// Reset terminal state (show cursor etc.)
|
||||||
|
process.call(state.ui, 1000, fn(reply_to) { ui_control.Exit(reply_to) })
|
||||||
|
|
||||||
// End main process
|
// End main process
|
||||||
process.send(state.exit, Nil)
|
process.send(state.exit, Nil)
|
||||||
actor.stop()
|
actor.stop()
|
||||||
|
|||||||
7
src/musicplayer/ui/control.gleam
Normal file
7
src/musicplayer/ui/control.gleam
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import gleam/erlang/process.{type Subject}
|
||||||
|
|
||||||
|
pub type Control {
|
||||||
|
UpdateState(content: String)
|
||||||
|
|
||||||
|
Exit(reply_to: Subject(Nil))
|
||||||
|
}
|
||||||
19
src/musicplayer/ui/internal.gleam
Normal file
19
src/musicplayer/ui/internal.gleam
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import gleam/io
|
||||||
|
|
||||||
|
pub fn print(content: String) -> Nil {
|
||||||
|
io.print(content <> "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://en.wikipedia.org/wiki/ANSI_escape_code#Control_Sequence_Introducer_commands
|
||||||
|
|
||||||
|
pub fn clear_screen() -> Nil {
|
||||||
|
io.print("\u{001B}[2J\u{001B}[H")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hide_cursor() -> Nil {
|
||||||
|
io.print("\u{001B}[?25l")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn show_cursor() -> Nil {
|
||||||
|
io.print("\u{001B}[?25h")
|
||||||
|
}
|
||||||
62
src/musicplayer/ui/ui.gleam
Normal file
62
src/musicplayer/ui/ui.gleam
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import gleam/erlang/process.{type Subject}
|
||||||
|
import gleam/otp/actor
|
||||||
|
import gleam/string
|
||||||
|
|
||||||
|
import musicplayer/ui/control.{type Control}
|
||||||
|
import musicplayer/ui/internal as ui_internal
|
||||||
|
|
||||||
|
pub type State(redraw, content) {
|
||||||
|
State(redraw: Subject(String), content: String)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new() -> Result(Subject(Control), String) {
|
||||||
|
let redraw_name = process.new_name("redraw")
|
||||||
|
let redraw: Subject(String) = process.named_subject(redraw_name)
|
||||||
|
case
|
||||||
|
actor.new(State(redraw, ""))
|
||||||
|
|> actor.on_message(handle_message)
|
||||||
|
|> actor.start
|
||||||
|
{
|
||||||
|
Error(start_error) ->
|
||||||
|
Error("Could not start actor: " <> string.inspect(start_error))
|
||||||
|
Ok(actor.Started(data: ui, ..)) -> {
|
||||||
|
process.spawn(fn() {
|
||||||
|
let assert Ok(_) = process.register(process.self(), redraw_name)
|
||||||
|
|
||||||
|
ui_internal.clear_screen()
|
||||||
|
ui_internal.hide_cursor()
|
||||||
|
|
||||||
|
redraw_loop(redraw)
|
||||||
|
})
|
||||||
|
|
||||||
|
Ok(ui)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_message(
|
||||||
|
state: State(redraw, content),
|
||||||
|
control: Control,
|
||||||
|
) -> actor.Next(State(redraw, content), Control) {
|
||||||
|
case control {
|
||||||
|
control.UpdateState(content) -> {
|
||||||
|
let state = State(..state, content:)
|
||||||
|
actor.send(state.redraw, content)
|
||||||
|
actor.continue(state)
|
||||||
|
}
|
||||||
|
control.Exit(reply_to) -> {
|
||||||
|
ui_internal.show_cursor()
|
||||||
|
process.send(reply_to, Nil)
|
||||||
|
actor.stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn redraw_loop(redraw: Subject(String)) -> Nil {
|
||||||
|
let content = process.receive_forever(redraw)
|
||||||
|
|
||||||
|
ui_internal.clear_screen()
|
||||||
|
ui_internal.print(content)
|
||||||
|
|
||||||
|
redraw_loop(redraw)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user