9 Commits

Author SHA1 Message Date
Alexander Heldt
6aa9cc28de Use StringTree to store the rendered layout
And print the entire layout at once
2025-12-02 21:12:54 +01:00
Alexander Heldt
3d7287902f Move render_layout to layout module 2025-12-02 21:11:50 +01:00
Alexander Heldt
deaa0fcf89 Don't prefix internal modules 2025-12-02 21:11:45 +01:00
Alexander Heldt
f9d7b573ac Simplify logging
To avoid having to pass around a `Subject`
2025-11-30 11:50:46 +01:00
Alexander Heldt
8134f7c3d6 Log dimension updates 2025-11-29 16:52:07 +01:00
Alexander Heldt
661b8f5e82 Update Layout.{width, height} on interval 2025-11-29 16:49:02 +01:00
Alexander Heldt
82a8800362 Remove unused fn ui.print 2025-11-29 15:22:39 +01:00
Alexander Heldt
1d12f46d2c Use logger 2025-11-29 15:06:24 +01:00
Alexander Heldt
610967b7be Add logging module 2025-11-29 15:06:15 +01:00
12 changed files with 171 additions and 31 deletions

View File

@@ -18,6 +18,7 @@ gleam_otp = ">= 1.2.0 and < 2.0.0"
gleam_erlang = ">= 1.3.0 and < 2.0.0"
simplifile = ">= 2.3.1 and < 3.0.0"
gleam_json = ">= 3.1.0 and < 4.0.0"
gleam_time = ">= 1.6.0 and < 2.0.0"
[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"

View File

@@ -7,6 +7,7 @@ packages = [
{ name = "gleam_json", version = "3.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "44FDAA8847BE8FC48CA7A1C089706BD54BADCC4C45B237A992EDDF9F2CDB2836" },
{ name = "gleam_otp", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "BA6A294E295E428EC1562DC1C11EA7530DCB981E8359134BEABC8493B7B2258E" },
{ name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" },
{ name = "gleam_time", version = "1.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_time", source = "hex", outer_checksum = "0DF3834D20193F0A38D0EB21F0A78D48F2EC276C285969131B86DF8D4EF9E762" },
{ name = "gleeunit", version = "1.9.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "DA9553CE58B67924B3C631F96FE3370C49EB6D6DC6B384EC4862CC4AAA718F3C" },
{ name = "simplifile", version = "2.3.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "957E0E5B75927659F1D2A1B7B75D7B9BA96FAA8D0C53EA71C4AD9CD0C6B848F6" },
]
@@ -16,5 +17,6 @@ gleam_erlang = { version = ">= 1.3.0 and < 2.0.0" }
gleam_json = { version = ">= 3.1.0 and < 4.0.0" }
gleam_otp = { version = ">= 1.2.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
gleam_time = { version = ">= 1.6.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
simplifile = { version = ">= 2.3.1 and < 3.0.0" }

View File

@@ -2,11 +2,14 @@ import gleam/erlang/process.{type Name}
import musicplayer/input/input
import musicplayer/input/key.{type Key}
import musicplayer/logging/logging
import musicplayer/mpv/mpv
import musicplayer/musicplayer
import musicplayer/ui/ui
pub fn main() -> Nil {
let assert Ok(_) = logging.initialize()
let input_keys_name: Name(Key) = process.new_name("input_keys")
input.new(input_keys_name)

View File

@@ -0,0 +1,5 @@
pub type Control {
Write(String)
Exit
}

View File

@@ -0,0 +1,30 @@
import gleam/result
import gleam/string
import gleam/time/calendar
import gleam/time/timestamp
import simplifile
const filepath = "/tmp/musicplayer.log"
pub fn initialize() -> Result(Nil, String) {
case simplifile.is_file(filepath) {
Ok(True) -> Ok(Nil)
_ -> simplifile.create_file(filepath)
}
|> result.map_error(fn(e) {
"Could not access or create log file: " <> string.inspect(e)
})
}
pub fn log(content: String) -> Nil {
let log_line =
timestamp.system_time()
|> timestamp.to_rfc3339(calendar.utc_offset)
<> ": "
<> content
<> "\n"
// Ignore any logging errors
let _ = simplifile.append(filepath, log_line)
Nil
}

View File

@@ -24,7 +24,7 @@ pub fn new() -> Result(Subject(Control), String) {
|> actor.start
{
Error(start_error) ->
Error("Could not start actor: " <> string.inspect(start_error))
Error("Could not start mpv: " <> string.inspect(start_error))
Ok(actor.Started(data: mpv, ..)) -> Ok(mpv)
}
}

View File

@@ -2,11 +2,12 @@ import gleam/erlang/process.{type Name, type Pid, type Subject}
import gleam/otp/actor
import gleam/result
import gleam/string
import musicplayer/time/time
import musicplayer/control.{type Control}
import musicplayer/input/key.{type Key}
import musicplayer/logging/logging
import musicplayer/mpv/control as mpv_control
import musicplayer/time/time
import musicplayer/ui/control as ui_control
import musicplayer/ui/layout
@@ -45,6 +46,7 @@ pub fn new(
Error(start_error) ->
Error("Could not start actor: " <> string.inspect(start_error))
Ok(actor.Started(pid:, data: musicplayer)) -> {
logging.log("musicplayer - started")
process.spawn(fn() {
let assert Ok(_) = process.register(process.self(), input_keys_name)
handle_key(musicplayer, input_keys)
@@ -60,6 +62,8 @@ pub fn new(
fn handle_message(state: State, control: Control) -> actor.Next(State, Control) {
case control {
control.Search -> {
logging.log("musicplayer - initiating search")
update_search(state.ui, "searching: ")
actor.continue(
@@ -72,6 +76,8 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
}
control.Raw(content) -> {
logging.log("musicplayer - recieved raw input: " <> content)
let content = case state.mode {
Idle -> state.input.content
Searching -> {
@@ -84,6 +90,8 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
actor.continue(State(..state, input: Input(..state.input, content:)))
}
control.Backspace -> {
logging.log("musicplayer - recieved backspace")
let content = case state.mode {
Idle -> state.input.content
Searching -> string.drop_end(state.input.content, 1)
@@ -91,6 +99,13 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
actor.continue(State(..state, input: Input(..state.input, content:)))
}
control.Return -> {
logging.log(
"musicplayer - recieved return. `input.capture`: "
<> "'"
<> state.input.content
<> "'",
)
// Note: state.input.content is now the final input, use it
// before it is reset
case state.mode {
@@ -104,17 +119,22 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
}
control.TogglePlayPause -> {
logging.log("musicplayer - toggling play/pause")
process.send(state.mpv, mpv_control.TogglePlayPause)
update_playback_time(state.mpv, state.ui)
actor.continue(state)
}
control.Exit -> {
logging.log("musicplayer - initiating musicplayer shutdown")
// Close `mpv` socket
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) })
logging.log("musicplayer - stopped")
actor.stop()
}
}

View File

@@ -3,6 +3,7 @@ import gleam/erlang/process.{type Subject}
import musicplayer/ui/layout.{type Section}
pub type Control {
UpdateDimensions(width: Int, height: Int)
UpdateState(section: Section, content: String)
Exit(reply_to: Subject(Nil))

View File

@@ -1,19 +1,19 @@
import gleam/int
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 print_at(text: String, x: Int, y: Int) -> Nil {
pub fn chars_at(chars: String, x: Int, y: Int) -> String {
let seq = "\u{001B}[" <> int.to_string(y) <> ";" <> int.to_string(x) <> "H"
io.print(seq <> text)
seq <> chars
}
pub fn print(chars: String) -> Nil {
io.print(chars)
}
pub fn hide_cursor() -> Nil {
@@ -23,3 +23,13 @@ pub fn hide_cursor() -> Nil {
pub fn show_cursor() -> Nil {
io.print("\u{001B}[?25h")
}
pub type Enotsup
// https://www.erlang.org/doc/apps/stdlib/io.html#rows/0
@external(erlang, "io", "rows")
pub fn io_get_rows() -> Result(Int, Enotsup)
// https://www.erlang.org/doc/apps/stdlib/io.html#columns/0
@external(erlang, "io", "columns")
pub fn io_get_columns() -> Result(Int, Enotsup)

View File

@@ -1,7 +1,11 @@
import gleam/dict
import gleam/list
import gleam/string_tree.{type StringTree}
import musicplayer/ui/internal
pub type Layout {
Layout(nodes: dict.Dict(Section, Node))
Layout(width: Int, height: Int, nodes: dict.Dict(Section, Node))
}
pub type Section {
@@ -31,7 +35,7 @@ pub fn new() -> Layout {
#(PlaybackTime, Node(content: "00:00", x: 1, y: 2, children: [])),
])
Layout(nodes: nodes)
Layout(0, 0, nodes: nodes)
}
pub fn update_section(
@@ -42,10 +46,45 @@ pub fn update_section(
case dict.get(layout.nodes, section) {
Error(_) -> layout
Ok(node) ->
Layout(nodes: dict.insert(
layout.nodes,
section,
Node(..node, content: content),
))
Layout(
..layout,
nodes: dict.insert(
layout.nodes,
section,
Node(..node, content: content),
),
)
}
}
pub fn update_dimensions(layout: Layout, width: Int, height: Int) -> Layout {
Layout(..layout, width:, height:)
}
pub fn render(layout: Layout, from: Section) -> Nil {
string_tree.new()
|> render_loop(layout, from, _)
|> string_tree.to_string
|> internal.print
}
pub fn render_loop(
layout: Layout,
from: Section,
into: StringTree,
) -> StringTree {
case dict.get(layout.nodes, from) {
Error(_) -> into
Ok(node) -> {
let parent =
string_tree.append(
into,
internal.chars_at(node.content, node.x, node.y),
)
list.fold(node.children, parent, fn(into_acc, child) {
render_loop(layout, child, into_acc)
})
}
}
}

View File

@@ -1,12 +1,13 @@
import gleam/dict
import gleam/erlang/process.{type Subject}
import gleam/int
import gleam/list
import gleam/otp/actor
import gleam/string
import musicplayer/logging/logging
import musicplayer/ui/control.{type Control}
import musicplayer/ui/internal as ui_internal
import musicplayer/ui/layout.{type Layout, type Section}
import musicplayer/ui/internal
import musicplayer/ui/layout.{type Layout}
pub type State(redraw, content) {
State(redraw: Subject(Layout), layout: Layout)
@@ -26,11 +27,16 @@ pub fn new() -> Result(Subject(Control), String) {
Error(start_error) ->
Error("Could not start actor: " <> string.inspect(start_error))
Ok(actor.Started(data: ui, ..)) -> {
process.spawn(fn() {
let update_dimensions_interval_ms = 300
update_dimensions_on_interval(ui, update_dimensions_interval_ms)
})
process.spawn(fn() {
let assert Ok(_) = process.register(process.self(), redraw_name)
ui_internal.clear_screen()
ui_internal.hide_cursor()
internal.clear_screen()
internal.hide_cursor()
redraw_on_update_loop(redraw)
})
@@ -45,6 +51,27 @@ fn handle_message(
control: Control,
) -> actor.Next(State(redraw, layout), Control) {
case control {
control.UpdateDimensions(width, height) -> {
let current_dimensions = #(state.layout.width, state.layout.height)
case #(width, height) == current_dimensions {
True -> actor.continue(state)
False -> {
[width, height]
|> list.map(int.to_string)
|> string.join(" ")
|> string.append("ui - updating dimensions: ", _)
|> logging.log
actor.continue(
State(
..state,
layout: layout.update_dimensions(state.layout, width, height),
),
)
}
}
}
control.UpdateState(section, content) -> {
let layout = layout.update_section(state.layout, section, content)
let state = State(..state, layout:)
@@ -54,7 +81,7 @@ fn handle_message(
}
control.Exit(reply_to) -> {
ui_internal.show_cursor()
internal.show_cursor()
process.send(reply_to, Nil)
actor.stop()
}
@@ -64,18 +91,20 @@ fn handle_message(
fn redraw_on_update_loop(redraw: Subject(Layout)) -> Nil {
let layout = process.receive_forever(redraw)
ui_internal.clear_screen()
render_layout(layout, layout.Root)
internal.clear_screen()
layout.render(layout, layout.Root)
redraw_on_update_loop(redraw)
}
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)
fn update_dimensions_on_interval(ui: Subject(Control), interval_ms: Int) {
case internal.io_get_columns(), internal.io_get_rows() {
Ok(width), Ok(height) -> {
process.send(ui, control.UpdateDimensions(width, height))
}
_, _ -> logging.log("ui - failed to update dimensions")
}
process.sleep(interval_ms)
update_dimensions_on_interval(ui, interval_ms)
}

View File

@@ -1,6 +1,6 @@
import gleeunit
import musicplayer/mpv/internal as control_internal
import musicplayer/mpv/internal
pub fn main() -> Nil {
gleeunit.main()
@@ -10,6 +10,6 @@ pub fn parse_playback_time_test() {
let json_string =
"{\"data\":\"123.456789\",\"request_id\":0,\"error\":\"success\"}\n"
let assert Ok(data) = control_internal.parse_playback_time(json_string)
let assert Ok(data) = internal.parse_playback_time(json_string)
assert data == 123.456789
}