Compare commits
3 Commits
b5965360eb
...
layout
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6aa9cc28de | ||
|
|
3d7287902f | ||
|
|
deaa0fcf89 |
@@ -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)
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -3,7 +3,7 @@ import gleam/erlang/process.{type Subject}
|
|||||||
import musicplayer/ui/layout.{type Section}
|
import musicplayer/ui/layout.{type Section}
|
||||||
|
|
||||||
pub type Control {
|
pub type Control {
|
||||||
UpdateDimensions(columns: Int, rows: Int)
|
UpdateDimensions(width: Int, height: Int)
|
||||||
UpdateState(section: Section, content: String)
|
UpdateState(section: Section, content: String)
|
||||||
|
|
||||||
Exit(reply_to: Subject(Nil))
|
Exit(reply_to: Subject(Nil))
|
||||||
|
|||||||
@@ -1,73 +1,41 @@
|
|||||||
import gleam/dict
|
import gleam/dict
|
||||||
import gleam/int
|
|
||||||
import gleam/list
|
import gleam/list
|
||||||
import gleam/pair
|
import gleam/string_tree.{type StringTree}
|
||||||
import gleam/set
|
|
||||||
import gleam/string
|
|
||||||
import gleam/string_tree
|
|
||||||
|
|
||||||
import musicplayer/logging/logging
|
|
||||||
import musicplayer/ui/ansi
|
|
||||||
import musicplayer/ui/internal
|
import musicplayer/ui/internal
|
||||||
|
|
||||||
pub const root_section = "reserved_root_section"
|
pub type Layout {
|
||||||
|
Layout(width: Int, height: Int, nodes: dict.Dict(Section, Node))
|
||||||
|
}
|
||||||
|
|
||||||
pub type Section {
|
pub type Section {
|
||||||
Section(String)
|
Root
|
||||||
|
|
||||||
Header
|
Header
|
||||||
Search
|
Search
|
||||||
PlaybackTime
|
PlaybackTime
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Dimension {
|
|
||||||
Percent(width: Int, height: Int)
|
|
||||||
// TODO add Flex that flows
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type Style {
|
|
||||||
Style(dimensions: Dimension)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type Node {
|
pub type Node {
|
||||||
Row(content: String, style: Style, children: List(Section))
|
Node(content: String, x: Int, y: Int, children: List(Section))
|
||||||
Cell(content: String, style: Style)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Layout {
|
pub fn new() -> Layout {
|
||||||
Layout(columns: Int, rows: Int, nodes: dict.Dict(Section, Node))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new(columns: Int, rows: Int, nodes: List(#(Section, Node))) -> Layout {
|
|
||||||
let children =
|
|
||||||
nodes
|
|
||||||
|> list.flat_map(fn(node) {
|
|
||||||
case pair.second(node) {
|
|
||||||
Row(children: c, ..) -> c
|
|
||||||
Cell(..) -> []
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|> set.from_list
|
|
||||||
|
|
||||||
// All sections that are not children of other nodes will be added as
|
|
||||||
// children to the root
|
|
||||||
let orphans =
|
|
||||||
nodes
|
|
||||||
|> list.map(pair.first)
|
|
||||||
|> list.filter(fn(node) { !set.contains(children, node) })
|
|
||||||
|
|
||||||
let nodes =
|
let nodes =
|
||||||
dict.from_list(nodes)
|
dict.from_list([
|
||||||
|> dict.insert(
|
#(
|
||||||
Section(root_section),
|
Root,
|
||||||
Row(
|
Node(content: "", x: 0, y: 0, children: [
|
||||||
content: "",
|
Header,
|
||||||
style: Style(dimensions: Percent(width: 100, height: 100)),
|
Search,
|
||||||
children: orphans,
|
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(columns:, rows:, nodes:)
|
Layout(0, 0, nodes: nodes)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_section(
|
pub fn update_section(
|
||||||
@@ -77,148 +45,46 @@ pub fn update_section(
|
|||||||
) -> Layout {
|
) -> Layout {
|
||||||
case dict.get(layout.nodes, section) {
|
case dict.get(layout.nodes, section) {
|
||||||
Error(_) -> layout
|
Error(_) -> layout
|
||||||
Ok(node) -> {
|
Ok(node) ->
|
||||||
let updated = case node {
|
Layout(
|
||||||
Cell(..) -> Cell(..node, content: content)
|
..layout,
|
||||||
Row(..) -> Row(..node, content: content)
|
nodes: dict.insert(
|
||||||
}
|
layout.nodes,
|
||||||
|
section,
|
||||||
Layout(..layout, nodes: dict.insert(layout.nodes, section, updated))
|
Node(..node, content: content),
|
||||||
}
|
),
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update_dimensions(layout: Layout, columns: Int, rows: Int) -> Layout {
|
|
||||||
Layout(..layout, columns:, rows:)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn render(layout: Layout) -> Nil {
|
|
||||||
internal.clear_screen()
|
|
||||||
|
|
||||||
[layout.columns, layout.rows]
|
|
||||||
|> list.map(int.to_string)
|
|
||||||
|> string.join(" ")
|
|
||||||
|> string.append("layout - render: ", _)
|
|
||||||
|> logging.log
|
|
||||||
|
|
||||||
let context =
|
|
||||||
RenderContext(
|
|
||||||
parent_width: layout.columns,
|
|
||||||
parent_height: layout.rows,
|
|
||||||
parent_top_left_x: 1,
|
|
||||||
parent_top_left_y: 1,
|
|
||||||
position_index: 0,
|
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let ansi_renders =
|
pub fn update_dimensions(layout: Layout, width: Int, height: Int) -> Layout {
|
||||||
Renders(
|
Layout(..layout, width:, height:)
|
||||||
text: fn(tree, chars, x, y) {
|
}
|
||||||
string_tree.append(tree, ansi.text(chars, x, y))
|
|
||||||
},
|
|
||||||
box: fn(tree, x, y, w, h) {
|
|
||||||
string_tree.append(tree, ansi.box(x, y, w, h))
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
pub fn render(layout: Layout, from: Section) -> Nil {
|
||||||
string_tree.new()
|
string_tree.new()
|
||||||
|> render_generic(layout, context, Section(root_section), _, ansi_renders)
|
|> render_loop(layout, from, _)
|
||||||
|> string_tree.to_string
|
|> string_tree.to_string
|
||||||
|> internal.print
|
|> internal.print
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Renders(into) {
|
pub fn render_loop(
|
||||||
Renders(
|
|
||||||
// into, chars, x, y
|
|
||||||
text: fn(into, String, Int, Int) -> into,
|
|
||||||
// into, x, y, width, height
|
|
||||||
box: fn(into, Int, Int, Int, Int) -> into,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type RenderContext {
|
|
||||||
RenderContext(
|
|
||||||
parent_width: Int,
|
|
||||||
parent_height: Int,
|
|
||||||
parent_top_left_x: Int,
|
|
||||||
parent_top_left_y: Int,
|
|
||||||
position_index: Int,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn render_generic(
|
|
||||||
layout: Layout,
|
layout: Layout,
|
||||||
context: RenderContext,
|
|
||||||
from: Section,
|
from: Section,
|
||||||
render_into: into,
|
into: StringTree,
|
||||||
renders: Renders(into),
|
) -> StringTree {
|
||||||
) -> into {
|
|
||||||
case dict.get(layout.nodes, from) {
|
case dict.get(layout.nodes, from) {
|
||||||
Error(_) -> render_into
|
Error(_) -> into
|
||||||
Ok(node) -> {
|
Ok(node) -> {
|
||||||
// Margin between container and the node being rendere
|
|
||||||
let margin = 2
|
|
||||||
|
|
||||||
let #(node_width, node_height) = case node.style.dimensions {
|
|
||||||
Percent(width:, height:) -> {
|
|
||||||
let width = { context.parent_width * width } / 100
|
|
||||||
let height = { context.parent_height * height } / 100
|
|
||||||
|
|
||||||
#(width, height)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if this node should be placed to the left or below the parent
|
|
||||||
let #(node_top_left_x, node_top_left_y) = case node {
|
|
||||||
Row(..) -> #(
|
|
||||||
context.parent_top_left_x,
|
|
||||||
context.parent_top_left_y + { context.position_index * node_height },
|
|
||||||
)
|
|
||||||
Cell(..) -> #(
|
|
||||||
context.parent_top_left_x + { context.position_index * node_width },
|
|
||||||
context.parent_top_left_y,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
let parent =
|
let parent =
|
||||||
render_into
|
string_tree.append(
|
||||||
|> renders.box(
|
into,
|
||||||
node_top_left_x,
|
internal.chars_at(node.content, node.x, node.y),
|
||||||
node_top_left_y,
|
|
||||||
node_width,
|
|
||||||
node_height,
|
|
||||||
)
|
|
||||||
|> renders.text(node.content, node_top_left_x, node_top_left_y)
|
|
||||||
|
|
||||||
case node {
|
|
||||||
Cell(..) -> parent
|
|
||||||
Row(children:, ..) -> {
|
|
||||||
list.index_map(children, fn(child, i) { #(i, child) })
|
|
||||||
|> list.fold(parent, fn(acc_into, ic) {
|
|
||||||
let #(i, child) = ic
|
|
||||||
|
|
||||||
let #(child_width, child_height) = case node.style.dimensions {
|
|
||||||
Percent(width:, height:) -> {
|
|
||||||
let width = { { context.parent_width * width } / 100 } - margin
|
|
||||||
let height =
|
|
||||||
{ { context.parent_height * height } / 100 } - margin
|
|
||||||
|
|
||||||
#(width, height)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let context =
|
|
||||||
RenderContext(
|
|
||||||
parent_width: child_width,
|
|
||||||
parent_height: child_height,
|
|
||||||
parent_top_left_x: context.parent_top_left_x + 1,
|
|
||||||
parent_top_left_y: context.parent_top_left_y + 1,
|
|
||||||
position_index: i,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
render_generic(layout, context, child, acc_into, renders)
|
list.fold(node.children, parent, fn(into_acc, child) {
|
||||||
|
render_loop(layout, child, into_acc)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,56 +0,0 @@
|
|||||||
import musicplayer/ui/internal
|
|
||||||
import musicplayer/ui/layout.{Percent, Section, Style}
|
|
||||||
import musicplayer/ui/layout_examples/wait_for_input.{wait_for_input}
|
|
||||||
|
|
||||||
pub fn main() {
|
|
||||||
let assert Ok(columns) = internal.io_get_columns()
|
|
||||||
let assert Ok(rows) = internal.io_get_rows()
|
|
||||||
|
|
||||||
two_rows_with_cells(columns, rows)
|
|
||||||
|> layout.render
|
|
||||||
|
|
||||||
wait_for_input()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Two rows:
|
|
||||||
/// First row has two cells
|
|
||||||
/// Second row has no cells
|
|
||||||
fn two_rows_with_cells(columns: Int, rows: Int) -> layout.Layout {
|
|
||||||
let nodes = [
|
|
||||||
#(
|
|
||||||
Section("Row1"),
|
|
||||||
layout.Row(
|
|
||||||
content: "row 1",
|
|
||||||
style: Style(dimensions: Percent(width: 100, height: 50)),
|
|
||||||
children: [
|
|
||||||
Section("A"),
|
|
||||||
Section("B"),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
#(
|
|
||||||
Section("A"),
|
|
||||||
layout.Cell(
|
|
||||||
content: "cell 1",
|
|
||||||
style: Style(dimensions: Percent(width: 50, height: 50)),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
#(
|
|
||||||
Section("B"),
|
|
||||||
layout.Cell(
|
|
||||||
content: "cell 2",
|
|
||||||
style: Style(dimensions: Percent(width: 50, height: 50)),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
#(
|
|
||||||
Section("Row2"),
|
|
||||||
layout.Row(
|
|
||||||
content: "row 2",
|
|
||||||
style: Style(dimensions: Percent(width: 50, height: 50)),
|
|
||||||
children: [],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
]
|
|
||||||
|
|
||||||
layout.new(columns, rows, nodes)
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -17,34 +17,7 @@ pub fn new() -> Result(Subject(Control), String) {
|
|||||||
let redraw_name = process.new_name("redraw")
|
let redraw_name = process.new_name("redraw")
|
||||||
let redraw: Subject(Layout) = process.named_subject(redraw_name)
|
let redraw: Subject(Layout) = process.named_subject(redraw_name)
|
||||||
|
|
||||||
let layout =
|
let layout = layout.new()
|
||||||
[
|
|
||||||
#(
|
|
||||||
layout.Header,
|
|
||||||
layout.Row(
|
|
||||||
content: "Foo (1) | Bar (2) | Baz (3)",
|
|
||||||
style: layout.Style(dimensions: layout.Percent(width: 100, height: 33)),
|
|
||||||
children: [],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
#(
|
|
||||||
layout.Search,
|
|
||||||
layout.Row(
|
|
||||||
content: "",
|
|
||||||
style: layout.Style(dimensions: layout.Percent(width: 100, height: 33)),
|
|
||||||
children: [],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
#(
|
|
||||||
layout.PlaybackTime,
|
|
||||||
layout.Row(
|
|
||||||
content: "00:00",
|
|
||||||
style: layout.Style(dimensions: layout.Percent(width: 100, height: 33)),
|
|
||||||
children: [],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
]
|
|
||||||
|> layout.new(0, 0, _)
|
|
||||||
|
|
||||||
case
|
case
|
||||||
actor.new(State(redraw, layout))
|
actor.new(State(redraw, layout))
|
||||||
@@ -65,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)
|
||||||
@@ -78,30 +51,33 @@ fn handle_message(
|
|||||||
control: Control,
|
control: Control,
|
||||||
) -> actor.Next(State(redraw, layout), Control) {
|
) -> actor.Next(State(redraw, layout), Control) {
|
||||||
case control {
|
case control {
|
||||||
control.UpdateDimensions(columns, rows) -> {
|
control.UpdateDimensions(width, height) -> {
|
||||||
let current_dimensions = #(state.layout.columns, state.layout.rows)
|
let current_dimensions = #(state.layout.width, state.layout.height)
|
||||||
|
|
||||||
case #(columns, rows) == current_dimensions {
|
case #(width, height) == current_dimensions {
|
||||||
True -> actor.continue(state)
|
True -> actor.continue(state)
|
||||||
False -> {
|
False -> {
|
||||||
[columns, rows]
|
[width, height]
|
||||||
|> list.map(int.to_string)
|
|> list.map(int.to_string)
|
||||||
|> string.join(" ")
|
|> string.join(" ")
|
||||||
|> string.append("ui - updating dimensions: ", _)
|
|> string.append("ui - updating dimensions: ", _)
|
||||||
|> logging.log
|
|> logging.log
|
||||||
|
|
||||||
let layout = layout.update_dimensions(state.layout, columns, rows)
|
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) -> {
|
||||||
@@ -112,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) {
|
||||||
|
|||||||
@@ -1,92 +0,0 @@
|
|||||||
import gleam/io
|
|
||||||
import gleam/string
|
|
||||||
import gleeunit
|
|
||||||
import gleeunit/should
|
|
||||||
import musicplayer/ui/virtual_ansi
|
|
||||||
|
|
||||||
import musicplayer/ui/layout.{Percent, Section, Style}
|
|
||||||
|
|
||||||
pub fn main() -> Nil {
|
|
||||||
gleeunit.main()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn percent_layout_test() {
|
|
||||||
let nodes = [
|
|
||||||
#(
|
|
||||||
Section("Row1"),
|
|
||||||
layout.Row(
|
|
||||||
content: "row 1",
|
|
||||||
style: Style(dimensions: Percent(width: 100, height: 50)),
|
|
||||||
children: [
|
|
||||||
Section("A"),
|
|
||||||
Section("B"),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
#(
|
|
||||||
Section("A"),
|
|
||||||
layout.Cell(
|
|
||||||
content: "cell 1",
|
|
||||||
style: Style(dimensions: Percent(width: 50, height: 100)),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
#(
|
|
||||||
Section("B"),
|
|
||||||
layout.Cell(
|
|
||||||
content: "cell 2",
|
|
||||||
style: Style(dimensions: Percent(width: 50, height: 100)),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
#(
|
|
||||||
Section("Row2"),
|
|
||||||
layout.Row(
|
|
||||||
content: "row 1",
|
|
||||||
style: Style(dimensions: Percent(width: 100, height: 50)),
|
|
||||||
children: [],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
]
|
|
||||||
|
|
||||||
let columns = 80
|
|
||||||
let rows = 20
|
|
||||||
let layout = layout.new(columns, rows, nodes)
|
|
||||||
|
|
||||||
let expected =
|
|
||||||
"
|
|
||||||
┌──────────────────────────────────────────────────────────────────────────────┐
|
|
||||||
│row 1────────────────────────────────────────────────────────────────────────┐│
|
|
||||||
││cell 1───────────────────────────────┐cell 2───────────────────────────────┐││
|
|
||||||
│││ ││ │││
|
|
||||||
│││ ││ │││
|
|
||||||
│││ ││ │││
|
|
||||||
│││ ││ │││
|
|
||||||
│││ ││ │││
|
|
||||||
││└────────────────────────────────────┘└────────────────────────────────────┘││
|
|
||||||
│└────────────────────────────────────────────────────────────────────────────┘│
|
|
||||||
│row 1────────────────────────────────────────────────────────────────────────┐│
|
|
||||||
││ ││
|
|
||||||
││ ││
|
|
||||||
││ ││
|
|
||||||
││ ││
|
|
||||||
││ ││
|
|
||||||
││ ││
|
|
||||||
││ ││
|
|
||||||
│└────────────────────────────────────────────────────────────────────────────┘│
|
|
||||||
└──────────────────────────────────────────────────────────────────────────────┘
|
|
||||||
"
|
|
||||||
|
|
||||||
let visual = virtual_ansi.render(layout)
|
|
||||||
case visual == string.trim(expected) {
|
|
||||||
True -> Nil
|
|
||||||
False -> {
|
|
||||||
io.println("Test failed")
|
|
||||||
io.println("Expected:")
|
|
||||||
io.println(string.trim(expected))
|
|
||||||
|
|
||||||
io.println("Got:")
|
|
||||||
io.println(visual)
|
|
||||||
|
|
||||||
should.equal(visual, expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,118 +0,0 @@
|
|||||||
import gleam/dict
|
|
||||||
import gleam/int
|
|
||||||
import gleam/list
|
|
||||||
import gleam/string
|
|
||||||
|
|
||||||
import musicplayer/ui/layout.{type Layout, RenderContext, Renders}
|
|
||||||
|
|
||||||
pub type Screen =
|
|
||||||
dict.Dict(#(Int, Int), String)
|
|
||||||
|
|
||||||
pub fn render(layout: Layout) -> String {
|
|
||||||
let test_renders =
|
|
||||||
Renders(
|
|
||||||
text: fn(screen, chars, x, y) { text(screen, chars, x, y) },
|
|
||||||
box: fn(screen, x, y, w, h) { box(screen, x, y, w, h) },
|
|
||||||
)
|
|
||||||
|
|
||||||
let context =
|
|
||||||
RenderContext(
|
|
||||||
parent_width: layout.columns,
|
|
||||||
parent_height: layout.rows,
|
|
||||||
parent_top_left_x: 1,
|
|
||||||
parent_top_left_y: 1,
|
|
||||||
position_index: 0,
|
|
||||||
)
|
|
||||||
|
|
||||||
let screen =
|
|
||||||
layout.render_generic(
|
|
||||||
layout,
|
|
||||||
context,
|
|
||||||
layout.Section(layout.root_section),
|
|
||||||
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) })
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user