1 Commits

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

View File

@@ -1,4 +1,5 @@
import gleam/erlang/process.{type Name, type Subject} import gleam/erlang/process.{type Name, type Subject}
import gleam/float
import gleam/otp/actor import gleam/otp/actor
import gleam/result import gleam/result
import gleam/string import gleam/string
@@ -6,9 +7,8 @@ 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/time/time
import musicplayer/ui/control as ui_control import musicplayer/ui/control as ui_control
import musicplayer/ui/layout import musicplayer/ui/section
type State(ui, mpv, input_inject, exit) { type State(ui, mpv, input_inject, exit) {
State( State(
@@ -44,8 +44,6 @@ pub fn new(
handle_key(musicplayer, input_keys) handle_key(musicplayer, input_keys)
}) })
process.spawn(fn() { update_playback_time_loop(mpv, ui, 1000) })
process.spawn(fn() { process.spawn(fn() {
let assert Ok(_) = process.register(process.self(), input_stream_name) let assert Ok(_) = process.register(process.self(), input_stream_name)
temp_input_stream(input_stream) temp_input_stream(input_stream)
@@ -70,7 +68,31 @@ fn handle_message(
echo "toggling play/pause" echo "toggling play/pause"
process.send(state.mpv, mpv_control.TogglePlayPause) process.send(state.mpv, mpv_control.TogglePlayPause)
update_playback_time(state.mpv, state.ui)
case
process.call(state.mpv, 1000, fn(reply_to) {
mpv_control.GetPlaybackTime(reply_to)
})
{
Error(err) ->
process.send(
state.ui,
ui_control.UpdateState(
section.PlaybackTime,
"playback time: N/A (err: " <> err.details <> ")",
),
)
Ok(mpv_control.PlaybackTime(data: playback_time)) ->
process.send(
state.ui,
ui_control.UpdateState(
section.PlaybackTime,
"playback time: " <> float.to_string(playback_time),
),
)
}
actor.continue(state) actor.continue(state)
} }
control.Exit -> { control.Exit -> {
@@ -87,46 +109,6 @@ fn handle_message(
} }
} }
fn update_playback_time_loop(
mpv: Subject(mpv_control.Control),
ui: Subject(ui_control.Control),
interval_ms: Int,
) {
process.sleep(interval_ms)
update_playback_time(mpv, ui)
update_playback_time_loop(mpv, ui, interval_ms)
}
fn update_playback_time(
mpv: Subject(mpv_control.Control),
ui: Subject(ui_control.Control),
) -> Nil {
case
process.call(mpv, 1000, fn(reply_to) {
mpv_control.GetPlaybackTime(reply_to)
})
{
Error(err) ->
process.send(
ui,
ui_control.UpdateState(
layout.PlaybackTime,
"playback time: N/A (err: " <> err.details <> ")",
),
)
Ok(mpv_control.PlaybackTime(data: playback_time)) ->
process.send(
ui,
ui_control.UpdateState(
layout.PlaybackTime,
"playback time: " <> time.to_time_string(playback_time),
),
)
}
}
/// `handle_key` listens to a subject onto which `input` will send messages with /// `handle_key` listens to a subject onto which `input` will send messages with
/// parsed `Key`s which will be mapped to `Control`s (if possible) /// parsed `Key`s which will be mapped to `Control`s (if possible)
fn handle_key(musicplayer: Subject(Control), input_keys: Subject(Key)) -> Nil { fn handle_key(musicplayer: Subject(Control), input_keys: Subject(Key)) -> Nil {

View File

@@ -1,20 +0,0 @@
import gleam/float
import gleam/int
import gleam/string
pub fn to_time_string(seconds: Float) -> String {
let total = float.truncate(seconds)
let minutes = total / 60
let seconds = total % 60
let mins =
int.to_string(minutes)
|> string.pad_start(to: 2, with: "0")
let secs =
int.to_string(seconds)
|> string.pad_start(to: 2, with: "0")
mins <> ":" <> secs
}

View File

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

View File

@@ -1,4 +1,3 @@
import gleam/int
import gleam/io import gleam/io
pub fn print(content: String) -> Nil { pub fn print(content: String) -> Nil {
@@ -11,11 +10,6 @@ pub fn clear_screen() -> Nil {
io.print("\u{001B}[2J\u{001B}[H") io.print("\u{001B}[2J\u{001B}[H")
} }
pub fn print_at(text: String, x: Int, y: Int) -> Nil {
let seq = "\u{001B}[" <> int.to_string(y) <> ";" <> int.to_string(x) <> "H"
io.print(seq <> text)
}
pub fn hide_cursor() -> Nil { pub fn hide_cursor() -> Nil {
io.print("\u{001B}[?25l") io.print("\u{001B}[?25l")
} }

View File

@@ -1,47 +0,0 @@
import gleam/dict
pub type Layout {
Layout(nodes: dict.Dict(Section, Node))
}
pub type Section {
Root
Header
PlaybackTime
}
pub type Node {
Node(content: String, x: Int, y: Int, children: List(Section))
}
pub fn new() -> Layout {
let nodes =
dict.from_list([
#(
Root,
Node(content: "Music Player", x: 1, y: 1, children: [
Header,
PlaybackTime,
]),
),
#(PlaybackTime, Node(content: "00:00", x: 1, y: 2, children: [])),
])
Layout(nodes: nodes)
}
pub fn update_section(
layout: Layout,
section: Section,
content: String,
) -> Layout {
case dict.get(layout.nodes, section) {
Error(_) -> layout
Ok(node) ->
Layout(nodes: dict.insert(
layout.nodes,
section,
Node(..node, content: content),
))
}
}

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,25 +1,28 @@
import gleam/dict import gleam/dict
import gleam/erlang/process.{type Subject} import gleam/erlang/process.{type Subject}
import gleam/list
import gleam/otp/actor import gleam/otp/actor
import gleam/string import gleam/string
import musicplayer/ui/control.{type Control} import musicplayer/ui/control.{type Control}
import musicplayer/ui/internal as ui_internal import musicplayer/ui/internal as ui_internal
import musicplayer/ui/layout.{type Layout, type Section} import musicplayer/ui/section.{type Section, type SectionContent, Content}
pub type State(redraw, content) { pub type State(redraw, content) {
State(redraw: Subject(Layout), layout: Layout) State(redraw: Subject(SectionContent), sections: SectionContent)
} }
pub fn new() -> Result(Subject(Control), String) { pub fn new() -> Result(Subject(Control), String) {
let redraw_name = process.new_name("redraw") let redraw_name = process.new_name("redraw")
let redraw: Subject(Layout) = process.named_subject(redraw_name) let redraw: Subject(String) = process.named_subject(redraw_name)
let layout = layout.new() let sections =
dict.from_list([
#(section.Header, Content("musicplayer:")),
#(section.PlaybackTime, Content("")),
])
case case
actor.new(State(redraw, layout)) actor.new(State(redraw, sections))
|> actor.on_message(handle_message) |> actor.on_message(handle_message)
|> actor.start |> actor.start
{ {
@@ -32,30 +35,24 @@ pub fn new() -> Result(Subject(Control), String) {
ui_internal.clear_screen() ui_internal.clear_screen()
ui_internal.hide_cursor() ui_internal.hide_cursor()
redraw_on_update_loop(redraw) redraw_loop(redraw)
}) })
process.spawn(fn() { redraw_on_tick_loop(ui, 1000) })
Ok(ui) Ok(ui)
} }
} }
} }
fn handle_message( fn handle_message(
state: State(redraw, layout), state: State(redraw, content),
control: Control, control: Control,
) -> actor.Next(State(redraw, layout), Control) { ) -> actor.Next(State(redraw, content), Control) {
case control { case control {
control.Redraw -> {
actor.send(state.redraw, state.layout)
actor.continue(state)
}
control.UpdateState(section, content) -> { control.UpdateState(section, content) -> {
let layout = layout.update_section(state.layout, section, content) let content = dict.insert(state.content, section, content)
let state = State(..state, layout:) let state = State(..state, content:)
actor.send(state.redraw, layout) actor.send(state.redraw, content)
actor.continue(state) actor.continue(state)
} }
@@ -67,28 +64,11 @@ fn handle_message(
} }
} }
fn redraw_on_update_loop(redraw: Subject(Layout)) -> Nil { fn redraw_loop(redraw: Subject(SectionContent)) -> Nil {
let layout = process.receive_forever(redraw) let content = process.receive_forever(redraw)
ui_internal.clear_screen() ui_internal.clear_screen()
render_layout(layout, layout.Root) ui_internal.print(content)
redraw_on_update_loop(redraw) redraw_loop(redraw)
}
fn redraw_on_tick_loop(ui: Subject(Control), interval_ms: Int) {
process.sleep(interval_ms)
process.send(ui, control.Redraw)
redraw_on_tick_loop(ui, interval_ms)
}
fn render_layout(layout: Layout, from: Section) -> Nil {
case dict.get(layout.nodes, from) {
Error(_) -> Nil
Ok(node) -> {
list.each(node.children, fn(child) { render_layout(layout, child) })
ui_internal.print_at(node.content, node.x, node.y)
}
}
} }