6 Commits

Author SHA1 Message Date
Alexander Heldt
89ef014c98 wip 2025-12-02 20:48:34 +01:00
Alexander Heldt
0025772177 wip 2025-11-30 21:45:55 +01:00
Alexander Heldt
033beb6b1f wip 2025-11-30 21:41:21 +01:00
Alexander Heldt
3589d94c29 wip 2025-11-30 21:29:43 +01:00
Alexander Heldt
59429d7721 wip 2025-11-30 18:22:15 +01:00
Alexander Heldt
7212df3abb wip-before-change-of-order 2025-11-30 16:27:31 +01:00
7 changed files with 453 additions and 244 deletions

View File

@@ -0,0 +1,42 @@
import gleam/list
import gleam/string
import gleam/string_tree
import musicplayer/ui/internal
pub fn text(chars: String, x: Int, y: Int) {
internal.chars_at(chars, x, y)
}
pub fn box(x: Int, y: Int, width: Int, height: Int) -> String {
let box_chars = #("", "", "", "", "", "")
let #(tl, tr, bl, br, h, v) = box_chars
// Add top of box
let tree =
string_tree.new()
|> string_tree.append(internal.chars_at(
tl <> string.repeat(h, width - 2) <> tr,
x,
y,
))
// Add sides of box
let tree_with_sides =
list.range(1, height - 2)
|> list.map(fn(row) {
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.concat
// Add bottom of box
tree_with_sides
|> string_tree.append(internal.chars_at(
bl <> string.repeat(h, width - 2) <> br,
x,
y + height - 1,
))
|> string_tree.to_string
}

View File

@@ -3,15 +3,12 @@ import gleam/float
import gleam/int
import gleam/list
import gleam/string
import gleam/string_tree.{type StringTree}
import gleam/string_tree
import musicplayer/logging/logging
import musicplayer/ui/ansi
import musicplayer/ui/internal
pub type Layout {
Layout(width: Int, height: Int, nodes: dict.Dict(Section, Node))
}
pub type Section {
Section(String)
@@ -21,22 +18,6 @@ pub type Section {
PlaybackTime
}
// pub type Section {
// Root
// Header
// Search
// PlaybackTime
// Test
// Row1
// A
// B
// Row2
// C
// D
// }
pub type NodeType {
Container
Row
@@ -54,67 +35,54 @@ pub type Node {
)
}
pub type Layout {
Layout(width: Int, height: Int, nodes: dict.Dict(Section, Node))
}
pub fn new() -> Layout {
let nodes =
dict.from_list([
#(
Section("Root"),
Root,
Node(
t: Container,
content: "container",
content: "Music Player",
width_percent: 100,
height_percent: 100,
children: [
Section("Row1"),
Section("Row2"),
],
children: [Header, Search, PlaybackTime],
),
),
#(
Section("Row1"),
Header,
Node(
t: Row,
content: "row 1",
content: "Foo (1) | Bar (2) | Baz (3)",
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,
height_percent: 33,
children: [],
),
),
#(
Section("B"),
Search,
Node(
t: Cell,
content: "cell 2",
width_percent: 50,
height_percent: 100,
t: Row,
content: "",
width_percent: 100,
height_percent: 33,
children: [],
),
),
#(
Section("Row2"),
PlaybackTime,
Node(
t: Row,
content: "row 1",
content: "00:00",
width_percent: 100,
height_percent: 50,
height_percent: 33,
children: [],
),
),
])
Layout(width: 0, height: 0, nodes: nodes)
}
@@ -154,39 +122,81 @@ pub fn render(layout: Layout) -> Nil {
let container_top_left_x = 1
let container_top_left_y = 1
let ansi_renders =
Renders(
box: fn(tree, x, y, w, h) {
string_tree.append(tree, ansi.box(x, y, w, h))
},
text: fn(tree, chars, x, y) {
string_tree.append(tree, ansi.text(chars, x, y))
},
)
string_tree.new()
|> render_loop(
|> render_generic(
layout,
container_width,
container_height,
container_top_left_x,
container_top_left_y,
0,
Section("Root"),
Root,
_,
ansi_renders,
)
|> string_tree.to_string
|> internal.print
}
pub fn render_loop(
pub type Renders(into) {
Renders(
text: fn(into, String, Int, Int) -> into,
box: fn(into, Int, Int, Int, Int) -> into,
)
}
pub fn render_generic(
layout: Layout,
// Dimensions
container_width: Float,
container_height: Float,
container_top_left_x: Int,
container_top_left_y: Int,
container_tl_x: Int,
container_tl_y: Int,
// State
index: Int,
from: Section,
tree: StringTree,
) -> StringTree {
render_into: into,
renders: Renders(into),
) -> into {
case dict.get(layout.nodes, from) {
Error(_) -> render_into
Ok(node) -> {
let margin = 2.0
case dict.get(layout.nodes, from) {
Error(_) -> tree
Ok(node) -> {
let final_tree =
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
let #(cx, cy) = case node.t {
Container -> #(container_tl_x, container_tl_y)
Row -> #(container_tl_x, container_tl_y + { index * height })
Cell -> #(container_tl_x + { index * width }, container_tl_y)
}
let parent =
render_into
|> renders.box(cx, cy, width, height)
// + 2 for header margin
|> renders.text(node.content, cx + 2, cy)
list.index_map(node.children, fn(child, i) { #(i, child) })
|> list.fold(tree, fn(updated_tree: StringTree, ic: #(Int, Section)) {
|> list.fold(parent, fn(acc_into, ic) {
let #(i, child) = ic
let cw =
@@ -201,79 +211,21 @@ pub fn render_loop(
-. 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)
let child_origin_x = container_tl_x + 1
let child_origin_y = container_tl_y + 1
render_generic(
layout,
cw,
ch,
child_origin_x,
child_origin_y,
i,
child,
acc_into,
renders,
)
})
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

@@ -0,0 +1,82 @@
import gleam/dict
import musicplayer/ui/internal
import musicplayer/ui/layout.{Container, Layout, Node, Section}
import musicplayer/ui/layout_examples/wait_for_input.{wait_for_input}
pub fn main() {
let assert Ok(width) = internal.io_get_columns()
let assert Ok(height) = internal.io_get_rows()
two_rows_with_cells(width, height)
|> layout.render
wait_for_input()
}
/// Two rows:
/// First row has two cells
/// Second row has no cells
fn two_rows_with_cells(width: Int, height: Int) -> layout.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: layout.Row,
content: "row 1",
width_percent: 100,
height_percent: 50,
children: [
Section("A"),
Section("A"),
],
),
),
#(
Section("A"),
Node(
t: layout.Cell,
content: "cell 1",
width_percent: 50,
height_percent: 100,
children: [],
),
),
#(
Section("B"),
Node(
t: layout.Cell,
content: "cell 2",
width_percent: 50,
height_percent: 100,
children: [],
),
),
#(
Section("Row2"),
Node(
t: layout.Row,
content: "row 2",
width_percent: 100,
height_percent: 50,
children: [],
),
),
])
Layout(width:, height:, nodes: nodes)
}

View File

@@ -0,0 +1,15 @@
import gleam/erlang/process.{type Name}
import musicplayer/input/input
import musicplayer/input/key.{type Key}
pub fn wait_for_input() {
let input_keys_name: Name(Key) = process.new_name("input_keys")
let assert Ok(_) = process.register(process.self(), input_keys_name)
input.new(input_keys_name)
process.new_selector()
|> process.select(process.named_subject(input_keys_name))
|> process.selector_receive_forever
}

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}
@@ -104,28 +103,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 +1,118 @@
// import gleam/dict
// import gleam/int
// import gleeunit
import gleam/dict
import gleam/io
import gleam/string
import gleeunit
import gleeunit/should
import musicplayer/ui/virtual_ansi
// import musicplayer/ui/layout.{type Node, Layout, Section}
import musicplayer/ui/layout.{Layout, Node, Section}
// pub fn main() -> Nil {
// gleeunit.main()
// }
pub fn main() -> Nil {
gleeunit.main()
}
// pub fn foo_test() {
// let expected = ""
pub fn foo_test() {
let layout =
Layout(
width: 80,
height: 20,
nodes: dict.from_list([
#(
Section("Root"),
Node(
t: layout.Container,
content: "container",
width_percent: 100,
height_percent: 100,
children: [
Section("Row1"),
Section("Row2"),
],
),
),
#(
Section("Row1"),
Node(
t: layout.Row,
content: "row 1",
width_percent: 100,
height_percent: 50,
children: [
Section("A"),
Section("B"),
],
),
),
#(
Section("A"),
Node(
t: layout.Cell,
content: "cell 1",
width_percent: 50,
height_percent: 100,
children: [],
),
),
#(
Section("B"),
Node(
t: layout.Cell,
content: "cell 2",
width_percent: 50,
height_percent: 100,
children: [],
),
),
#(
Section("Row2"),
Node(
t: layout.Row,
content: "row 1",
width_percent: 100,
height_percent: 50,
children: [],
),
),
]),
)
// 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 expected =
"
container──────────────────────────────────────────────────────────────────────┐
│row 1────────────────────────────────────────────────────────────────────────┐│
││cell 1───────────────────────────────┐cell 2───────────────────────────────┐││
│││ ││ │││
│││ ││ │││
│││ ││ │││
│││ ││ │││
│││ ││ │││
││└────────────────────────────────────┘└────────────────────────────────────┘││
│└────────────────────────────────────────────────────────────────────────────┘│
│row 1────────────────────────────────────────────────────────────────────────┐│
││ ││
││ ││
││ ││
││ ││
││ ││
││ ││
││ ││
│└────────────────────────────────────────────────────────────────────────────┘│
└──────────────────────────────────────────────────────────────────────────────┘
"
// 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 visual =
virtual_ansi.render(layout, Section("Root"), layout.width, layout.height)
case visual == string.trim(expected) {
True -> Nil
False -> {
io.println("Test failed")
io.println("Expected:")
io.println(string.trim(expected))
// let assert Ok(data) =
// layout.render_loop(layout, container_width, container_height)
// assert data == 123.456789
// }
io.println("Got:")
io.println(visual)
should.equal(visual, expected)
}
}
}

View File

@@ -0,0 +1,113 @@
import gleam/dict
import gleam/int
import gleam/list
import gleam/string
import musicplayer/ui/layout.{type Layout, type Section, Renders}
pub type Screen =
dict.Dict(#(Int, Int), String)
pub fn render(layout: Layout, root: Section, width: Int, height: Int) -> String {
let test_renders =
Renders(
box: fn(screen, x, y, w, h) { box(screen, x, y, w, h) },
text: fn(screen, chars, x, y) { text(screen, chars, x, y) },
)
let screen =
layout.render_generic(
layout,
int.to_float(width),
int.to_float(height),
1,
1,
0,
root,
dict.new(),
test_renders,
)
screen_to_string(screen)
}
pub fn screen_to_string(screen: Screen) -> String {
let keys = dict.keys(screen)
// Find the bounding box of the drawing
let max_x = list.fold(keys, 0, fn(m, k) { int.max(m, k.0) })
let max_y = list.fold(keys, 0, fn(m, k) { int.max(m, k.1) })
// We start from 1 because ANSI is 1-based
let min_y = list.fold(keys, 1000, fn(m, k) { int.min(m, k.1) })
list.range(min_y, max_y)
|> list.map(fn(y) {
list.range(1, max_x)
|> list.map(fn(x) {
case dict.get(screen, #(x, y)) {
Ok(char) -> char
Error(_) -> " "
// Fill gaps with space
}
})
|> string.join("")
})
|> string.join("\n")
}
pub fn text(screen: Screen, text: String, start_x: Int, y: Int) -> Screen {
// We use to_graphemes to ensure Unicode characters (like emoji or box lines)
// are treated as single visual units
text
|> string.to_graphemes
|> list.index_fold(screen, fn(acc, char, i) {
dict.insert(acc, #(start_x + i, y), char)
})
}
pub fn box(screen: Screen, x: Int, y: Int, w: Int, h: Int) -> Screen {
let box_chars = #("", "", "", "", "", "")
let #(tl, tr, bl, br, hor, ver) = box_chars
// Don't draw impossible boxes
case w < 2 || h < 2 {
True -> screen
False -> {
screen
// 1. Corners
|> dict.insert(#(x, y), tl)
|> dict.insert(#(x + w - 1, y), tr)
|> dict.insert(#(x, y + h - 1), bl)
|> dict.insert(#(x + w - 1, y + h - 1), br)
// 2. Top and Bottom edges
|> horizontal_line(x + 1, y, w - 2, hor)
|> horizontal_line(x + 1, y + h - 1, w - 2, hor)
// 3. Side edges
|> vertical_line(x, y + 1, h - 2, ver)
|> vertical_line(x + w - 1, y + 1, h - 2, ver)
}
}
}
fn horizontal_line(
screen: Screen,
x: Int,
y: Int,
len: Int,
char: String,
) -> Screen {
list.range(0, len - 1)
|> list.fold(screen, fn(acc, i) { dict.insert(acc, #(x + i, y), char) })
}
fn vertical_line(
screen: Screen,
x: Int,
y: Int,
len: Int,
char: String,
) -> Screen {
list.range(0, len - 1)
|> list.fold(screen, fn(acc, i) { dict.insert(acc, #(x, y + i), char) })
}