use-string-trees

This commit is contained in:
Alexander Heldt
2025-11-30 15:38:52 +01:00
parent 50c053a42a
commit 1732b12fbe
3 changed files with 86 additions and 32 deletions

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

@@ -3,6 +3,7 @@ import gleam/float
import gleam/int import gleam/int
import gleam/list import gleam/list
import gleam/string import gleam/string
import gleam/string_tree.{type StringTree}
import musicplayer/logging/logging import musicplayer/logging/logging
import musicplayer/ui/internal import musicplayer/ui/internal
@@ -153,7 +154,8 @@ pub fn render(layout: Layout) -> Nil {
let container_top_left_x = 1 let container_top_left_x = 1
let container_top_left_y = 1 let container_top_left_y = 1
render_loop( string_tree.new()
|> render_loop(
layout, layout,
container_width, container_width,
container_height, container_height,
@@ -161,7 +163,10 @@ pub fn render(layout: Layout) -> Nil {
container_top_left_y, container_top_left_y,
0, 0,
Section("Root"), Section("Root"),
_,
) )
|> string_tree.to_string
|> internal.print
} }
pub fn render_loop( pub fn render_loop(
@@ -172,14 +177,16 @@ pub fn render_loop(
container_top_left_y: Int, container_top_left_y: Int,
index: Int, index: Int,
from: Section, from: Section,
) -> Nil { tree: StringTree,
) -> StringTree {
let margin = 2.0 let margin = 2.0
case dict.get(layout.nodes, from) { case dict.get(layout.nodes, from) {
Error(_) -> Nil Error(_) -> tree
Ok(node) -> { Ok(node) -> {
let final_tree =
list.index_map(node.children, fn(child, i) { #(i, child) }) list.index_map(node.children, fn(child, i) { #(i, child) })
|> list.each(fn(ic: #(Int, Section)) { |> list.fold(tree, fn(updated_tree: StringTree, ic: #(Int, Section)) {
let #(i, child) = ic let #(i, child) = ic
let cw = let cw =
@@ -196,7 +203,7 @@ pub fn render_loop(
let cx = container_top_left_x + 1 let cx = container_top_left_x + 1
let cy = container_top_left_y + 1 let cy = container_top_left_y + 1
render_loop(layout, cw, ch, cx, cy, i, child) render_loop(layout, cw, ch, cx, cy, i, child, updated_tree)
}) })
logging.log("section: " <> string.inspect(from)) logging.log("section: " <> string.inspect(from))
@@ -233,23 +240,40 @@ pub fn render_loop(
logging.log("cx: " <> int.to_string(cx)) logging.log("cx: " <> int.to_string(cx))
logging.log("cy: " <> int.to_string(cy)) logging.log("cy: " <> int.to_string(cy))
draw_box(cx, cy, width, height) final_tree
|> string_tree.append(draw_box(cx, cy, width, height))
// Box heading // Box heading
internal.print_at(node.content, cx, cy) |> string_tree.append(internal.chars_at(node.content, cx, cy))
} }
} }
} }
fn draw_box(x: Int, y: Int, width: Int, height: Int) -> Nil { fn draw_box(x: Int, y: Int, width: Int, height: Int) -> String {
let box_tree = string_tree.new()
let box_chars = #("", "", "", "", "", "") let box_chars = #("", "", "", "", "", "")
let #(tl, tr, bl, br, h, v) = box_chars let #(tl, tr, bl, br, h, v) = box_chars
internal.print_at(tl <> string.repeat(h, width - 2) <> tr, x, y)
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.range(1, height - 2)
|> list.each(fn(row) { |> list.map(fn(row) {
internal.print_at(v, x, y + row) box_tree
internal.print_at(v, x + width - 1, y + row) |> string_tree.append(internal.chars_at(v, x, y + row))
|> string_tree.append(internal.chars_at(v, x + width - 1, y + row))
}) })
internal.print_at(bl <> string.repeat(h, width - 2) <> br, x, y + height - 1) 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,6 +3,7 @@ import gleam/int
import gleam/list import gleam/list
import gleam/otp/actor import gleam/otp/actor
import gleam/string import gleam/string
import gleam/string_tree.{type StringTree}
import musicplayer/logging/logging import musicplayer/logging/logging
import musicplayer/ui/control.{type Control} import musicplayer/ui/control.{type Control}
@@ -103,3 +104,28 @@ fn update_dimensions_on_interval(ui: Subject(Control), interval_ms: Int) {
process.sleep(interval_ms) process.sleep(interval_ms)
update_dimensions_on_interval(ui, 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))
// }
// }
// }