This commit is contained in:
Alexander Heldt
2025-11-18 23:03:10 +01:00
parent e1dc3c1de7
commit 8e15c990fa
3 changed files with 48 additions and 54 deletions

View File

@@ -22,7 +22,7 @@ pub const esc = "\u{001B}"
pub const csi = "[" pub const csi = "["
// input introducer // input introducer
const input_introducer = "$" pub const input_introducer = "::"
pub fn from_list(l: List(String)) -> Key { pub fn from_list(l: List(String)) -> Key {
case l { case l {

View File

@@ -19,6 +19,10 @@ pub type ControlError {
ControlError(details: String) ControlError(details: String)
} }
// TODO this should also have a context:
// `/` in "artist list" "context will should be`control.Search`
// `<some char>` in "create new playlist" context should be `control.Input`
// `q` in most contexts should be `Exit`, but in a popup it should be `Close`
pub fn from_key(key: Key) -> Result(Control, Nil) { pub fn from_key(key: Key) -> Result(Control, Nil) {
echo key echo key
case key { case key {

View File

@@ -9,8 +9,8 @@ import mpv/control.{type Control}
import tcp/reason import tcp/reason
import tcp/tcp.{type Socket} import tcp/tcp.{type Socket}
type State(socket, input, exit) { type State(socket, inject_input, exit) {
State(socket: Socket, inject: Subject(Key), 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,14 +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) -> {
let input_name = process.new_name("input") // `inject_input` is created by name to allow the process that
let input = process.named_subject(input_name) // 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_name = process.new_name("inject") let inject_input_name = process.new_name("inject_input")
let inject = process.named_subject(inject_name) let inject_input = process.named_subject(inject_input_name)
case case
actor.new(State(socket, inject, exit)) actor.new(State(socket, inject_input, exit))
|> actor.on_message(handle_message) |> actor.on_message(handle_message)
|> actor.start |> actor.start
{ {
@@ -37,28 +37,15 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
echo "waiting for input" echo "waiting for input"
key.start_raw_shell() key.start_raw_shell()
// read_input should just have access to input, for sending
// input hnadler should register both inject and input
process.spawn(fn() { process.spawn(fn() {
echo "registering?" let input = process.new_subject()
let register_input = process.register(process.self(), input_name) let assert Ok(_) =
let register_inject = process.register(process.self(), inject_name) process.register(process.self(), inject_input_name)
case register_input, register_inject { process.send(input, key.Continue([]))
Ok(_), Ok(_) -> { read_input(data, input, inject_input)
echo "registered both"
input_handler(data, input, inject)
}
_, _ -> {
echo "failed to register"
Error("TODO panic?")
}
}
}) })
process.spawn(fn() { read_input(input) })
Ok(Nil) Ok(Nil)
} }
} }
@@ -72,7 +59,7 @@ fn handle_message(
) -> actor.Next(State(socket, inject, exit), Control) { ) -> actor.Next(State(socket, inject, exit), Control) {
case control { case control {
control.Search -> { control.Search -> {
process.send(state.inject, key.Continue(["$"])) process.send(state.inject_input, key.Continue([key.input_introducer]))
actor.continue(state) actor.continue(state)
} }
control.TogglePlayPause -> { control.TogglePlayPause -> {
@@ -97,37 +84,40 @@ fn handle_message(
} }
} }
fn read_input(input: Subject(Control)) -> Nil { /// read_input selects from two subjects: `input` and `inject`
echo "### read_input" /// `inject` can be used by the agent to send back input character(s)
/// to force the input to be something other than user input.
///
/// This is useful to create a `Key` without the user having to
/// input all of the character(s), to then be able to create a
/// `Control` from that `Key`
fn read_input(
agent: Subject(Control),
input: Subject(Key),
inject_input: Subject(Key),
) -> Nil {
let buffer = case
process.new_selector()
|> process.select(input)
|> process.select(inject_input)
|> process.selector_receive_forever
{
key.Continue(buffer) -> buffer
_ -> []
}
case case
key.read_input_until_key([]) key.read_input_until_key(buffer)
|> control.from_key |> control.from_key
{ {
Error(_) -> Nil Error(_) -> Nil
Ok(control) -> process.send(input, control) Ok(control) -> process.send(agent, control)
} }
read_input(input) // currently needed to allow injects to be received. not nice
} // maybe timed out select on inject first, then normal?
process.sleep(100)
fn input_handler( process.send(input, key.Continue([]))
actor: Subject(Control), read_input(agent, input, inject_input)
input: Subject(Control),
inject: Subject(Key),
) {
echo "### input_handler"
case
process.new_selector()
|> process.select_map(input, fn(i) { Ok(i) })
|> process.select_map(inject, control.from_key)
|> process.selector_receive_forever
{
Error(_) -> Nil
Ok(c) -> {
echo "input_handler: " <> string.inspect(c)
process.send(actor, c)
}
}
input_handler(actor, input, inject)
} }