1 Commits

Author SHA1 Message Date
Alexander Heldt
3df1a50e51 wip 2025-11-23 10:41:55 +01:00
4 changed files with 35 additions and 5 deletions

View File

@@ -8,6 +8,7 @@ import musicplayer/control.{type Control}
import musicplayer/input/key.{type Key}
import musicplayer/mpv/control as mpv_control
import musicplayer/ui/control as ui_control
import musicplayer/ui/section
type State(ui, mpv, input_inject, exit) {
State(
@@ -77,6 +78,7 @@ fn handle_message(
process.send(
state.ui,
ui_control.UpdateState(
section.PlaybackTime,
"playback time: N/A (err: " <> err.details <> ")",
),
)
@@ -85,6 +87,7 @@ fn handle_message(
process.send(
state.ui,
ui_control.UpdateState(
section.PlaybackTime,
"playback time: " <> float.to_string(playback_time),
),
)

View File

@@ -1,7 +1,9 @@
import gleam/erlang/process.{type Subject}
import musicplayer/ui/section.{type Section}
pub type Control {
UpdateState(content: String)
UpdateState(section: Section, content: String)
Exit(reply_to: Subject(Nil))
}

View File

@@ -0,0 +1,13 @@
import gleam/dict
pub type Section {
Header
PlaybackTime
}
pub type Content {
Content(content: String)
}
pub type SectionContent =
dict.Dict(Section, Content)

View File

@@ -1,19 +1,28 @@
import gleam/dict
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
import musicplayer/ui/section.{type Section, type SectionContent, Content}
pub type State(redraw, content) {
State(redraw: Subject(String), content: String)
State(redraw: Subject(SectionContent), sections: SectionContent)
}
pub fn new() -> Result(Subject(Control), String) {
let redraw_name = process.new_name("redraw")
let redraw: Subject(String) = process.named_subject(redraw_name)
let sections =
dict.from_list([
#(section.Header, Content("musicplayer:")),
#(section.PlaybackTime, Content("")),
])
case
actor.new(State(redraw, ""))
actor.new(State(redraw, sections))
|> actor.on_message(handle_message)
|> actor.start
{
@@ -39,11 +48,14 @@ fn handle_message(
control: Control,
) -> actor.Next(State(redraw, content), Control) {
case control {
control.UpdateState(content) -> {
control.UpdateState(section, content) -> {
let content = dict.insert(state.content, section, 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)
@@ -52,7 +64,7 @@ fn handle_message(
}
}
fn redraw_loop(redraw: Subject(String)) -> Nil {
fn redraw_loop(redraw: Subject(SectionContent)) -> Nil {
let content = process.receive_forever(redraw)
ui_internal.clear_screen()