Compare commits
3 Commits
4752ce418b
...
layout
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6aa9cc28de | ||
|
|
3d7287902f | ||
|
|
deaa0fcf89 |
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user