Compare commits
4 Commits
693eaf44bd
...
layout
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6aa9cc28de | ||
|
|
3d7287902f | ||
|
|
deaa0fcf89 | ||
|
|
f9d7b573ac |
@@ -8,15 +8,14 @@ import musicplayer/musicplayer
|
|||||||
import musicplayer/ui/ui
|
import musicplayer/ui/ui
|
||||||
|
|
||||||
pub fn main() -> Nil {
|
pub fn main() -> Nil {
|
||||||
let assert Ok(logger) = logging.new("/tmp/musicplayer.log")
|
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)
|
||||||
|
|
||||||
let assert Ok(ui) = ui.new(logger)
|
let assert Ok(ui) = ui.new()
|
||||||
let assert Ok(mpv) = mpv.new()
|
let assert Ok(mpv) = mpv.new()
|
||||||
let assert Ok(musicplayer_pid) =
|
let assert Ok(musicplayer_pid) = musicplayer.new(ui, mpv, input_keys_name)
|
||||||
musicplayer.new(logger, ui, mpv, input_keys_name)
|
|
||||||
|
|
||||||
let monitor = process.monitor(musicplayer_pid)
|
let monitor = process.monitor(musicplayer_pid)
|
||||||
process.new_selector()
|
process.new_selector()
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import gleam/erlang/process.{type Subject}
|
|
||||||
|
|
||||||
pub type Control {
|
pub type Control {
|
||||||
Write(String)
|
Write(String)
|
||||||
|
|
||||||
Exit(reply_to: Subject(Nil))
|
Exit
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,40 +1,22 @@
|
|||||||
import gleam/erlang/process.{type Subject}
|
|
||||||
import gleam/otp/actor
|
|
||||||
import gleam/result
|
import gleam/result
|
||||||
import gleam/string
|
import gleam/string
|
||||||
import gleam/time/calendar
|
import gleam/time/calendar
|
||||||
import gleam/time/timestamp
|
import gleam/time/timestamp
|
||||||
import simplifile
|
import simplifile
|
||||||
|
|
||||||
import musicplayer/logging/control.{type Control}
|
const filepath = "/tmp/musicplayer.log"
|
||||||
|
|
||||||
type State {
|
pub fn initialize() -> Result(Nil, String) {
|
||||||
State(filepath: String)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new(filepath: String) -> Result(Subject(Control), String) {
|
|
||||||
use _ <- result.try(
|
|
||||||
case simplifile.is_file(filepath) {
|
case simplifile.is_file(filepath) {
|
||||||
Ok(True) -> Ok(Nil)
|
Ok(True) -> Ok(Nil)
|
||||||
_ -> simplifile.create_file(filepath)
|
_ -> simplifile.create_file(filepath)
|
||||||
}
|
}
|
||||||
|> result.map_error(fn(e) {
|
|> result.map_error(fn(e) {
|
||||||
"Could not access or create log file: " <> string.inspect(e)
|
"Could not access or create log file: " <> string.inspect(e)
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
||||||
actor.new(State(filepath:))
|
|
||||||
|> actor.on_message(handle_message)
|
|
||||||
|> actor.start
|
|
||||||
|> result.map_error(fn(start_error) {
|
|
||||||
"Could not start logger: " <> string.inspect(start_error)
|
|
||||||
})
|
})
|
||||||
|> result.map(fn(started) { started.data })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_message(state: State, control: Control) -> actor.Next(State, Control) {
|
pub fn log(content: String) -> Nil {
|
||||||
case control {
|
|
||||||
control.Write(content) -> {
|
|
||||||
let log_line =
|
let log_line =
|
||||||
timestamp.system_time()
|
timestamp.system_time()
|
||||||
|> timestamp.to_rfc3339(calendar.utc_offset)
|
|> timestamp.to_rfc3339(calendar.utc_offset)
|
||||||
@@ -43,16 +25,6 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
|
|||||||
<> "\n"
|
<> "\n"
|
||||||
|
|
||||||
// Ignore any logging errors
|
// Ignore any logging errors
|
||||||
let _ = simplifile.append(state.filepath, log_line)
|
let _ = simplifile.append(filepath, log_line)
|
||||||
actor.continue(state)
|
Nil
|
||||||
}
|
|
||||||
control.Exit(reply_to) -> {
|
|
||||||
process.send(reply_to, Nil)
|
|
||||||
actor.stop()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn log(logger: Subject(Control), content: String) -> Nil {
|
|
||||||
process.send(logger, control.Write(content))
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import gleam/string
|
|||||||
|
|
||||||
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/control as logging_control
|
|
||||||
import musicplayer/logging/logging
|
import musicplayer/logging/logging
|
||||||
import musicplayer/mpv/control as mpv_control
|
import musicplayer/mpv/control as mpv_control
|
||||||
import musicplayer/time/time
|
import musicplayer/time/time
|
||||||
@@ -25,14 +24,12 @@ type State {
|
|||||||
State(
|
State(
|
||||||
mode: Mode,
|
mode: Mode,
|
||||||
input: Input,
|
input: Input,
|
||||||
logger: Subject(logging_control.Control),
|
|
||||||
ui: Subject(ui_control.Control),
|
ui: Subject(ui_control.Control),
|
||||||
mpv: Subject(mpv_control.Control),
|
mpv: Subject(mpv_control.Control),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(
|
pub fn new(
|
||||||
logger: Subject(logging_control.Control),
|
|
||||||
ui: Subject(ui_control.Control),
|
ui: Subject(ui_control.Control),
|
||||||
mpv: Subject(mpv_control.Control),
|
mpv: Subject(mpv_control.Control),
|
||||||
input_keys_name: Name(Key),
|
input_keys_name: Name(Key),
|
||||||
@@ -42,14 +39,14 @@ pub fn new(
|
|||||||
let input = Input(False, "")
|
let input = Input(False, "")
|
||||||
|
|
||||||
case
|
case
|
||||||
actor.new(State(Idle, input, logger, ui, mpv))
|
actor.new(State(Idle, input, ui, mpv))
|
||||||
|> 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(pid:, data: musicplayer)) -> {
|
Ok(actor.Started(pid:, data: musicplayer)) -> {
|
||||||
logging.log(logger, "musicplayer - started")
|
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)
|
||||||
@@ -65,7 +62,7 @@ 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(state.logger, "musicplayer - initiating search")
|
logging.log("musicplayer - initiating search")
|
||||||
|
|
||||||
update_search(state.ui, "searching: ")
|
update_search(state.ui, "searching: ")
|
||||||
|
|
||||||
@@ -79,7 +76,7 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
|
|||||||
}
|
}
|
||||||
|
|
||||||
control.Raw(content) -> {
|
control.Raw(content) -> {
|
||||||
logging.log(state.logger, "musicplayer - recieved raw input: " <> 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
|
||||||
@@ -93,7 +90,7 @@ 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(state.logger, "musicplayer - recieved backspace")
|
logging.log("musicplayer - recieved backspace")
|
||||||
|
|
||||||
let content = case state.mode {
|
let content = case state.mode {
|
||||||
Idle -> state.input.content
|
Idle -> state.input.content
|
||||||
@@ -103,7 +100,6 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
|
|||||||
}
|
}
|
||||||
control.Return -> {
|
control.Return -> {
|
||||||
logging.log(
|
logging.log(
|
||||||
state.logger,
|
|
||||||
"musicplayer - recieved return. `input.capture`: "
|
"musicplayer - recieved return. `input.capture`: "
|
||||||
<> "'"
|
<> "'"
|
||||||
<> state.input.content
|
<> state.input.content
|
||||||
@@ -123,26 +119,21 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
|
|||||||
}
|
}
|
||||||
|
|
||||||
control.TogglePlayPause -> {
|
control.TogglePlayPause -> {
|
||||||
logging.log(state.logger, "musicplayer - toggling play/pause")
|
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(state.logger, "musicplayer - initiating musicplayer shutdown")
|
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(state.logger, "musicplayer - stopped")
|
logging.log("musicplayer - stopped")
|
||||||
|
|
||||||
// Close logger (NOOP)
|
|
||||||
process.call(state.logger, 1000, fn(reply_to) {
|
|
||||||
logging_control.Exit(reply_to)
|
|
||||||
})
|
|
||||||
|
|
||||||
actor.stop()
|
actor.stop()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,9 +7,13 @@ 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 {
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
import gleam/dict
|
import gleam/dict
|
||||||
|
import gleam/list
|
||||||
|
import gleam/string_tree.{type StringTree}
|
||||||
|
|
||||||
|
import musicplayer/ui/internal
|
||||||
|
|
||||||
pub type Layout {
|
pub type Layout {
|
||||||
Layout(width: Int, height: Int, nodes: dict.Dict(Section, Node))
|
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 {
|
pub fn update_dimensions(layout: Layout, width: Int, height: Int) -> Layout {
|
||||||
Layout(..layout, width:, height:)
|
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,34 +1,26 @@
|
|||||||
import gleam/dict
|
|
||||||
import gleam/erlang/process.{type Subject}
|
import gleam/erlang/process.{type Subject}
|
||||||
import gleam/int
|
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/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(
|
State(redraw: Subject(Layout), layout: Layout)
|
||||||
logger: Subject(logging_control.Control),
|
|
||||||
redraw: Subject(Layout),
|
|
||||||
layout: Layout,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(
|
pub fn new() -> Result(Subject(Control), String) {
|
||||||
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(logger, redraw, layout))
|
actor.new(State(redraw, layout))
|
||||||
|> actor.on_message(handle_message)
|
|> actor.on_message(handle_message)
|
||||||
|> actor.start
|
|> actor.start
|
||||||
{
|
{
|
||||||
@@ -37,14 +29,14 @@ pub fn new(
|
|||||||
Ok(actor.Started(data: ui, ..)) -> {
|
Ok(actor.Started(data: ui, ..)) -> {
|
||||||
process.spawn(fn() {
|
process.spawn(fn() {
|
||||||
let update_dimensions_interval_ms = 300
|
let update_dimensions_interval_ms = 300
|
||||||
update_dimensions_on_interval(logger, ui, update_dimensions_interval_ms)
|
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_on_update_loop(redraw)
|
||||||
})
|
})
|
||||||
@@ -69,7 +61,7 @@ fn handle_message(
|
|||||||
|> 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(state.logger, _)
|
|> logging.log
|
||||||
|
|
||||||
actor.continue(
|
actor.continue(
|
||||||
State(
|
State(
|
||||||
@@ -89,7 +81,7 @@ fn handle_message(
|
|||||||
}
|
}
|
||||||
|
|
||||||
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()
|
||||||
}
|
}
|
||||||
@@ -99,34 +91,20 @@ fn handle_message(
|
|||||||
fn redraw_on_update_loop(redraw: Subject(Layout)) -> Nil {
|
fn redraw_on_update_loop(redraw: Subject(Layout)) -> Nil {
|
||||||
let layout = process.receive_forever(redraw)
|
let layout = process.receive_forever(redraw)
|
||||||
|
|
||||||
ui_internal.clear_screen()
|
internal.clear_screen()
|
||||||
render_layout(layout, layout.Root)
|
layout.render(layout, layout.Root)
|
||||||
|
|
||||||
redraw_on_update_loop(redraw)
|
redraw_on_update_loop(redraw)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_dimensions_on_interval(
|
fn update_dimensions_on_interval(ui: Subject(Control), interval_ms: Int) {
|
||||||
logger: Subject(logging_control.Control),
|
case internal.io_get_columns(), internal.io_get_rows() {
|
||||||
ui: Subject(Control),
|
|
||||||
interval_ms: Int,
|
|
||||||
) {
|
|
||||||
case ui_internal.io_get_columns(), ui_internal.io_get_rows() {
|
|
||||||
Ok(width), Ok(height) -> {
|
Ok(width), Ok(height) -> {
|
||||||
process.send(ui, control.UpdateDimensions(width, height))
|
process.send(ui, control.UpdateDimensions(width, height))
|
||||||
}
|
}
|
||||||
_, _ -> logging.log(logger, "ui - failed to update dimensions")
|
_, _ -> logging.log("ui - failed to update dimensions")
|
||||||
}
|
}
|
||||||
|
|
||||||
process.sleep(interval_ms)
|
process.sleep(interval_ms)
|
||||||
update_dimensions_on_interval(logger, ui, 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 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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user