Compare commits
1 Commits
26d9985a38
...
d311eedadb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d311eedadb |
@@ -4,7 +4,6 @@ import musicplayer/input/input
|
|||||||
import musicplayer/input/key.{type Key}
|
import musicplayer/input/key.{type Key}
|
||||||
import musicplayer/mpv/mpv
|
import musicplayer/mpv/mpv
|
||||||
import musicplayer/musicplayer
|
import musicplayer/musicplayer
|
||||||
import musicplayer/ui/ui
|
|
||||||
|
|
||||||
pub fn main() -> Nil {
|
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")
|
||||||
@@ -16,16 +15,13 @@ pub fn main() -> Nil {
|
|||||||
// inject input
|
// inject input
|
||||||
let input_inject_name: Name(Key) = process.new_name("input_inject_keys")
|
let input_inject_name: Name(Key) = process.new_name("input_inject_keys")
|
||||||
|
|
||||||
// TODO should input.new just return the inject_subject?
|
|
||||||
input.new(input_keys_name, input_stream_name, input_inject_name)
|
input.new(input_keys_name, input_stream_name, input_inject_name)
|
||||||
|
|
||||||
let assert Ok(ui) = ui.new()
|
|
||||||
let assert Ok(mpv) = mpv.new()
|
let assert Ok(mpv) = mpv.new()
|
||||||
|
|
||||||
let exit = process.new_subject()
|
let exit = process.new_subject()
|
||||||
let assert Ok(_) =
|
let assert Ok(_) =
|
||||||
musicplayer.new(
|
musicplayer.new(
|
||||||
ui,
|
|
||||||
mpv,
|
mpv,
|
||||||
input_keys_name,
|
input_keys_name,
|
||||||
input_stream_name,
|
input_stream_name,
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import musicplayer/input/key.{type Key}
|
|||||||
|
|
||||||
pub type Control {
|
pub type Control {
|
||||||
TogglePlayPause
|
TogglePlayPause
|
||||||
|
|
||||||
Search
|
Search
|
||||||
|
|
||||||
Exit
|
Exit
|
||||||
@@ -17,8 +18,8 @@ pub fn from_key(key: Key) -> Result(Control, Nil) {
|
|||||||
fn char_control(char: String) -> Result(Control, Nil) {
|
fn char_control(char: String) -> Result(Control, Nil) {
|
||||||
case char {
|
case char {
|
||||||
" " -> Ok(TogglePlayPause)
|
" " -> Ok(TogglePlayPause)
|
||||||
"/" -> Ok(Search)
|
|
||||||
"q" -> Ok(Exit)
|
"q" -> Ok(Exit)
|
||||||
|
"/" -> Ok(Search)
|
||||||
_ -> Error(Nil)
|
_ -> Error(Nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ pub fn new(
|
|||||||
read_input(input_keys, input_stream, input_inject)
|
read_input(input_keys, input_stream, input_inject)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
echo "waiting for input"
|
||||||
key.start_raw_shell()
|
key.start_raw_shell()
|
||||||
Nil
|
Nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import gleam/otp/actor
|
|||||||
import gleam/result
|
import gleam/result
|
||||||
import gleam/string
|
import gleam/string
|
||||||
|
|
||||||
import musicplayer/mpv/control.{type Control}
|
import musicplayer/mpv/control
|
||||||
import musicplayer/tcp/reason
|
import musicplayer/tcp/reason
|
||||||
import musicplayer/tcp/tcp.{type Socket}
|
import musicplayer/tcp/tcp.{type Socket}
|
||||||
|
|
||||||
@@ -11,7 +11,7 @@ type State(socket) {
|
|||||||
State(socket: Socket)
|
State(socket: Socket)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new() -> Result(Subject(Control), String) {
|
pub fn new() -> Result(Subject(control.Control), String) {
|
||||||
// TODO start up mvp here, currently hi-jacking `naviterm`s socket
|
// TODO start up mvp here, currently hi-jacking `naviterm`s socket
|
||||||
let socket_path = "/tmp/naviterm_mpv"
|
let socket_path = "/tmp/naviterm_mpv"
|
||||||
|
|
||||||
@@ -33,8 +33,8 @@ pub fn new() -> Result(Subject(Control), String) {
|
|||||||
|
|
||||||
fn handle_message(
|
fn handle_message(
|
||||||
state: State(socket),
|
state: State(socket),
|
||||||
control: Control,
|
control: control.Control,
|
||||||
) -> actor.Next(State(socket), Control) {
|
) -> actor.Next(State(socket), control.Control) {
|
||||||
case control {
|
case control {
|
||||||
control.TogglePlayPause -> {
|
control.TogglePlayPause -> {
|
||||||
echo "mpv: toggling play/pause"
|
echo "mpv: toggling play/pause"
|
||||||
|
|||||||
@@ -7,11 +7,9 @@ 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/mpv/control as mpv_control
|
import musicplayer/mpv/control as mpv_control
|
||||||
import musicplayer/ui/control as ui_control
|
|
||||||
|
|
||||||
type State(ui, mpv, input_inject, exit) {
|
type State(mpv, input_inject, exit) {
|
||||||
State(
|
State(
|
||||||
ui: Subject(ui_control.Control),
|
|
||||||
mpv: Subject(mpv_control.Control),
|
mpv: Subject(mpv_control.Control),
|
||||||
input_inject: Subject(Key),
|
input_inject: Subject(Key),
|
||||||
exit: Subject(Nil),
|
exit: Subject(Nil),
|
||||||
@@ -19,7 +17,6 @@ type State(ui, mpv, input_inject, exit) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(
|
pub fn new(
|
||||||
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),
|
||||||
input_stream_name: Name(List(String)),
|
input_stream_name: Name(List(String)),
|
||||||
@@ -31,7 +28,7 @@ pub fn new(
|
|||||||
let input_inject = process.named_subject(input_inject_name)
|
let input_inject = process.named_subject(input_inject_name)
|
||||||
|
|
||||||
case
|
case
|
||||||
actor.new(State(ui, mpv, input_inject, exit))
|
actor.new(State(mpv, input_inject, exit))
|
||||||
|> actor.on_message(handle_message)
|
|> actor.on_message(handle_message)
|
||||||
|> actor.start
|
|> actor.start
|
||||||
{
|
{
|
||||||
@@ -54,9 +51,9 @@ pub fn new(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn handle_message(
|
fn handle_message(
|
||||||
state: State(ui, mpv, input_inject, exit),
|
state: State(mpv, input_inject, exit),
|
||||||
control: Control,
|
control: Control,
|
||||||
) -> actor.Next(State(ui, mpv, input_inject, exit), Control) {
|
) -> actor.Next(State(mpv, input_inject, exit), Control) {
|
||||||
case control {
|
case control {
|
||||||
control.Search -> {
|
control.Search -> {
|
||||||
process.send(state.input_inject, key.Continue([key.input_introducer]))
|
process.send(state.input_inject, key.Continue([key.input_introducer]))
|
||||||
@@ -73,32 +70,17 @@ fn handle_message(
|
|||||||
mpv_control.GetPlaybackTime(reply_to)
|
mpv_control.GetPlaybackTime(reply_to)
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
Error(err) ->
|
Error(err) -> echo "! could not get playbackTime: " <> err.details
|
||||||
process.send(
|
|
||||||
state.ui,
|
|
||||||
ui_control.UpdateState(
|
|
||||||
"playback time: N/A (err: " <> err.details <> ")",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
Ok(mpv_control.PlaybackTime(data: playback_time)) ->
|
Ok(mpv_control.PlaybackTime(data: playback_time)) ->
|
||||||
process.send(
|
echo "playbacktime from mpv: " <> float.to_string(playback_time)
|
||||||
state.ui,
|
|
||||||
ui_control.UpdateState(
|
|
||||||
"playback time: " <> float.to_string(playback_time),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
actor.continue(state)
|
actor.continue(state)
|
||||||
}
|
}
|
||||||
control.Exit -> {
|
control.Exit -> {
|
||||||
// Close `mpv` socket
|
// Close socket to `mpv`
|
||||||
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.)
|
|
||||||
process.call(state.ui, 1000, fn(reply_to) { ui_control.Exit(reply_to) })
|
|
||||||
|
|
||||||
// End main process
|
// End main process
|
||||||
process.send(state.exit, Nil)
|
process.send(state.exit, Nil)
|
||||||
actor.stop()
|
actor.stop()
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
import gleam/erlang/process.{type Subject}
|
|
||||||
|
|
||||||
pub type Control {
|
|
||||||
UpdateState(content: String)
|
|
||||||
|
|
||||||
Exit(reply_to: Subject(Nil))
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
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
|
|
||||||
|
|
||||||
pub fn clear_screen() -> Nil {
|
|
||||||
io.print("\u{001B}[2J\u{001B}[H")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn hide_cursor() -> Nil {
|
|
||||||
io.print("\u{001B}[?25l")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn show_cursor() -> Nil {
|
|
||||||
io.print("\u{001B}[?25h")
|
|
||||||
}
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
import gleam/erlang/process.{type Subject}
|
|
||||||
import gleam/otp/actor
|
|
||||||
import gleam/string
|
|
||||||
|
|
||||||
import musicplayer/ui/control.{type Control}
|
|
||||||
import musicplayer/ui/internal as ui_internal
|
|
||||||
|
|
||||||
pub type State(redraw, content) {
|
|
||||||
State(redraw: Subject(String), content: String)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new() -> Result(Subject(Control), String) {
|
|
||||||
let redraw_name = process.new_name("redraw")
|
|
||||||
let redraw: Subject(String) = process.named_subject(redraw_name)
|
|
||||||
case
|
|
||||||
actor.new(State(redraw, ""))
|
|
||||||
|> actor.on_message(handle_message)
|
|
||||||
|> actor.start
|
|
||||||
{
|
|
||||||
Error(start_error) ->
|
|
||||||
Error("Could not start actor: " <> string.inspect(start_error))
|
|
||||||
Ok(actor.Started(data: ui, ..)) -> {
|
|
||||||
process.spawn(fn() {
|
|
||||||
let assert Ok(_) = process.register(process.self(), redraw_name)
|
|
||||||
|
|
||||||
ui_internal.clear_screen()
|
|
||||||
ui_internal.hide_cursor()
|
|
||||||
|
|
||||||
redraw_loop(redraw)
|
|
||||||
})
|
|
||||||
|
|
||||||
Ok(ui)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_message(
|
|
||||||
state: State(redraw, content),
|
|
||||||
control: Control,
|
|
||||||
) -> actor.Next(State(redraw, content), Control) {
|
|
||||||
case control {
|
|
||||||
control.UpdateState(content) -> {
|
|
||||||
let state = State(..state, content:)
|
|
||||||
actor.send(state.redraw, content)
|
|
||||||
actor.continue(state)
|
|
||||||
}
|
|
||||||
control.Exit(reply_to) -> {
|
|
||||||
ui_internal.show_cursor()
|
|
||||||
process.send(reply_to, Nil)
|
|
||||||
actor.stop()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn redraw_loop(redraw: Subject(String)) -> Nil {
|
|
||||||
let content = process.receive_forever(redraw)
|
|
||||||
|
|
||||||
ui_internal.clear_screen()
|
|
||||||
ui_internal.print(content)
|
|
||||||
|
|
||||||
redraw_loop(redraw)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user