Compare commits
2 Commits
b69852f7ba
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d01b6d3f18 | ||
|
|
249b0671e4 |
@@ -1,110 +0,0 @@
|
|||||||
import gleam/erlang/process.{type Subject}
|
|
||||||
import gleam/float
|
|
||||||
import gleam/otp/actor
|
|
||||||
import gleam/result
|
|
||||||
import gleam/string
|
|
||||||
|
|
||||||
import input/key.{type Key}
|
|
||||||
import mpv/control.{type Control}
|
|
||||||
import tcp/reason
|
|
||||||
import tcp/tcp.{type Socket}
|
|
||||||
|
|
||||||
type State(socket, inject_input, tap_input, exit) {
|
|
||||||
State(
|
|
||||||
socket: Socket,
|
|
||||||
inject_input: Subject(Key),
|
|
||||||
tap_input: Subject(List(String)),
|
|
||||||
exit: Subject(Nil),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
|
||||||
// TODO start up mvp here, currently hi-jacking `naviterm`s socket
|
|
||||||
let socket_path = "/tmp/naviterm_mpv"
|
|
||||||
|
|
||||||
case tcp.connect(socket_path) {
|
|
||||||
Error(r) -> Error("Could not connect to mpv: " <> reason.to_string(r))
|
|
||||||
Ok(socket) -> {
|
|
||||||
// `inject_input` is created by name to allow the process that
|
|
||||||
// owns `read_input` to be able to register it, while the agent
|
|
||||||
// also have a reference to it to be able to inject input
|
|
||||||
let inject_input_name = process.new_name("inject_input")
|
|
||||||
let inject_input = process.named_subject(inject_input_name)
|
|
||||||
|
|
||||||
let tap_input_name = process.new_name("tap_input")
|
|
||||||
let tap_input = process.named_subject(tap_input_name)
|
|
||||||
|
|
||||||
case
|
|
||||||
actor.new(State(socket, inject_input, tap_input, exit))
|
|
||||||
|> actor.on_message(handle_message)
|
|
||||||
|> actor.start
|
|
||||||
{
|
|
||||||
Error(start_error) ->
|
|
||||||
Error("Could not start actor: " <> string.inspect(start_error))
|
|
||||||
Ok(actor.Started(data:, ..)) -> {
|
|
||||||
echo "waiting for input"
|
|
||||||
key.start_raw_shell()
|
|
||||||
|
|
||||||
process.spawn(fn() {
|
|
||||||
let assert Ok(_) =
|
|
||||||
process.register(process.self(), inject_input_name)
|
|
||||||
|
|
||||||
read_input(data, inject_input, tap_input)
|
|
||||||
})
|
|
||||||
|
|
||||||
Ok(Nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_message(
|
|
||||||
state: State(socket, inject, input_output, exit),
|
|
||||||
control: Control,
|
|
||||||
) -> actor.Next(State(socket, inject, input_output, exit), Control) {
|
|
||||||
case control {
|
|
||||||
control.TogglePlayPause -> {
|
|
||||||
echo "toggling play/pause"
|
|
||||||
|
|
||||||
let _ =
|
|
||||||
result.map_error(control.toggle_play_pause(state.socket), fn(err) {
|
|
||||||
echo "Could not toggle play/pause: " <> err.details
|
|
||||||
})
|
|
||||||
|
|
||||||
let _ =
|
|
||||||
result.map(control.get_playback_time(state.socket), fn(playback) {
|
|
||||||
echo "playback: " <> float.to_string(playback.data)
|
|
||||||
})
|
|
||||||
|
|
||||||
actor.continue(state)
|
|
||||||
}
|
|
||||||
control.Exit -> {
|
|
||||||
process.send(state.exit, Nil)
|
|
||||||
actor.stop()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `read_input` operates by reading from input until a `Key` can be created.
|
|
||||||
/// It is possible to create a `Key` without the users input by sending
|
|
||||||
/// messages to `inject_input` which will initialize the "input to key" sequence.
|
|
||||||
/// This is useful to ultimately create a `Control` without the user having to
|
|
||||||
/// input all of the character(s) needed.
|
|
||||||
fn read_input(
|
|
||||||
subject: Subject(Control),
|
|
||||||
inject_input: Subject(Key),
|
|
||||||
tap_input: Subject(List(String)),
|
|
||||||
) -> Nil {
|
|
||||||
let buffer = case process.receive(inject_input, 1) {
|
|
||||||
Ok(key.Continue(buffer)) -> buffer
|
|
||||||
Ok(_) | Error(_) -> []
|
|
||||||
}
|
|
||||||
|
|
||||||
let _ =
|
|
||||||
key.read_input_until_key(buffer, tap_input)
|
|
||||||
|> control.from_key
|
|
||||||
|> result.map(process.send(subject, _))
|
|
||||||
|
|
||||||
read_input(subject, inject_input, tap_input)
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,22 @@
|
|||||||
import gleam/erlang/process
|
import gleam/erlang/process.{type Name}
|
||||||
import mpv/mpv
|
|
||||||
|
import musicplayer/input/input.{type Listener}
|
||||||
|
import musicplayer/input/key.{type Key}
|
||||||
|
import musicplayer/mpv/mpv
|
||||||
|
|
||||||
pub fn main() -> Nil {
|
pub fn main() -> Nil {
|
||||||
let exit = process.new_subject()
|
let exit = process.new_subject()
|
||||||
let assert Ok(_) = mpv.new(exit)
|
|
||||||
|
// `inject_input` is created by name to allow the `input` process that
|
||||||
|
// owns `read_input` to be able to register and receive from it,
|
||||||
|
// while the any other processes can use the name reference to
|
||||||
|
// inject input
|
||||||
|
let inject_input_name: Name(Key) = process.new_name("inject_input")
|
||||||
|
|
||||||
|
let assert Ok(mpv_listener) = mpv.new(exit)
|
||||||
|
|
||||||
|
let listeners: List(Listener) = [mpv_listener]
|
||||||
|
|
||||||
|
input.new(listeners, inject_input_name)
|
||||||
process.receive_forever(exit)
|
process.receive_forever(exit)
|
||||||
}
|
}
|
||||||
|
|||||||
59
src/musicplayer/input/input.gleam
Normal file
59
src/musicplayer/input/input.gleam
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import gleam/erlang/process.{type Name, type Subject}
|
||||||
|
import gleam/list
|
||||||
|
import gleam/option.{type Option, None, Some}
|
||||||
|
|
||||||
|
import musicplayer/input/key.{type Key}
|
||||||
|
|
||||||
|
pub type Listener {
|
||||||
|
InputListener(final: Subject(Key), tap: Option(Subject(List(String))))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `new` accepts a list of listeners that are composed of two subjects;
|
||||||
|
/// - one to get the final `Key` and
|
||||||
|
/// - one to tap the input as it is read from i/o
|
||||||
|
/// and
|
||||||
|
/// - a subject name that is used to create a `Subject` that other processes
|
||||||
|
/// know they can inject a `Key` into the input with
|
||||||
|
pub fn new(listeners: List(Listener), inject_input_name: Name(Key)) -> Nil {
|
||||||
|
let _ =
|
||||||
|
process.spawn(fn() {
|
||||||
|
let inject_input: Subject(Key) = process.named_subject(inject_input_name)
|
||||||
|
let assert Ok(_) = process.register(process.self(), inject_input_name)
|
||||||
|
|
||||||
|
// Extract all finals and taps (that are defined)
|
||||||
|
let #(finals, taps) =
|
||||||
|
list.fold(listeners, #([], []), fn(acc, listener) {
|
||||||
|
let #(finals, taps) = acc
|
||||||
|
|
||||||
|
let finals = [listener.final, ..finals]
|
||||||
|
|
||||||
|
let taps = case listener.tap {
|
||||||
|
Some(t) -> [t, ..taps]
|
||||||
|
None -> taps
|
||||||
|
}
|
||||||
|
|
||||||
|
#(finals, taps)
|
||||||
|
})
|
||||||
|
|
||||||
|
read_input(finals, taps, inject_input)
|
||||||
|
})
|
||||||
|
|
||||||
|
Nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_input(
|
||||||
|
finals: List(Subject(Key)),
|
||||||
|
taps: List(Subject(List(String))),
|
||||||
|
inject_input: Subject(Key),
|
||||||
|
) -> Nil {
|
||||||
|
let buffer = case process.receive(inject_input, 1) {
|
||||||
|
Ok(key.Continue(buffer)) -> buffer
|
||||||
|
Ok(_) | Error(_) -> []
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ =
|
||||||
|
key.read_input_until_key(buffer, taps)
|
||||||
|
|> fn(k) { list.each(finals, process.send(_, k)) }
|
||||||
|
|
||||||
|
read_input(finals, taps, inject_input)
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@ import gleam/erlang/process.{type Subject}
|
|||||||
import gleam/list
|
import gleam/list
|
||||||
import gleam/string
|
import gleam/string
|
||||||
|
|
||||||
import input/internal as internal_input
|
import musicplayer/input/internal as internal_input
|
||||||
|
|
||||||
pub type Key {
|
pub type Key {
|
||||||
Char(String)
|
Char(String)
|
||||||
@@ -62,7 +62,7 @@ pub fn start_raw_shell() {
|
|||||||
|
|
||||||
pub fn read_input_until_key(
|
pub fn read_input_until_key(
|
||||||
l: List(String),
|
l: List(String),
|
||||||
tap_input: Subject(List(String)),
|
taps: List(Subject(List(String))),
|
||||||
) -> Key {
|
) -> Key {
|
||||||
case
|
case
|
||||||
internal_input.read_input()
|
internal_input.read_input()
|
||||||
@@ -71,8 +71,8 @@ pub fn read_input_until_key(
|
|||||||
|> from_list
|
|> from_list
|
||||||
{
|
{
|
||||||
Continue(l) -> {
|
Continue(l) -> {
|
||||||
process.send(tap_input, l)
|
list.each(taps, process.send(_, l))
|
||||||
read_input_until_key(l, tap_input)
|
read_input_until_key(l, taps)
|
||||||
}
|
}
|
||||||
k -> k
|
k -> k
|
||||||
}
|
}
|
||||||
@@ -2,10 +2,10 @@ import gleam/json
|
|||||||
import gleam/result
|
import gleam/result
|
||||||
import gleam/string
|
import gleam/string
|
||||||
|
|
||||||
import input/key.{type Key}
|
import musicplayer/input/key.{type Key}
|
||||||
import mpv/internal/control as internal_control
|
import musicplayer/mpv/internal as internal_control
|
||||||
import tcp/reason.{type Reason}
|
import musicplayer/tcp/reason.{type Reason}
|
||||||
import tcp/tcp.{type Socket}
|
import musicplayer/tcp/tcp.{type Socket}
|
||||||
|
|
||||||
pub type Control {
|
pub type Control {
|
||||||
TogglePlayPause
|
TogglePlayPause
|
||||||
89
src/musicplayer/mpv/mpv.gleam
Normal file
89
src/musicplayer/mpv/mpv.gleam
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
import gleam/erlang/process.{type Subject}
|
||||||
|
import gleam/float
|
||||||
|
import gleam/option.{None}
|
||||||
|
import gleam/otp/actor
|
||||||
|
import gleam/result
|
||||||
|
import gleam/string
|
||||||
|
|
||||||
|
import musicplayer/input/input.{type Listener, InputListener}
|
||||||
|
import musicplayer/input/key.{type Key}
|
||||||
|
import musicplayer/mpv/control.{type Control}
|
||||||
|
import musicplayer/tcp/reason
|
||||||
|
import musicplayer/tcp/tcp.{type Socket}
|
||||||
|
|
||||||
|
type State(socket, exit) {
|
||||||
|
State(socket: Socket, exit: Subject(Nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new(exit: Subject(Nil)) -> Result(Listener, String) {
|
||||||
|
// TODO start up mvp here, currently hi-jacking `naviterm`s socket
|
||||||
|
let socket_path = "/tmp/naviterm_mpv"
|
||||||
|
|
||||||
|
case tcp.connect(socket_path) {
|
||||||
|
Error(r) -> Error("Could not connect to mpv: " <> reason.to_string(r))
|
||||||
|
Ok(socket) -> {
|
||||||
|
let final_input_name = process.new_name("mpv_final_input")
|
||||||
|
let final_input: Subject(Key) = process.named_subject(final_input_name)
|
||||||
|
|
||||||
|
case
|
||||||
|
actor.new(State(socket, exit))
|
||||||
|
|> actor.on_message(handle_message)
|
||||||
|
|> actor.start
|
||||||
|
{
|
||||||
|
Error(start_error) ->
|
||||||
|
Error("Could not start actor: " <> string.inspect(start_error))
|
||||||
|
Ok(actor.Started(data:, ..)) -> {
|
||||||
|
echo "waiting for input"
|
||||||
|
key.start_raw_shell()
|
||||||
|
|
||||||
|
process.spawn(fn() {
|
||||||
|
let assert Ok(_) =
|
||||||
|
process.register(process.self(), final_input_name)
|
||||||
|
|
||||||
|
handle_key(final_input, data)
|
||||||
|
})
|
||||||
|
|
||||||
|
Ok(InputListener(final: final_input, tap: None))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_message(
|
||||||
|
state: State(socket, exit),
|
||||||
|
control: Control,
|
||||||
|
) -> actor.Next(State(socket, exit), Control) {
|
||||||
|
case control {
|
||||||
|
control.TogglePlayPause -> {
|
||||||
|
echo "toggling play/pause"
|
||||||
|
|
||||||
|
let _ =
|
||||||
|
result.map_error(control.toggle_play_pause(state.socket), fn(err) {
|
||||||
|
echo "Could not toggle play/pause: " <> err.details
|
||||||
|
})
|
||||||
|
|
||||||
|
let _ =
|
||||||
|
result.map(control.get_playback_time(state.socket), fn(playback) {
|
||||||
|
echo "playback: " <> float.to_string(playback.data)
|
||||||
|
})
|
||||||
|
|
||||||
|
actor.continue(state)
|
||||||
|
}
|
||||||
|
control.Exit -> {
|
||||||
|
process.send(state.exit, Nil)
|
||||||
|
actor.stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `handle_key` listens to a subject onto which `input` will send messages with
|
||||||
|
/// parsed `Key`s which will be mapped to `Control`s (if possible)
|
||||||
|
fn handle_key(final_input: Subject(Key), subject: Subject(Control)) -> Nil {
|
||||||
|
let _ =
|
||||||
|
process.receive_forever(final_input)
|
||||||
|
|> control.from_key
|
||||||
|
|> result.map(process.send(subject, _))
|
||||||
|
|
||||||
|
handle_key(final_input, subject)
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@ import gleam/bit_array
|
|||||||
import gleam/erlang/atom
|
import gleam/erlang/atom
|
||||||
import gleam/result
|
import gleam/result
|
||||||
|
|
||||||
import tcp/reason.{type Reason}
|
import musicplayer/tcp/reason.{type Reason}
|
||||||
|
|
||||||
pub type Socket
|
pub type Socket
|
||||||
|
|
||||||
@@ -4,8 +4,8 @@ import gleam/otp/actor
|
|||||||
import gleam/result
|
import gleam/result
|
||||||
import gleam/string
|
import gleam/string
|
||||||
|
|
||||||
import tcp/reason.{type Reason}
|
import musicplayer/tcp/reason.{type Reason}
|
||||||
import tcp/tcp
|
import musicplayer/tcp/tcp
|
||||||
|
|
||||||
pub type Message {
|
pub type Message {
|
||||||
Shutdown
|
Shutdown
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import gleam/list
|
import gleam/list
|
||||||
import gleeunit
|
import gleeunit
|
||||||
|
|
||||||
import input/key.{type Key, Char, csi, esc, input_introducer as ii}
|
import musicplayer/input/key.{type Key, Char, csi, esc, input_introducer as ii}
|
||||||
|
|
||||||
pub fn main() -> Nil {
|
pub fn main() -> Nil {
|
||||||
gleeunit.main()
|
gleeunit.main()
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
import gleam/list
|
import gleam/list
|
||||||
import gleeunit
|
import gleeunit
|
||||||
|
|
||||||
import input/key.{type Key, Char}
|
import musicplayer/input/key.{type Key, Char}
|
||||||
import mpv/control.{type Control}
|
import musicplayer/mpv/control.{type Control}
|
||||||
import mpv/internal/control as control_internal
|
import musicplayer/mpv/internal as control_internal
|
||||||
|
|
||||||
pub fn main() -> Nil {
|
pub fn main() -> Nil {
|
||||||
gleeunit.main()
|
gleeunit.main()
|
||||||
@@ -3,7 +3,7 @@ import gleeunit
|
|||||||
import simplifile
|
import simplifile
|
||||||
|
|
||||||
import echo_server
|
import echo_server
|
||||||
import tcp/tcp
|
import musicplayer/tcp/tcp
|
||||||
|
|
||||||
pub fn main() -> Nil {
|
pub fn main() -> Nil {
|
||||||
gleeunit.main()
|
gleeunit.main()
|
||||||
Reference in New Issue
Block a user