Compare commits
2 Commits
82a8800362
...
8134f7c3d6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8134f7c3d6 | ||
|
|
661b8f5e82 |
@@ -13,7 +13,7 @@ pub fn main() -> Nil {
|
|||||||
let input_keys_name: Name(Key) = process.new_name("input_keys")
|
let input_keys_name: Name(Key) = process.new_name("input_keys")
|
||||||
input.new(input_keys_name)
|
input.new(input_keys_name)
|
||||||
|
|
||||||
let assert Ok(ui) = ui.new()
|
let assert Ok(ui) = ui.new(logger)
|
||||||
let assert Ok(mpv) = mpv.new()
|
let assert Ok(mpv) = mpv.new()
|
||||||
let assert Ok(musicplayer_pid) =
|
let assert Ok(musicplayer_pid) =
|
||||||
musicplayer.new(logger, ui, mpv, input_keys_name)
|
musicplayer.new(logger, ui, mpv, input_keys_name)
|
||||||
|
|||||||
@@ -3,6 +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(width: Int, height: Int)
|
||||||
UpdateState(section: Section, content: String)
|
UpdateState(section: Section, content: String)
|
||||||
|
|
||||||
Exit(reply_to: Subject(Nil))
|
Exit(reply_to: Subject(Nil))
|
||||||
|
|||||||
@@ -19,3 +19,13 @@ pub fn hide_cursor() -> Nil {
|
|||||||
pub fn show_cursor() -> Nil {
|
pub fn show_cursor() -> Nil {
|
||||||
io.print("\u{001B}[?25h")
|
io.print("\u{001B}[?25h")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type Enotsup
|
||||||
|
|
||||||
|
// https://www.erlang.org/doc/apps/stdlib/io.html#rows/0
|
||||||
|
@external(erlang, "io", "rows")
|
||||||
|
pub fn io_get_rows() -> Result(Int, Enotsup)
|
||||||
|
|
||||||
|
// https://www.erlang.org/doc/apps/stdlib/io.html#columns/0
|
||||||
|
@external(erlang, "io", "columns")
|
||||||
|
pub fn io_get_columns() -> Result(Int, Enotsup)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import gleam/dict
|
import gleam/dict
|
||||||
|
|
||||||
pub type Layout {
|
pub type Layout {
|
||||||
Layout(nodes: dict.Dict(Section, Node))
|
Layout(width: Int, height: Int, nodes: dict.Dict(Section, Node))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Section {
|
pub type Section {
|
||||||
@@ -31,7 +31,7 @@ pub fn new() -> Layout {
|
|||||||
#(PlaybackTime, Node(content: "00:00", x: 1, y: 2, children: [])),
|
#(PlaybackTime, Node(content: "00:00", x: 1, y: 2, children: [])),
|
||||||
])
|
])
|
||||||
|
|
||||||
Layout(nodes: nodes)
|
Layout(0, 0, nodes: nodes)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_section(
|
pub fn update_section(
|
||||||
@@ -42,10 +42,17 @@ pub fn update_section(
|
|||||||
case dict.get(layout.nodes, section) {
|
case dict.get(layout.nodes, section) {
|
||||||
Error(_) -> layout
|
Error(_) -> layout
|
||||||
Ok(node) ->
|
Ok(node) ->
|
||||||
Layout(nodes: dict.insert(
|
Layout(
|
||||||
layout.nodes,
|
..layout,
|
||||||
section,
|
nodes: dict.insert(
|
||||||
Node(..node, content: content),
|
layout.nodes,
|
||||||
))
|
section,
|
||||||
|
Node(..node, content: content),
|
||||||
|
),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn update_dimensions(layout: Layout, width: Int, height: Int) -> Layout {
|
||||||
|
Layout(..layout, width:, height:)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,31 +1,45 @@
|
|||||||
import gleam/dict
|
import gleam/dict
|
||||||
import gleam/erlang/process.{type Subject}
|
import gleam/erlang/process.{type Subject}
|
||||||
|
import gleam/int
|
||||||
import gleam/list
|
import gleam/list
|
||||||
import gleam/otp/actor
|
import gleam/otp/actor
|
||||||
import gleam/string
|
import gleam/string
|
||||||
|
|
||||||
|
import musicplayer/logging/control as logging_control
|
||||||
|
import musicplayer/logging/logging
|
||||||
import musicplayer/ui/control.{type Control}
|
import musicplayer/ui/control.{type Control}
|
||||||
import musicplayer/ui/internal as ui_internal
|
import musicplayer/ui/internal as ui_internal
|
||||||
import musicplayer/ui/layout.{type Layout, type Section}
|
import musicplayer/ui/layout.{type Layout, type Section}
|
||||||
|
|
||||||
pub type State(redraw, content) {
|
pub type State(redraw, content) {
|
||||||
State(redraw: Subject(Layout), layout: Layout)
|
State(
|
||||||
|
logger: Subject(logging_control.Control),
|
||||||
|
redraw: Subject(Layout),
|
||||||
|
layout: Layout,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new() -> Result(Subject(Control), String) {
|
pub fn new(
|
||||||
|
logger: Subject(logging_control.Control),
|
||||||
|
) -> 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 = layout.new()
|
let layout = layout.new()
|
||||||
|
|
||||||
case
|
case
|
||||||
actor.new(State(redraw, layout))
|
actor.new(State(logger, redraw, layout))
|
||||||
|> actor.on_message(handle_message)
|
|> actor.on_message(handle_message)
|
||||||
|> actor.start
|
|> actor.start
|
||||||
{
|
{
|
||||||
Error(start_error) ->
|
Error(start_error) ->
|
||||||
Error("Could not start actor: " <> string.inspect(start_error))
|
Error("Could not start actor: " <> string.inspect(start_error))
|
||||||
Ok(actor.Started(data: ui, ..)) -> {
|
Ok(actor.Started(data: ui, ..)) -> {
|
||||||
|
process.spawn(fn() {
|
||||||
|
let update_dimensions_interval_ms = 300
|
||||||
|
update_dimensions_on_interval(logger, ui, update_dimensions_interval_ms)
|
||||||
|
})
|
||||||
|
|
||||||
process.spawn(fn() {
|
process.spawn(fn() {
|
||||||
let assert Ok(_) = process.register(process.self(), redraw_name)
|
let assert Ok(_) = process.register(process.self(), redraw_name)
|
||||||
|
|
||||||
@@ -45,6 +59,27 @@ 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(width, height) -> {
|
||||||
|
let current_dimensions = #(state.layout.width, state.layout.height)
|
||||||
|
|
||||||
|
case #(width, height) == current_dimensions {
|
||||||
|
True -> actor.continue(state)
|
||||||
|
False -> {
|
||||||
|
[width, height]
|
||||||
|
|> list.map(int.to_string)
|
||||||
|
|> string.join(" ")
|
||||||
|
|> string.append("ui - updating dimensions: ", _)
|
||||||
|
|> logging.log(state.logger, _)
|
||||||
|
|
||||||
|
actor.continue(
|
||||||
|
State(
|
||||||
|
..state,
|
||||||
|
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:)
|
let state = State(..state, layout:)
|
||||||
@@ -70,6 +105,22 @@ fn redraw_on_update_loop(redraw: Subject(Layout)) -> Nil {
|
|||||||
redraw_on_update_loop(redraw)
|
redraw_on_update_loop(redraw)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update_dimensions_on_interval(
|
||||||
|
logger: Subject(logging_control.Control),
|
||||||
|
ui: Subject(Control),
|
||||||
|
interval_ms: Int,
|
||||||
|
) {
|
||||||
|
case ui_internal.io_get_columns(), ui_internal.io_get_rows() {
|
||||||
|
Ok(width), Ok(height) -> {
|
||||||
|
process.send(ui, control.UpdateDimensions(width, height))
|
||||||
|
}
|
||||||
|
_, _ -> logging.log(logger, "ui - failed to update dimensions")
|
||||||
|
}
|
||||||
|
|
||||||
|
process.sleep(interval_ms)
|
||||||
|
update_dimensions_on_interval(logger, ui, interval_ms)
|
||||||
|
}
|
||||||
|
|
||||||
fn render_layout(layout: Layout, from: Section) -> Nil {
|
fn render_layout(layout: Layout, from: Section) -> Nil {
|
||||||
case dict.get(layout.nodes, from) {
|
case dict.get(layout.nodes, from) {
|
||||||
Error(_) -> Nil
|
Error(_) -> Nil
|
||||||
|
|||||||
Reference in New Issue
Block a user