Compare commits
4 Commits
2332710235
...
693eaf44bd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
693eaf44bd | ||
|
|
6ef3e9734d | ||
|
|
e524a865fe | ||
|
|
5e8ba9a4b0 |
@@ -8,14 +8,15 @@ 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 assert Ok(logger) = logging.new("/tmp/musicplayer.log")
|
||||||
|
|
||||||
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) = musicplayer.new(ui, mpv, input_keys_name)
|
let assert Ok(musicplayer_pid) =
|
||||||
|
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,5 +1,7 @@
|
|||||||
|
import gleam/erlang/process.{type Subject}
|
||||||
|
|
||||||
pub type Control {
|
pub type Control {
|
||||||
Write(String)
|
Write(String)
|
||||||
|
|
||||||
Exit
|
Exit(reply_to: Subject(Nil))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +1,58 @@
|
|||||||
|
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
|
||||||
|
|
||||||
const filepath = "/tmp/musicplayer.log"
|
import musicplayer/logging/control.{type Control}
|
||||||
|
|
||||||
pub fn initialize() -> Result(Nil, String) {
|
type State {
|
||||||
case simplifile.is_file(filepath) {
|
State(filepath: String)
|
||||||
Ok(True) -> Ok(Nil)
|
}
|
||||||
_ -> simplifile.create_file(filepath)
|
|
||||||
}
|
pub fn new(filepath: String) -> Result(Subject(Control), String) {
|
||||||
|> result.map_error(fn(e) {
|
use _ <- result.try(
|
||||||
"Could not access or create log file: " <> string.inspect(e)
|
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)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
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 })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn log(content: String) -> Nil {
|
fn handle_message(state: State, control: Control) -> actor.Next(State, Control) {
|
||||||
let log_line =
|
case control {
|
||||||
timestamp.system_time()
|
control.Write(content) -> {
|
||||||
|> timestamp.to_rfc3339(calendar.utc_offset)
|
let log_line =
|
||||||
<> ": "
|
timestamp.system_time()
|
||||||
<> content
|
|> timestamp.to_rfc3339(calendar.utc_offset)
|
||||||
<> "\n"
|
<> ": "
|
||||||
|
<> content
|
||||||
|
<> "\n"
|
||||||
|
|
||||||
// Ignore any logging errors
|
// Ignore any logging errors
|
||||||
let _ = simplifile.append(filepath, log_line)
|
let _ = simplifile.append(state.filepath, log_line)
|
||||||
Nil
|
actor.continue(state)
|
||||||
|
}
|
||||||
|
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,6 +5,7 @@ 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
|
||||||
@@ -24,12 +25,14 @@ 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),
|
||||||
@@ -39,14 +42,14 @@ pub fn new(
|
|||||||
let input = Input(False, "")
|
let input = Input(False, "")
|
||||||
|
|
||||||
case
|
case
|
||||||
actor.new(State(Idle, input, ui, mpv))
|
actor.new(State(Idle, input, logger, 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("musicplayer - started")
|
logging.log(logger, "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)
|
||||||
@@ -62,7 +65,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("musicplayer - initiating search")
|
logging.log(state.logger, "musicplayer - initiating search")
|
||||||
|
|
||||||
update_search(state.ui, "searching: ")
|
update_search(state.ui, "searching: ")
|
||||||
|
|
||||||
@@ -76,7 +79,7 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
|
|||||||
}
|
}
|
||||||
|
|
||||||
control.Raw(content) -> {
|
control.Raw(content) -> {
|
||||||
logging.log("musicplayer - recieved raw input: " <> content)
|
logging.log(state.logger, "musicplayer - recieved raw input: " <> content)
|
||||||
|
|
||||||
let content = case state.mode {
|
let content = case state.mode {
|
||||||
Idle -> state.input.content
|
Idle -> state.input.content
|
||||||
@@ -90,7 +93,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("musicplayer - recieved backspace")
|
logging.log(state.logger, "musicplayer - recieved backspace")
|
||||||
|
|
||||||
let content = case state.mode {
|
let content = case state.mode {
|
||||||
Idle -> state.input.content
|
Idle -> state.input.content
|
||||||
@@ -100,10 +103,11 @@ 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
|
||||||
<> "'",
|
<> "'",
|
||||||
)
|
)
|
||||||
|
|
||||||
// Note: state.input.content is now the final input, use it
|
// Note: state.input.content is now the final input, use it
|
||||||
@@ -119,21 +123,26 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
|
|||||||
}
|
}
|
||||||
|
|
||||||
control.TogglePlayPause -> {
|
control.TogglePlayPause -> {
|
||||||
logging.log("musicplayer - toggling play/pause")
|
logging.log(state.logger, "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")
|
logging.log(state.logger, "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")
|
logging.log(state.logger, "musicplayer - stopped")
|
||||||
|
|
||||||
|
// Close logger (NOOP)
|
||||||
|
process.call(state.logger, 1000, fn(reply_to) {
|
||||||
|
logging_control.Exit(reply_to)
|
||||||
|
})
|
||||||
|
|
||||||
actor.stop()
|
actor.stop()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
import gleam/dict
|
import gleam/dict
|
||||||
|
import gleam/erlang/process.{type Subject}
|
||||||
import gleam/float
|
import gleam/float
|
||||||
import gleam/int
|
import gleam/int
|
||||||
import gleam/list
|
import gleam/list
|
||||||
import gleam/string
|
import gleam/string
|
||||||
|
|
||||||
|
import musicplayer/logging/control as logging_control
|
||||||
import musicplayer/logging/logging
|
import musicplayer/logging/logging
|
||||||
import musicplayer/ui/internal
|
import musicplayer/ui/internal
|
||||||
|
|
||||||
@@ -81,13 +83,13 @@ pub fn update_dimensions(layout: Layout, width: Int, height: Int) -> Layout {
|
|||||||
Layout(..layout, width:, height:)
|
Layout(..layout, width:, height:)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(layout: Layout) -> Nil {
|
pub fn render(logger: Subject(logging_control.Control), layout: Layout) -> Nil {
|
||||||
internal.clear_screen()
|
internal.clear_screen()
|
||||||
[layout.width, layout.height]
|
[layout.width, layout.height]
|
||||||
|> list.map(int.to_string)
|
|> list.map(int.to_string)
|
||||||
|> string.join(" ")
|
|> string.join(" ")
|
||||||
|> string.append("layout - render: ", _)
|
|> string.append("layout - render: ", _)
|
||||||
|> logging.log
|
|> logging.log(logger, _)
|
||||||
|
|
||||||
let container_width = int.to_float(layout.width)
|
let container_width = int.to_float(layout.width)
|
||||||
let container_height = int.to_float(layout.height)
|
let container_height = int.to_float(layout.height)
|
||||||
@@ -101,6 +103,7 @@ pub fn render(layout: Layout) -> Nil {
|
|||||||
container_top_left_x,
|
container_top_left_x,
|
||||||
container_top_left_y,
|
container_top_left_y,
|
||||||
Root,
|
Root,
|
||||||
|
logger,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,6 +114,7 @@ pub fn render_loop(
|
|||||||
container_top_left_x: Int,
|
container_top_left_x: Int,
|
||||||
container_top_left_y: Int,
|
container_top_left_y: Int,
|
||||||
from: Section,
|
from: Section,
|
||||||
|
logger: Subject(logging_control.Control),
|
||||||
) -> Nil {
|
) -> Nil {
|
||||||
let margin = 2.0
|
let margin = 2.0
|
||||||
|
|
||||||
@@ -133,30 +137,38 @@ pub fn render_loop(
|
|||||||
let cx = container_top_left_x + 1
|
let cx = container_top_left_x + 1
|
||||||
let cy = container_top_left_y + 1
|
let cy = container_top_left_y + 1
|
||||||
|
|
||||||
render_loop(layout, cw, ch, cx, cy, child)
|
render_loop(layout, cw, ch, cx, cy, child, logger)
|
||||||
})
|
})
|
||||||
|
|
||||||
logging.log("section: " <> string.inspect(from))
|
logging.log(logger, "section: " <> string.inspect(from))
|
||||||
logging.log("container width: " <> float.to_string(container_width))
|
logging.log(
|
||||||
logging.log("container height: " <> float.to_string(container_height))
|
logger,
|
||||||
|
"container width: " <> float.to_string(container_width),
|
||||||
|
)
|
||||||
|
logging.log(
|
||||||
|
logger,
|
||||||
|
"container height: " <> float.to_string(container_height),
|
||||||
|
)
|
||||||
|
|
||||||
let width =
|
let width =
|
||||||
container_width *. { int.to_float(node.width_percent) /. 100.0 }
|
container_width
|
||||||
|
*. { int.to_float(node.width_percent) /. int.to_float(100) }
|
||||||
|> float.floor
|
|> float.floor
|
||||||
|> float.truncate
|
|> float.truncate
|
||||||
|
|
||||||
let height =
|
let height =
|
||||||
container_height *. { int.to_float(node.height_percent) /. 100.0 }
|
container_height
|
||||||
|
*. { int.to_float(node.height_percent) /. int.to_float(100) }
|
||||||
|> float.floor
|
|> float.floor
|
||||||
|> float.truncate
|
|> float.truncate
|
||||||
|
|
||||||
logging.log("width: " <> int.to_string(width))
|
logging.log(logger, "width: " <> int.to_string(width))
|
||||||
logging.log("height: " <> int.to_string(height))
|
logging.log(logger, "height: " <> int.to_string(height))
|
||||||
|
|
||||||
let cx = container_top_left_x
|
let cx = container_top_left_x
|
||||||
let cy = container_top_left_y
|
let cy = container_top_left_y
|
||||||
logging.log("cx: " <> int.to_string(cx))
|
logging.log(logger, "cx: " <> int.to_string(cx))
|
||||||
logging.log("cy: " <> int.to_string(cy))
|
logging.log(logger, "cy: " <> int.to_string(cy))
|
||||||
|
|
||||||
draw_box(cx, cy, width, height)
|
draw_box(cx, cy, width, height)
|
||||||
// Box heading
|
// Box heading
|
||||||
|
|||||||
@@ -4,23 +4,30 @@ 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
|
import musicplayer/ui/internal
|
||||||
import musicplayer/ui/layout.{type Layout}
|
import musicplayer/ui/layout.{type Layout}
|
||||||
|
|
||||||
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
|
||||||
{
|
{
|
||||||
@@ -29,7 +36,7 @@ pub fn new() -> Result(Subject(Control), String) {
|
|||||||
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(ui, update_dimensions_interval_ms)
|
update_dimensions_on_interval(logger, ui, update_dimensions_interval_ms)
|
||||||
})
|
})
|
||||||
|
|
||||||
process.spawn(fn() {
|
process.spawn(fn() {
|
||||||
@@ -38,7 +45,7 @@ pub fn new() -> Result(Subject(Control), String) {
|
|||||||
internal.clear_screen()
|
internal.clear_screen()
|
||||||
internal.hide_cursor()
|
internal.hide_cursor()
|
||||||
|
|
||||||
redraw_loop(redraw)
|
redraw_loop(logger, redraw)
|
||||||
})
|
})
|
||||||
|
|
||||||
Ok(ui)
|
Ok(ui)
|
||||||
@@ -61,7 +68,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
|
|> logging.log(state.logger, _)
|
||||||
|
|
||||||
let layout = layout.update_dimensions(state.layout, width, height)
|
let layout = layout.update_dimensions(state.layout, width, height)
|
||||||
|
|
||||||
@@ -85,21 +92,28 @@ fn handle_message(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn redraw_loop(redraw: Subject(Layout)) -> Nil {
|
fn redraw_loop(
|
||||||
|
logger: Subject(logging_control.Control),
|
||||||
|
redraw: Subject(Layout),
|
||||||
|
) -> Nil {
|
||||||
process.receive_forever(redraw)
|
process.receive_forever(redraw)
|
||||||
|> layout.render
|
|> layout.render(logger, _)
|
||||||
|
|
||||||
redraw_loop(redraw)
|
redraw_loop(logger, redraw)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_dimensions_on_interval(ui: Subject(Control), interval_ms: Int) {
|
fn update_dimensions_on_interval(
|
||||||
|
logger: Subject(logging_control.Control),
|
||||||
|
ui: Subject(Control),
|
||||||
|
interval_ms: Int,
|
||||||
|
) {
|
||||||
case internal.io_get_columns(), internal.io_get_rows() {
|
case internal.io_get_columns(), 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("ui - failed to update dimensions")
|
_, _ -> logging.log(logger, "ui - failed to update dimensions")
|
||||||
}
|
}
|
||||||
|
|
||||||
process.sleep(interval_ms)
|
process.sleep(interval_ms)
|
||||||
update_dimensions_on_interval(ui, interval_ms)
|
update_dimensions_on_interval(logger, ui, interval_ms)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user