3 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
4 changed files with 44 additions and 126 deletions

View File

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

View File

@@ -7,9 +7,13 @@ 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 { pub fn chars_at(chars: String, x: Int, y: Int) -> String {
let seq = "\u{001B}[" <> int.to_string(y) <> ";" <> int.to_string(x) <> "H" 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 { pub fn hide_cursor() -> Nil {

View File

@@ -1,10 +1,7 @@
import gleam/dict import gleam/dict
import gleam/float
import gleam/int
import gleam/list import gleam/list
import gleam/string import gleam/string_tree.{type StringTree}
import musicplayer/logging/logging
import musicplayer/ui/internal import musicplayer/ui/internal
pub type Layout { pub type Layout {
@@ -18,14 +15,8 @@ pub type Section {
PlaybackTime PlaybackTime
} }
/// A Nodes width and height is in percentage (of the available width/height of its parent Node)
pub type Node { pub type Node {
Node( Node(content: String, x: Int, y: Int, children: List(Section))
content: String,
width_percent: Int,
height_percent: Int,
children: List(Section),
)
} }
pub fn new() -> Layout { pub fn new() -> Layout {
@@ -33,26 +24,15 @@ pub fn new() -> Layout {
dict.from_list([ dict.from_list([
#( #(
Root, Root,
Node(content: "", width_percent: 100, height_percent: 100, children: [ Node(content: "", x: 0, y: 0, children: [
// Header, Header,
// Search, Search,
PlaybackTime, PlaybackTime,
]), ]),
), ),
// #( #(Header, Node(content: "Music Player", x: 1, y: 1, children: [])),
// Header, #(Search, Node(content: "", x: 30, y: 1, children: [])),
// Node(content: "Music Player", width: 50, height: 10, children: []), #(PlaybackTime, Node(content: "00:00", x: 1, y: 2, 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) Layout(0, 0, nodes: nodes)
@@ -81,100 +61,30 @@ pub fn update_dimensions(layout: Layout, width: Int, height: Int) -> Layout {
Layout(..layout, width:, height:) Layout(..layout, width:, height:)
} }
pub fn render(layout: Layout) -> Nil { pub fn render(layout: Layout, from: Section) -> Nil {
internal.clear_screen() string_tree.new()
[layout.width, layout.height] |> render_loop(layout, from, _)
|> list.map(int.to_string) |> string_tree.to_string
|> string.join(" ") |> internal.print
|> string.append("layout - render: ", _)
|> logging.log
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,
)
} }
pub fn render_loop( pub fn render_loop(
layout: Layout, layout: Layout,
container_width: Float,
container_height: Float,
container_top_left_x: Int,
container_top_left_y: Int,
from: Section, from: Section,
) -> Nil { into: StringTree,
let margin = 2.0 ) -> StringTree {
case dict.get(layout.nodes, from) { case dict.get(layout.nodes, from) {
Error(_) -> Nil Error(_) -> into
Ok(node) -> { Ok(node) -> {
list.each(node.children, fn(child) { let parent =
let cw = string_tree.append(
container_width into,
*. { int.to_float(node.width_percent) /. 100.0 } internal.chars_at(node.content, node.x, node.y),
-. margin )
|> float.floor
let ch = list.fold(node.children, parent, fn(into_acc, child) {
container_height render_loop(layout, child, into_acc)
*. { 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)
}) })
logging.log("section: " <> string.inspect(from))
logging.log("container width: " <> float.to_string(container_width))
logging.log("container height: " <> float.to_string(container_height))
let width =
container_width *. { int.to_float(node.width_percent) /. 100.0 }
|> float.floor
|> float.truncate
let height =
container_height *. { int.to_float(node.height_percent) /. 100.0 }
|> float.floor
|> float.truncate
logging.log("width: " <> int.to_string(width))
logging.log("height: " <> int.to_string(height))
let cx = container_top_left_x
let cy = container_top_left_y
logging.log("cx: " <> int.to_string(cx))
logging.log("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

@@ -38,7 +38,7 @@ pub fn new() -> Result(Subject(Control), String) {
internal.clear_screen() internal.clear_screen()
internal.hide_cursor() internal.hide_cursor()
redraw_loop(redraw) redraw_on_update_loop(redraw)
}) })
Ok(ui) Ok(ui)
@@ -63,18 +63,21 @@ fn handle_message(
|> string.append("ui - updating dimensions: ", _) |> string.append("ui - updating dimensions: ", _)
|> logging.log |> logging.log
let layout = layout.update_dimensions(state.layout, width, height) actor.continue(
State(
process.send(state.redraw, layout) ..state,
actor.continue(State(..state, layout:)) layout: layout.update_dimensions(state.layout, width, height),
),
)
} }
} }
} }
control.UpdateState(section, content) -> { control.UpdateState(section, content) -> {
let layout = layout.update_section(state.layout, section, content) let layout = layout.update_section(state.layout, section, content)
let state = State(..state, layout:)
actor.send(state.redraw, layout) actor.send(state.redraw, layout)
actor.continue(State(..state, layout:)) actor.continue(state)
} }
control.Exit(reply_to) -> { control.Exit(reply_to) -> {
@@ -85,11 +88,13 @@ fn handle_message(
} }
} }
fn redraw_loop(redraw: Subject(Layout)) -> Nil { fn redraw_on_update_loop(redraw: Subject(Layout)) -> Nil {
process.receive_forever(redraw) let layout = process.receive_forever(redraw)
|> layout.render
redraw_loop(redraw) internal.clear_screen()
layout.render(layout, layout.Root)
redraw_on_update_loop(redraw)
} }
fn update_dimensions_on_interval(ui: Subject(Control), interval_ms: Int) { fn update_dimensions_on_interval(ui: Subject(Control), interval_ms: Int) {