Compare commits
18 Commits
0877344a94
...
dynamic-la
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
89ef014c98 | ||
|
|
0025772177 | ||
|
|
033beb6b1f | ||
|
|
3589d94c29 | ||
|
|
59429d7721 | ||
|
|
7212df3abb | ||
|
|
1732b12fbe | ||
|
|
50c053a42a | ||
|
|
2332710235 | ||
|
|
18c4793872 | ||
|
|
4752ce418b | ||
|
|
df9160b932 | ||
|
|
f9d7b573ac | ||
|
|
8134f7c3d6 | ||
|
|
661b8f5e82 | ||
|
|
82a8800362 | ||
|
|
1d12f46d2c | ||
|
|
610967b7be |
@@ -18,6 +18,7 @@ gleam_otp = ">= 1.2.0 and < 2.0.0"
|
|||||||
gleam_erlang = ">= 1.3.0 and < 2.0.0"
|
gleam_erlang = ">= 1.3.0 and < 2.0.0"
|
||||||
simplifile = ">= 2.3.1 and < 3.0.0"
|
simplifile = ">= 2.3.1 and < 3.0.0"
|
||||||
gleam_json = ">= 3.1.0 and < 4.0.0"
|
gleam_json = ">= 3.1.0 and < 4.0.0"
|
||||||
|
gleam_time = ">= 1.6.0 and < 2.0.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
gleeunit = ">= 1.0.0 and < 2.0.0"
|
gleeunit = ">= 1.0.0 and < 2.0.0"
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ packages = [
|
|||||||
{ name = "gleam_json", version = "3.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "44FDAA8847BE8FC48CA7A1C089706BD54BADCC4C45B237A992EDDF9F2CDB2836" },
|
{ name = "gleam_json", version = "3.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "44FDAA8847BE8FC48CA7A1C089706BD54BADCC4C45B237A992EDDF9F2CDB2836" },
|
||||||
{ name = "gleam_otp", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "BA6A294E295E428EC1562DC1C11EA7530DCB981E8359134BEABC8493B7B2258E" },
|
{ name = "gleam_otp", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "BA6A294E295E428EC1562DC1C11EA7530DCB981E8359134BEABC8493B7B2258E" },
|
||||||
{ name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" },
|
{ name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" },
|
||||||
|
{ name = "gleam_time", version = "1.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_time", source = "hex", outer_checksum = "0DF3834D20193F0A38D0EB21F0A78D48F2EC276C285969131B86DF8D4EF9E762" },
|
||||||
{ name = "gleeunit", version = "1.9.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "DA9553CE58B67924B3C631F96FE3370C49EB6D6DC6B384EC4862CC4AAA718F3C" },
|
{ name = "gleeunit", version = "1.9.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "DA9553CE58B67924B3C631F96FE3370C49EB6D6DC6B384EC4862CC4AAA718F3C" },
|
||||||
{ name = "simplifile", version = "2.3.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "957E0E5B75927659F1D2A1B7B75D7B9BA96FAA8D0C53EA71C4AD9CD0C6B848F6" },
|
{ name = "simplifile", version = "2.3.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "957E0E5B75927659F1D2A1B7B75D7B9BA96FAA8D0C53EA71C4AD9CD0C6B848F6" },
|
||||||
]
|
]
|
||||||
@@ -16,5 +17,6 @@ gleam_erlang = { version = ">= 1.3.0 and < 2.0.0" }
|
|||||||
gleam_json = { version = ">= 3.1.0 and < 4.0.0" }
|
gleam_json = { version = ">= 3.1.0 and < 4.0.0" }
|
||||||
gleam_otp = { version = ">= 1.2.0 and < 2.0.0" }
|
gleam_otp = { version = ">= 1.2.0 and < 2.0.0" }
|
||||||
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
|
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
|
||||||
|
gleam_time = { version = ">= 1.6.0 and < 2.0.0" }
|
||||||
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
|
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
|
||||||
simplifile = { version = ">= 2.3.1 and < 3.0.0" }
|
simplifile = { version = ">= 2.3.1 and < 3.0.0" }
|
||||||
|
|||||||
@@ -2,11 +2,14 @@ import gleam/erlang/process.{type Name}
|
|||||||
|
|
||||||
import musicplayer/input/input
|
import musicplayer/input/input
|
||||||
import musicplayer/input/key.{type Key}
|
import musicplayer/input/key.{type Key}
|
||||||
|
import musicplayer/logging/logging
|
||||||
import musicplayer/mpv/mpv
|
import musicplayer/mpv/mpv
|
||||||
import musicplayer/musicplayer
|
import musicplayer/musicplayer
|
||||||
import musicplayer/ui/ui
|
import musicplayer/ui/ui
|
||||||
|
|
||||||
pub fn main() -> Nil {
|
pub fn main() -> Nil {
|
||||||
|
let assert Ok(_) = logging.initialize()
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
|
|||||||
5
src/musicplayer/logging/control.gleam
Normal file
5
src/musicplayer/logging/control.gleam
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
pub type Control {
|
||||||
|
Write(String)
|
||||||
|
|
||||||
|
Exit
|
||||||
|
}
|
||||||
30
src/musicplayer/logging/logging.gleam
Normal file
30
src/musicplayer/logging/logging.gleam
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import gleam/result
|
||||||
|
import gleam/string
|
||||||
|
import gleam/time/calendar
|
||||||
|
import gleam/time/timestamp
|
||||||
|
import simplifile
|
||||||
|
|
||||||
|
const filepath = "/tmp/musicplayer.log"
|
||||||
|
|
||||||
|
pub fn initialize() -> Result(Nil, String) {
|
||||||
|
case simplifile.is_file(filepath) {
|
||||||
|
Ok(True) -> Ok(Nil)
|
||||||
|
_ -> simplifile.create_file(filepath)
|
||||||
|
}
|
||||||
|
|> result.map_error(fn(e) {
|
||||||
|
"Could not access or create log file: " <> string.inspect(e)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn log(content: String) -> Nil {
|
||||||
|
let log_line =
|
||||||
|
timestamp.system_time()
|
||||||
|
|> timestamp.to_rfc3339(calendar.utc_offset)
|
||||||
|
<> ": "
|
||||||
|
<> content
|
||||||
|
<> "\n"
|
||||||
|
|
||||||
|
// Ignore any logging errors
|
||||||
|
let _ = simplifile.append(filepath, log_line)
|
||||||
|
Nil
|
||||||
|
}
|
||||||
@@ -24,7 +24,7 @@ pub fn new() -> Result(Subject(Control), String) {
|
|||||||
|> actor.start
|
|> actor.start
|
||||||
{
|
{
|
||||||
Error(start_error) ->
|
Error(start_error) ->
|
||||||
Error("Could not start actor: " <> string.inspect(start_error))
|
Error("Could not start mpv: " <> string.inspect(start_error))
|
||||||
Ok(actor.Started(data: mpv, ..)) -> Ok(mpv)
|
Ok(actor.Started(data: mpv, ..)) -> Ok(mpv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,12 @@ import gleam/erlang/process.{type Name, type Pid, type Subject}
|
|||||||
import gleam/otp/actor
|
import gleam/otp/actor
|
||||||
import gleam/result
|
import gleam/result
|
||||||
import gleam/string
|
import gleam/string
|
||||||
import musicplayer/time/time
|
|
||||||
|
|
||||||
import musicplayer/control.{type Control}
|
import musicplayer/control.{type Control}
|
||||||
import musicplayer/input/key.{type Key}
|
import musicplayer/input/key.{type Key}
|
||||||
|
import musicplayer/logging/logging
|
||||||
import musicplayer/mpv/control as mpv_control
|
import musicplayer/mpv/control as mpv_control
|
||||||
|
import musicplayer/time/time
|
||||||
import musicplayer/ui/control as ui_control
|
import musicplayer/ui/control as ui_control
|
||||||
import musicplayer/ui/layout
|
import musicplayer/ui/layout
|
||||||
|
|
||||||
@@ -45,6 +46,7 @@ pub fn new(
|
|||||||
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(pid:, data: musicplayer)) -> {
|
Ok(actor.Started(pid:, data: musicplayer)) -> {
|
||||||
|
logging.log("musicplayer - started")
|
||||||
process.spawn(fn() {
|
process.spawn(fn() {
|
||||||
let assert Ok(_) = process.register(process.self(), input_keys_name)
|
let assert Ok(_) = process.register(process.self(), input_keys_name)
|
||||||
handle_key(musicplayer, input_keys)
|
handle_key(musicplayer, input_keys)
|
||||||
@@ -60,6 +62,8 @@ pub fn new(
|
|||||||
fn handle_message(state: State, control: Control) -> actor.Next(State, Control) {
|
fn handle_message(state: State, control: Control) -> actor.Next(State, Control) {
|
||||||
case control {
|
case control {
|
||||||
control.Search -> {
|
control.Search -> {
|
||||||
|
logging.log("musicplayer - initiating search")
|
||||||
|
|
||||||
update_search(state.ui, "searching: ")
|
update_search(state.ui, "searching: ")
|
||||||
|
|
||||||
actor.continue(
|
actor.continue(
|
||||||
@@ -72,6 +76,8 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
|
|||||||
}
|
}
|
||||||
|
|
||||||
control.Raw(content) -> {
|
control.Raw(content) -> {
|
||||||
|
logging.log("musicplayer - recieved raw input: " <> content)
|
||||||
|
|
||||||
let content = case state.mode {
|
let content = case state.mode {
|
||||||
Idle -> state.input.content
|
Idle -> state.input.content
|
||||||
Searching -> {
|
Searching -> {
|
||||||
@@ -84,6 +90,8 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
|
|||||||
actor.continue(State(..state, input: Input(..state.input, content:)))
|
actor.continue(State(..state, input: Input(..state.input, content:)))
|
||||||
}
|
}
|
||||||
control.Backspace -> {
|
control.Backspace -> {
|
||||||
|
logging.log("musicplayer - recieved backspace")
|
||||||
|
|
||||||
let content = case state.mode {
|
let content = case state.mode {
|
||||||
Idle -> state.input.content
|
Idle -> state.input.content
|
||||||
Searching -> string.drop_end(state.input.content, 1)
|
Searching -> string.drop_end(state.input.content, 1)
|
||||||
@@ -91,6 +99,13 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
|
|||||||
actor.continue(State(..state, input: Input(..state.input, content:)))
|
actor.continue(State(..state, input: Input(..state.input, content:)))
|
||||||
}
|
}
|
||||||
control.Return -> {
|
control.Return -> {
|
||||||
|
logging.log(
|
||||||
|
"musicplayer - recieved return. `input.capture`: "
|
||||||
|
<> "'"
|
||||||
|
<> state.input.content
|
||||||
|
<> "'",
|
||||||
|
)
|
||||||
|
|
||||||
// Note: state.input.content is now the final input, use it
|
// Note: state.input.content is now the final input, use it
|
||||||
// before it is reset
|
// before it is reset
|
||||||
case state.mode {
|
case state.mode {
|
||||||
@@ -104,17 +119,22 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
|
|||||||
}
|
}
|
||||||
|
|
||||||
control.TogglePlayPause -> {
|
control.TogglePlayPause -> {
|
||||||
|
logging.log("musicplayer - toggling play/pause")
|
||||||
|
|
||||||
process.send(state.mpv, mpv_control.TogglePlayPause)
|
process.send(state.mpv, mpv_control.TogglePlayPause)
|
||||||
update_playback_time(state.mpv, state.ui)
|
update_playback_time(state.mpv, state.ui)
|
||||||
actor.continue(state)
|
actor.continue(state)
|
||||||
}
|
}
|
||||||
control.Exit -> {
|
control.Exit -> {
|
||||||
|
logging.log("musicplayer - initiating musicplayer shutdown")
|
||||||
// Close `mpv` socket
|
// Close `mpv` socket
|
||||||
process.call(state.mpv, 1000, fn(reply_to) { mpv_control.Exit(reply_to) })
|
process.call(state.mpv, 1000, fn(reply_to) { mpv_control.Exit(reply_to) })
|
||||||
|
|
||||||
// Reset terminal state (show cursor etc.)
|
// Reset terminal state (show cursor etc.)
|
||||||
process.call(state.ui, 1000, fn(reply_to) { ui_control.Exit(reply_to) })
|
process.call(state.ui, 1000, fn(reply_to) { ui_control.Exit(reply_to) })
|
||||||
|
|
||||||
|
logging.log("musicplayer - stopped")
|
||||||
|
|
||||||
actor.stop()
|
actor.stop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -126,6 +146,7 @@ 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)
|
||||||
|
|||||||
42
src/musicplayer/ui/ansi.gleam
Normal file
42
src/musicplayer/ui/ansi.gleam
Normal 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
|
||||||
|
}
|
||||||
@@ -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))
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
import gleam/int
|
import gleam/int
|
||||||
import gleam/io
|
import gleam/io
|
||||||
|
|
||||||
pub fn print(content: String) -> Nil {
|
|
||||||
io.print(content <> "\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://en.wikipedia.org/wiki/ANSI_escape_code#Control_Sequence_Introducer_commands
|
// https://en.wikipedia.org/wiki/ANSI_escape_code#Control_Sequence_Introducer_commands
|
||||||
|
|
||||||
pub fn clear_screen() -> Nil {
|
pub fn clear_screen() -> Nil {
|
||||||
io.print("\u{001B}[2J\u{001B}[H")
|
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"
|
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 {
|
pub fn hide_cursor() -> Nil {
|
||||||
@@ -23,3 +23,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,18 +1,42 @@
|
|||||||
import gleam/dict
|
import gleam/dict
|
||||||
|
import gleam/float
|
||||||
|
import gleam/int
|
||||||
|
import gleam/list
|
||||||
|
import gleam/string
|
||||||
|
import gleam/string_tree
|
||||||
|
|
||||||
pub type Layout {
|
import musicplayer/logging/logging
|
||||||
Layout(nodes: dict.Dict(Section, Node))
|
import musicplayer/ui/ansi
|
||||||
}
|
import musicplayer/ui/internal
|
||||||
|
|
||||||
pub type Section {
|
pub type Section {
|
||||||
|
Section(String)
|
||||||
|
|
||||||
Root
|
Root
|
||||||
Header
|
Header
|
||||||
Search
|
Search
|
||||||
PlaybackTime
|
PlaybackTime
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type NodeType {
|
||||||
|
Container
|
||||||
|
Row
|
||||||
|
Cell
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A Nodes width and height is in percentage (of the available width/height of its parent Node)
|
||||||
pub type Node {
|
pub type Node {
|
||||||
Node(content: String, x: Int, y: Int, children: List(Section))
|
Node(
|
||||||
|
t: NodeType,
|
||||||
|
content: String,
|
||||||
|
width_percent: Int,
|
||||||
|
height_percent: Int,
|
||||||
|
children: List(Section),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Layout {
|
||||||
|
Layout(width: Int, height: Int, nodes: dict.Dict(Section, Node))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new() -> Layout {
|
pub fn new() -> Layout {
|
||||||
@@ -20,18 +44,46 @@ pub fn new() -> Layout {
|
|||||||
dict.from_list([
|
dict.from_list([
|
||||||
#(
|
#(
|
||||||
Root,
|
Root,
|
||||||
Node(content: "", x: 0, y: 0, children: [
|
Node(
|
||||||
Header,
|
t: Container,
|
||||||
Search,
|
content: "Music Player",
|
||||||
PlaybackTime,
|
width_percent: 100,
|
||||||
]),
|
height_percent: 100,
|
||||||
|
children: [Header, Search, PlaybackTime],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
#(
|
||||||
|
Header,
|
||||||
|
Node(
|
||||||
|
t: Row,
|
||||||
|
content: "Foo (1) | Bar (2) | Baz (3)",
|
||||||
|
width_percent: 100,
|
||||||
|
height_percent: 33,
|
||||||
|
children: [],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
#(
|
||||||
|
Search,
|
||||||
|
Node(
|
||||||
|
t: Row,
|
||||||
|
content: "",
|
||||||
|
width_percent: 100,
|
||||||
|
height_percent: 33,
|
||||||
|
children: [],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
#(
|
||||||
|
PlaybackTime,
|
||||||
|
Node(
|
||||||
|
t: Row,
|
||||||
|
content: "00:00",
|
||||||
|
width_percent: 100,
|
||||||
|
height_percent: 33,
|
||||||
|
children: [],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
#(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(width: 0, height: 0, nodes: nodes)
|
||||||
Layout(nodes: nodes)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_section(
|
pub fn update_section(
|
||||||
@@ -42,10 +94,138 @@ 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:)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render(layout: Layout) -> Nil {
|
||||||
|
internal.clear_screen()
|
||||||
|
[layout.width, layout.height]
|
||||||
|
|> list.map(int.to_string)
|
||||||
|
|> string.join(" ")
|
||||||
|
|> string.append("layout - render: ", _)
|
||||||
|
|> logging.log
|
||||||
|
|
||||||
|
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 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_generic(
|
||||||
|
layout,
|
||||||
|
container_width,
|
||||||
|
container_height,
|
||||||
|
container_top_left_x,
|
||||||
|
container_top_left_y,
|
||||||
|
0,
|
||||||
|
Root,
|
||||||
|
_,
|
||||||
|
ansi_renders,
|
||||||
|
)
|
||||||
|
|> string_tree.to_string
|
||||||
|
|> internal.print
|
||||||
|
}
|
||||||
|
|
||||||
|
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_tl_x: Int,
|
||||||
|
container_tl_y: Int,
|
||||||
|
// State
|
||||||
|
index: Int,
|
||||||
|
from: Section,
|
||||||
|
render_into: into,
|
||||||
|
renders: Renders(into),
|
||||||
|
) -> into {
|
||||||
|
case dict.get(layout.nodes, from) {
|
||||||
|
Error(_) -> render_into
|
||||||
|
Ok(node) -> {
|
||||||
|
let margin = 2.0
|
||||||
|
|
||||||
|
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(parent, fn(acc_into, ic) {
|
||||||
|
let #(i, child) = ic
|
||||||
|
|
||||||
|
let cw =
|
||||||
|
container_width
|
||||||
|
*. { int.to_float(node.width_percent) /. 100.0 }
|
||||||
|
-. margin
|
||||||
|
|> float.floor
|
||||||
|
|
||||||
|
let ch =
|
||||||
|
container_height
|
||||||
|
*. { int.to_float(node.height_percent) /. 100.0 }
|
||||||
|
-. margin
|
||||||
|
|> float.floor
|
||||||
|
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
82
src/musicplayer/ui/layout_examples/layout_examples.gleam
Normal file
82
src/musicplayer/ui/layout_examples/layout_examples.gleam
Normal 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)
|
||||||
|
}
|
||||||
15
src/musicplayer/ui/layout_examples/wait_for_input.gleam
Normal file
15
src/musicplayer/ui/layout_examples/wait_for_input.gleam
Normal 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
|
||||||
|
}
|
||||||
@@ -1,12 +1,13 @@
|
|||||||
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/logging
|
||||||
import musicplayer/ui/control.{type Control}
|
import musicplayer/ui/control.{type Control}
|
||||||
import musicplayer/ui/internal as ui_internal
|
import musicplayer/ui/internal
|
||||||
import musicplayer/ui/layout.{type Layout, type Section}
|
import musicplayer/ui/layout.{type Layout}
|
||||||
|
|
||||||
pub type State(redraw, content) {
|
pub type State(redraw, content) {
|
||||||
State(redraw: Subject(Layout), layout: Layout)
|
State(redraw: Subject(Layout), layout: Layout)
|
||||||
@@ -26,13 +27,18 @@ pub fn new() -> Result(Subject(Control), String) {
|
|||||||
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(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)
|
||||||
|
|
||||||
ui_internal.clear_screen()
|
internal.clear_screen()
|
||||||
ui_internal.hide_cursor()
|
internal.hide_cursor()
|
||||||
|
|
||||||
redraw_on_update_loop(redraw)
|
redraw_loop(redraw)
|
||||||
})
|
})
|
||||||
|
|
||||||
Ok(ui)
|
Ok(ui)
|
||||||
@@ -45,37 +51,55 @@ 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
|
||||||
|
|
||||||
|
let layout = layout.update_dimensions(state.layout, width, height)
|
||||||
|
|
||||||
|
process.send(state.redraw, layout)
|
||||||
|
actor.continue(State(..state, layout:))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
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)
|
actor.continue(State(..state, layout:))
|
||||||
}
|
}
|
||||||
|
|
||||||
control.Exit(reply_to) -> {
|
control.Exit(reply_to) -> {
|
||||||
ui_internal.show_cursor()
|
internal.show_cursor()
|
||||||
process.send(reply_to, Nil)
|
process.send(reply_to, Nil)
|
||||||
actor.stop()
|
actor.stop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn redraw_on_update_loop(redraw: Subject(Layout)) -> Nil {
|
fn redraw_loop(redraw: Subject(Layout)) -> Nil {
|
||||||
let layout = process.receive_forever(redraw)
|
process.receive_forever(redraw)
|
||||||
|
|> layout.render
|
||||||
|
|
||||||
ui_internal.clear_screen()
|
redraw_loop(redraw)
|
||||||
render_layout(layout, layout.Root)
|
|
||||||
|
|
||||||
redraw_on_update_loop(redraw)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_layout(layout: Layout, from: Section) -> Nil {
|
fn update_dimensions_on_interval(ui: Subject(Control), interval_ms: Int) {
|
||||||
case dict.get(layout.nodes, from) {
|
case internal.io_get_columns(), internal.io_get_rows() {
|
||||||
Error(_) -> Nil
|
Ok(width), Ok(height) -> {
|
||||||
Ok(node) -> {
|
process.send(ui, control.UpdateDimensions(width, height))
|
||||||
list.each(node.children, fn(child) { render_layout(layout, child) })
|
|
||||||
ui_internal.print_at(node.content, node.x, node.y)
|
|
||||||
}
|
}
|
||||||
|
_, _ -> logging.log("ui - failed to update dimensions")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
process.sleep(interval_ms)
|
||||||
|
update_dimensions_on_interval(ui, interval_ms)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import gleeunit
|
import gleeunit
|
||||||
|
|
||||||
import musicplayer/mpv/internal as control_internal
|
import musicplayer/mpv/internal
|
||||||
|
|
||||||
pub fn main() -> Nil {
|
pub fn main() -> Nil {
|
||||||
gleeunit.main()
|
gleeunit.main()
|
||||||
@@ -10,6 +10,6 @@ pub fn parse_playback_time_test() {
|
|||||||
let json_string =
|
let json_string =
|
||||||
"{\"data\":\"123.456789\",\"request_id\":0,\"error\":\"success\"}\n"
|
"{\"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
|
assert data == 123.456789
|
||||||
}
|
}
|
||||||
|
|||||||
118
test/musicplayer/ui/layout_test.gleam
Normal file
118
test/musicplayer/ui/layout_test.gleam
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
import gleam/dict
|
||||||
|
import gleam/io
|
||||||
|
import gleam/string
|
||||||
|
import gleeunit
|
||||||
|
import gleeunit/should
|
||||||
|
import musicplayer/ui/virtual_ansi
|
||||||
|
|
||||||
|
import musicplayer/ui/layout.{Layout, Node, Section}
|
||||||
|
|
||||||
|
pub fn main() -> Nil {
|
||||||
|
gleeunit.main()
|
||||||
|
}
|
||||||
|
|
||||||
|
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 expected =
|
||||||
|
"
|
||||||
|
container──────────────────────────────────────────────────────────────────────┐
|
||||||
|
│row 1────────────────────────────────────────────────────────────────────────┐│
|
||||||
|
││cell 1───────────────────────────────┐cell 2───────────────────────────────┐││
|
||||||
|
│││ ││ │││
|
||||||
|
│││ ││ │││
|
||||||
|
│││ ││ │││
|
||||||
|
│││ ││ │││
|
||||||
|
│││ ││ │││
|
||||||
|
││└────────────────────────────────────┘└────────────────────────────────────┘││
|
||||||
|
│└────────────────────────────────────────────────────────────────────────────┘│
|
||||||
|
│row 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))
|
||||||
|
|
||||||
|
io.println("Got:")
|
||||||
|
io.println(visual)
|
||||||
|
|
||||||
|
should.equal(visual, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
113
test/musicplayer/ui/virtual_ansi.gleam
Normal file
113
test/musicplayer/ui/virtual_ansi.gleam
Normal 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) })
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user