Add ability to inject characters into the input
This commit is contained in:
@@ -4,13 +4,13 @@ import gleam/otp/actor
|
|||||||
import gleam/result
|
import gleam/result
|
||||||
import gleam/string
|
import gleam/string
|
||||||
|
|
||||||
import input/key
|
import input/key.{type Key}
|
||||||
import mpv/control.{type Control}
|
import mpv/control.{type Control}
|
||||||
import tcp/reason
|
import tcp/reason
|
||||||
import tcp/tcp.{type Socket}
|
import tcp/tcp.{type Socket}
|
||||||
|
|
||||||
type State(socket, exit) {
|
type State(socket, inject_input, exit) {
|
||||||
State(socket: Socket, exit: Subject(Nil))
|
State(socket: Socket, inject_input: Subject(Key), exit: Subject(Nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
||||||
@@ -20,8 +20,14 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
|||||||
case tcp.connect(socket_path) {
|
case tcp.connect(socket_path) {
|
||||||
Error(r) -> Error("Could not connect to mpv: " <> reason.to_string(r))
|
Error(r) -> Error("Could not connect to mpv: " <> reason.to_string(r))
|
||||||
Ok(socket) -> {
|
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)
|
||||||
|
|
||||||
case
|
case
|
||||||
actor.new(State(socket, exit))
|
actor.new(State(socket, inject_input, exit))
|
||||||
|> actor.on_message(handle_message)
|
|> actor.on_message(handle_message)
|
||||||
|> actor.start
|
|> actor.start
|
||||||
{
|
{
|
||||||
@@ -30,7 +36,14 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
|||||||
Ok(actor.Started(data:, ..)) -> {
|
Ok(actor.Started(data:, ..)) -> {
|
||||||
echo "waiting for input"
|
echo "waiting for input"
|
||||||
key.start_raw_shell()
|
key.start_raw_shell()
|
||||||
process.spawn(fn() { read_input(data) })
|
|
||||||
|
process.spawn(fn() {
|
||||||
|
let assert Ok(_) =
|
||||||
|
process.register(process.self(), inject_input_name)
|
||||||
|
|
||||||
|
read_input(data, inject_input)
|
||||||
|
})
|
||||||
|
|
||||||
Ok(Nil)
|
Ok(Nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -39,9 +52,9 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn handle_message(
|
fn handle_message(
|
||||||
state: State(socket, exit),
|
state: State(socket, inject, exit),
|
||||||
control: Control,
|
control: Control,
|
||||||
) -> actor.Next(State(socket, exit), Control) {
|
) -> actor.Next(State(socket, inject, exit), Control) {
|
||||||
case control {
|
case control {
|
||||||
control.TogglePlayPause -> {
|
control.TogglePlayPause -> {
|
||||||
echo "toggling play/pause"
|
echo "toggling play/pause"
|
||||||
@@ -65,14 +78,21 @@ fn handle_message(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_input(subject: Subject(Control)) -> Nil {
|
/// `read_input` operates by reading from input until a `Key` can be created.
|
||||||
case
|
/// It is possible to create a `Key` without the users input by sending
|
||||||
key.read_input_until_key([])
|
/// messages to `inject_input` which will initialize the "input to key" sequence.
|
||||||
|> control.from_key
|
/// This is useful to ultimately create a `Control` without the user having to
|
||||||
{
|
/// input all of the character(s) needed.
|
||||||
Error(_) -> Nil
|
fn read_input(subject: Subject(Control), inject_input: Subject(Key)) -> Nil {
|
||||||
Ok(control) -> process.send(subject, control)
|
let buffer = case process.receive(inject_input, 1) {
|
||||||
|
Ok(key.Continue(buffer)) -> buffer
|
||||||
|
Ok(_) | Error(_) -> []
|
||||||
}
|
}
|
||||||
|
|
||||||
read_input(subject)
|
let _ =
|
||||||
|
key.read_input_until_key(buffer)
|
||||||
|
|> control.from_key
|
||||||
|
|> result.map(process.send(subject, _))
|
||||||
|
|
||||||
|
read_input(subject, inject_input)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user