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 37 additions and 335 deletions

View File

@@ -146,7 +146,6 @@ 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,11 +1,7 @@
import gleam/dict
import gleam/float
import gleam/int
import gleam/list
import gleam/string
import gleam/string_tree.{type StringTree}
import musicplayer/logging/logging
import musicplayer/ui/internal
pub type Layout {
@@ -13,109 +9,33 @@ pub type Layout {
}
pub type Section {
Section(String)
Root
Header
Search
PlaybackTime
}
// pub type Section {
// Root
// Header
// Search
// PlaybackTime
// Test
// Row1
// A
// B
// Row2
// C
// D
// }
pub type NodeType {
Container
Row
Cell
}
/// A Nodes width and height is in percentage (of the available width/height of its parent Node)
pub type Node {
Node(
t: NodeType,
content: String,
width_percent: Int,
height_percent: Int,
children: List(Section),
)
Node(content: String, x: Int, y: Int, children: List(Section))
}
pub fn new() -> Layout {
let nodes =
dict.from_list([
#(
Section("Root"),
Node(
t: Container,
content: "container",
width_percent: 100,
height_percent: 100,
children: [
Section("Row1"),
Section("Row2"),
],
),
),
#(
Section("Row1"),
Node(
t: Row,
content: "row 1",
width_percent: 100,
height_percent: 50,
children: [
Section("A"),
Section("A"),
],
),
),
#(
Section("A"),
Node(
t: Cell,
content: "cell 1",
width_percent: 50,
height_percent: 100,
children: [],
),
),
#(
Section("B"),
Node(
t: Cell,
content: "cell 2",
width_percent: 50,
height_percent: 100,
children: [],
),
),
#(
Section("Row2"),
Node(
t: Row,
content: "row 1",
width_percent: 100,
height_percent: 50,
children: [],
),
Root,
Node(content: "", x: 0, y: 0, 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: [])),
])
Layout(width: 0, height: 0, nodes: nodes)
Layout(0, 0, nodes: nodes)
}
pub fn update_section(
@@ -141,139 +61,30 @@ pub fn update_dimensions(layout: Layout, width: Int, height: Int) -> Layout {
Layout(..layout, width:, height:)
}
pub fn render(layout: Layout) -> Nil {
internal.clear_screen()
[layout.width, layout.height]
|> list.map(int.to_string)
|> string.join(" ")
|> 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
pub fn render(layout: Layout, from: Section) -> Nil {
string_tree.new()
|> render_loop(
layout,
container_width,
container_height,
container_top_left_x,
container_top_left_y,
0,
Section("Root"),
_,
)
|> render_loop(layout, from, _)
|> string_tree.to_string
|> internal.print
}
pub fn render_loop(
layout: Layout,
container_width: Float,
container_height: Float,
container_top_left_x: Int,
container_top_left_y: Int,
index: Int,
from: Section,
tree: StringTree,
into: StringTree,
) -> StringTree {
let margin = 2.0
case dict.get(layout.nodes, from) {
Error(_) -> tree
Error(_) -> into
Ok(node) -> {
let final_tree =
list.index_map(node.children, fn(child, i) { #(i, child) })
|> list.fold(tree, fn(updated_tree: StringTree, ic: #(Int, Section)) {
let #(i, child) = ic
let parent =
string_tree.append(
into,
internal.chars_at(node.content, node.x, node.y),
)
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, i, child, updated_tree)
list.fold(node.children, parent, fn(into_acc, child) {
render_loop(layout, child, into_acc)
})
logging.log("section: " <> string.inspect(from))
logging.log("section type: " <> string.inspect(node.t))
logging.log("index: " <> string.inspect(index))
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("section width: " <> int.to_string(width))
logging.log("section height: " <> int.to_string(height))
let #(cx, cy) = case node.t {
Container -> #(container_top_left_x, container_top_left_y)
Row -> #(
container_top_left_x,
container_top_left_y + { index * height },
)
Cell -> #(
container_top_left_x + { index * width },
container_top_left_y,
)
}
logging.log("cx: " <> int.to_string(cx))
logging.log("cy: " <> int.to_string(cy))
final_tree
|> string_tree.append(draw_box(cx, cy, width, height))
// Box heading
|> string_tree.append(internal.chars_at(node.content, cx, cy))
}
}
}
fn draw_box(x: Int, y: Int, width: Int, height: Int) -> String {
let box_tree = string_tree.new()
let box_chars = #("", "", "", "", "", "")
let #(tl, tr, bl, br, h, v) = box_chars
let box_tree =
string_tree.append(
box_tree,
internal.chars_at(tl <> string.repeat(h, width - 2) <> tr, x, y),
)
let box_trees =
list.range(1, height - 2)
|> list.map(fn(row) {
box_tree
|> string_tree.append(internal.chars_at(v, x, y + row))
|> string_tree.append(internal.chars_at(v, x + width - 1, y + row))
})
string_tree.append(
string_tree.concat(box_trees),
internal.chars_at(
bl <> string.repeat(h, width - 2) <> br,
x,
y + height - 1,
),
)
|> string_tree.to_string
}

View File

@@ -3,7 +3,6 @@ import gleam/int
import gleam/list
import gleam/otp/actor
import gleam/string
import gleam/string_tree.{type StringTree}
import musicplayer/logging/logging
import musicplayer/ui/control.{type Control}
@@ -39,7 +38,7 @@ pub fn new() -> Result(Subject(Control), String) {
internal.clear_screen()
internal.hide_cursor()
redraw_loop(redraw)
redraw_on_update_loop(redraw)
})
Ok(ui)
@@ -64,18 +63,21 @@ fn handle_message(
|> string.append("ui - updating dimensions: ", _)
|> logging.log
let layout = layout.update_dimensions(state.layout, width, height)
process.send(state.redraw, layout)
actor.continue(State(..state, layout:))
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:)
actor.send(state.redraw, layout)
actor.continue(State(..state, layout:))
actor.continue(state)
}
control.Exit(reply_to) -> {
@@ -86,11 +88,13 @@ fn handle_message(
}
}
fn redraw_loop(redraw: Subject(Layout)) -> Nil {
process.receive_forever(redraw)
|> layout.render
fn redraw_on_update_loop(redraw: Subject(Layout)) -> Nil {
let layout = process.receive_forever(redraw)
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) {
@@ -104,28 +108,3 @@ fn update_dimensions_on_interval(ui: Subject(Control), interval_ms: Int) {
process.sleep(interval_ms)
update_dimensions_on_interval(ui, interval_ms)
}
// fn render_layout(layout: Layout, from: Section) -> Nil {
// string_tree.new()
// |> render_layout_loop(layout, from, _)
// |> string_tree.to_string
// |> ui_internal.print
// }
// fn render_layout_loop(
// layout: Layout,
// from: Section,
// tree: StringTree,
// ) -> StringTree {
// case dict.get(layout.nodes, from) {
// Error(_) -> tree
// Ok(node) -> {
// let acc_after_children =
// list.fold(node.children, tree, fn(current_acc, child_id) {
// render_layout_loop(layout, child_id, current_acc)
// })
// acc_after_children
// |> string_tree.append(ui_internal.chars_at(node.content, node.x, node.y))
// }
// }
// }

View File

@@ -1,87 +0,0 @@
// import gleam/dict
// import gleam/int
// import gleeunit
// import musicplayer/ui/layout.{type Node, Layout, Section}
// pub fn main() -> Nil {
// gleeunit.main()
// }
// pub fn foo_test() {
// let expected = ""
// let layout =
// Layout(
// width: 80,
// height: 20,
// nodes:,
// dict.from_list([
// #(
// Section("Root"),
// Node(
// t: Container,
// content: "container",
// width_percent: 100,
// height_percent: 100,
// children: [
// Section("Row1"),
// Section("Row2"),
// ],
// ),
// ),
// #(
// Section("Row1"),
// Node(
// t: Row,
// content: "row 1",
// width_percent: 100,
// height_percent: 50,
// children: [
// Section("A"),
// Section("B"),
// ],
// ),
// ),
// #(
// Section("A"),
// Node(
// t: Cell,
// content: "cell 1",
// width_percent: 50,
// height_percent: 100,
// children: [],
// ),
// ),
// #(
// B,
// Node(
// t: Cell,
// content: "cell 2",
// width_percent: 50,
// height_percent: 100,
// children: [],
// ),
// ),
// #(
// Section("Row2"),
// Node(
// t: Row,
// content: "row 1",
// width_percent: 100,
// height_percent: 50,
// children: [],
// ),
// ),
// ]),
// )
// 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
// let assert Ok(data) =
// layout.render_loop(layout, container_width, container_height)
// assert data == 123.456789
// }