4 Commits

Author SHA1 Message Date
Alexander Heldt
693eaf44bd working-but-not-columnwise 2025-11-29 21:43:59 +01:00
Alexander Heldt
6ef3e9734d wip-working-ish 2025-11-29 20:46:35 +01:00
Alexander Heldt
e524a865fe Move render_layout to layout module 2025-11-29 19:03:37 +01:00
Alexander Heldt
5e8ba9a4b0 Don't prefix internal modules 2025-11-29 18:59:28 +01:00
4 changed files with 163 additions and 41 deletions

View File

@@ -155,6 +155,7 @@ fn update_playback_time_loop(
interval_ms: Int,
) {
process.sleep(interval_ms)
// TODO only update if state is playing
update_playback_time(mpv, ui)
update_playback_time_loop(mpv, ui, interval_ms)

View File

@@ -1,4 +1,13 @@
import gleam/dict
import gleam/erlang/process.{type Subject}
import gleam/float
import gleam/int
import gleam/list
import gleam/string
import musicplayer/logging/control as logging_control
import musicplayer/logging/logging
import musicplayer/ui/internal
pub type Layout {
Layout(width: Int, height: Int, nodes: dict.Dict(Section, Node))
@@ -11,8 +20,14 @@ pub type Section {
PlaybackTime
}
/// A Nodes width and height is in percentage (of the available width/height of its parent Node)
pub type Node {
Node(content: String, x: Int, y: Int, children: List(Section))
Node(
content: String,
width_percent: Int,
height_percent: Int,
children: List(Section),
)
}
pub fn new() -> Layout {
@@ -20,15 +35,26 @@ pub fn new() -> Layout {
dict.from_list([
#(
Root,
Node(content: "", x: 0, y: 0, children: [
Header,
Search,
Node(content: "", width_percent: 100, height_percent: 100, children: [
// Header,
// Search,
PlaybackTime,
]),
),
#(Header, Node(content: "Music Player", x: 1, y: 1, children: [])),
#(Search, Node(content: "", x: 30, y: 1, children: [])),
#(PlaybackTime, Node(content: "00:00", x: 1, y: 2, children: [])),
// #(
// Header,
// Node(content: "Music Player", width: 50, height: 10, children: []),
// ),
// #(Search, Node(content: "", width: 50, height: 10, children: [])),
#(
PlaybackTime,
Node(
content: "00:00",
width_percent: 50,
height_percent: 100,
children: [],
),
),
])
Layout(0, 0, nodes: nodes)
@@ -56,3 +82,111 @@ pub fn update_section(
pub fn update_dimensions(layout: Layout, width: Int, height: Int) -> Layout {
Layout(..layout, width:, height:)
}
pub fn render(logger: Subject(logging_control.Control), layout: Layout) -> Nil {
internal.clear_screen()
[layout.width, layout.height]
|> list.map(int.to_string)
|> string.join(" ")
|> string.append("layout - render: ", _)
|> logging.log(logger, _)
let container_width = int.to_float(layout.width)
let container_height = int.to_float(layout.height)
let container_top_left_x = 1
let container_top_left_y = 1
render_loop(
layout,
container_width,
container_height,
container_top_left_x,
container_top_left_y,
Root,
logger,
)
}
pub fn render_loop(
layout: Layout,
container_width: Float,
container_height: Float,
container_top_left_x: Int,
container_top_left_y: Int,
from: Section,
logger: Subject(logging_control.Control),
) -> Nil {
let margin = 2.0
case dict.get(layout.nodes, from) {
Error(_) -> Nil
Ok(node) -> {
list.each(node.children, fn(child) {
let cw =
container_width
*. { int.to_float(node.width_percent) /. 100.0 }
-. margin
|> float.floor
let ch =
container_height
*. { int.to_float(node.height_percent) /. 100.0 }
-. margin
|> float.floor
let cx = container_top_left_x + 1
let cy = container_top_left_y + 1
render_loop(layout, cw, ch, cx, cy, child, logger)
})
logging.log(logger, "section: " <> string.inspect(from))
logging.log(
logger,
"container width: " <> float.to_string(container_width),
)
logging.log(
logger,
"container height: " <> float.to_string(container_height),
)
let width =
container_width
*. { int.to_float(node.width_percent) /. int.to_float(100) }
|> float.floor
|> float.truncate
let height =
container_height
*. { int.to_float(node.height_percent) /. int.to_float(100) }
|> float.floor
|> float.truncate
logging.log(logger, "width: " <> int.to_string(width))
logging.log(logger, "height: " <> int.to_string(height))
let cx = container_top_left_x
let cy = container_top_left_y
logging.log(logger, "cx: " <> int.to_string(cx))
logging.log(logger, "cy: " <> int.to_string(cy))
draw_box(cx, cy, width, height)
// Box heading
internal.print_at(node.content, cx, cy)
}
}
}
fn draw_box(x: Int, y: Int, width: Int, height: Int) -> Nil {
let box_chars = #("", "", "", "", "", "")
let #(tl, tr, bl, br, h, v) = box_chars
internal.print_at(tl <> string.repeat(h, width - 2) <> tr, x, y)
list.range(1, height - 2)
|> list.each(fn(row) {
internal.print_at(v, x, y + row)
internal.print_at(v, x + width - 1, y + row)
})
internal.print_at(bl <> string.repeat(h, width - 2) <> br, x, y + height - 1)
}

View File

@@ -1,4 +1,3 @@
import gleam/dict
import gleam/erlang/process.{type Subject}
import gleam/int
import gleam/list
@@ -8,8 +7,8 @@ import gleam/string
import musicplayer/logging/control as logging_control
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(
@@ -43,10 +42,10 @@ pub fn new(
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)
redraw_loop(logger, redraw)
})
Ok(ui)
@@ -71,38 +70,36 @@ fn handle_message(
|> string.append("ui - updating dimensions: ", _)
|> logging.log(state.logger, _)
actor.continue(
State(
..state,
layout: layout.update_dimensions(state.layout, width, height),
),
)
let layout = layout.update_dimensions(state.layout, width, height)
process.send(state.redraw, layout)
actor.continue(State(..state, layout:))
}
}
}
control.UpdateState(section, content) -> {
let layout = layout.update_section(state.layout, section, content)
let state = State(..state, layout:)
actor.send(state.redraw, layout)
actor.continue(state)
actor.continue(State(..state, layout:))
}
control.Exit(reply_to) -> {
ui_internal.show_cursor()
internal.show_cursor()
process.send(reply_to, Nil)
actor.stop()
}
}
}
fn redraw_on_update_loop(redraw: Subject(Layout)) -> Nil {
let layout = process.receive_forever(redraw)
fn redraw_loop(
logger: Subject(logging_control.Control),
redraw: Subject(Layout),
) -> Nil {
process.receive_forever(redraw)
|> layout.render(logger, _)
ui_internal.clear_screen()
render_layout(layout, layout.Root)
redraw_on_update_loop(redraw)
redraw_loop(logger, redraw)
}
fn update_dimensions_on_interval(
@@ -110,7 +107,7 @@ fn update_dimensions_on_interval(
ui: Subject(Control),
interval_ms: Int,
) {
case ui_internal.io_get_columns(), ui_internal.io_get_rows() {
case internal.io_get_columns(), internal.io_get_rows() {
Ok(width), Ok(height) -> {
process.send(ui, control.UpdateDimensions(width, height))
}
@@ -120,13 +117,3 @@ fn update_dimensions_on_interval(
process.sleep(interval_ms)
update_dimensions_on_interval(logger, 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)
}
}
}

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
}