3 Commits
main ... layout

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 48 additions and 23 deletions

View File

@@ -7,9 +7,13 @@ pub fn clear_screen() -> Nil {
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"
io.print(seq <> text)
seq <> chars
}
pub fn print(chars: String) -> Nil {
io.print(chars)
}
pub fn hide_cursor() -> Nil {

View File

@@ -1,4 +1,8 @@
import gleam/dict
import gleam/list
import gleam/string_tree.{type StringTree}
import musicplayer/ui/internal
pub type Layout {
Layout(width: Int, height: Int, nodes: dict.Dict(Section, Node))
@@ -56,3 +60,31 @@ pub fn update_section(
pub fn update_dimensions(layout: Layout, width: Int, height: Int) -> Layout {
Layout(..layout, width:, height:)
}
pub fn render(layout: Layout, from: Section) -> Nil {
string_tree.new()
|> render_loop(layout, from, _)
|> string_tree.to_string
|> internal.print
}
pub fn render_loop(
layout: Layout,
from: Section,
into: StringTree,
) -> StringTree {
case dict.get(layout.nodes, from) {
Error(_) -> into
Ok(node) -> {
let parent =
string_tree.append(
into,
internal.chars_at(node.content, node.x, node.y),
)
list.fold(node.children, parent, fn(into_acc, child) {
render_loop(layout, child, into_acc)
})
}
}
}

View File

@@ -1,4 +1,3 @@
import gleam/dict
import gleam/erlang/process.{type Subject}
import gleam/int
import gleam/list
@@ -7,8 +6,8 @@ import gleam/string
import musicplayer/logging/logging
import musicplayer/ui/control.{type Control}
import musicplayer/ui/internal as ui_internal
import musicplayer/ui/layout.{type Layout, type Section}
import musicplayer/ui/internal
import musicplayer/ui/layout.{type Layout}
pub type State(redraw, content) {
State(redraw: Subject(Layout), layout: Layout)
@@ -36,8 +35,8 @@ pub fn new() -> Result(Subject(Control), String) {
process.spawn(fn() {
let assert Ok(_) = process.register(process.self(), redraw_name)
ui_internal.clear_screen()
ui_internal.hide_cursor()
internal.clear_screen()
internal.hide_cursor()
redraw_on_update_loop(redraw)
})
@@ -82,7 +81,7 @@ fn handle_message(
}
control.Exit(reply_to) -> {
ui_internal.show_cursor()
internal.show_cursor()
process.send(reply_to, Nil)
actor.stop()
}
@@ -92,14 +91,14 @@ fn handle_message(
fn redraw_on_update_loop(redraw: Subject(Layout)) -> Nil {
let layout = process.receive_forever(redraw)
ui_internal.clear_screen()
render_layout(layout, layout.Root)
internal.clear_screen()
layout.render(layout, layout.Root)
redraw_on_update_loop(redraw)
}
fn update_dimensions_on_interval(ui: Subject(Control), interval_ms: Int) {
case ui_internal.io_get_columns(), ui_internal.io_get_rows() {
case internal.io_get_columns(), internal.io_get_rows() {
Ok(width), Ok(height) -> {
process.send(ui, control.UpdateDimensions(width, height))
}
@@ -109,13 +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 {
case dict.get(layout.nodes, from) {
Error(_) -> Nil
Ok(node) -> {
list.each(node.children, fn(child) { render_layout(layout, child) })
ui_internal.print_at(node.content, node.x, node.y)
}
}
}

View File

@@ -1,6 +1,6 @@
import gleeunit
import musicplayer/mpv/internal as control_internal
import musicplayer/mpv/internal
pub fn main() -> Nil {
gleeunit.main()
@@ -10,6 +10,6 @@ pub fn parse_playback_time_test() {
let json_string =
"{\"data\":\"123.456789\",\"request_id\":0,\"error\":\"success\"}\n"
let assert Ok(data) = control_internal.parse_playback_time(json_string)
let assert Ok(data) = internal.parse_playback_time(json_string)
assert data == 123.456789
}