From 8e15c990fad054affb346afe5932c9667b0c19b4 Mon Sep 17 00:00:00 2001 From: Alexander Heldt Date: Tue, 18 Nov 2025 23:03:10 +0100 Subject: [PATCH] iwp --- src/input/key.gleam | 2 +- src/mpv/control.gleam | 4 ++ src/mpv/mpv.gleam | 96 +++++++++++++++++++------------------------ 3 files changed, 48 insertions(+), 54 deletions(-) diff --git a/src/input/key.gleam b/src/input/key.gleam index 5d032b4..203488c 100644 --- a/src/input/key.gleam +++ b/src/input/key.gleam @@ -22,7 +22,7 @@ pub const esc = "\u{001B}" pub const csi = "[" // input introducer -const input_introducer = "$" +pub const input_introducer = "::" pub fn from_list(l: List(String)) -> Key { case l { diff --git a/src/mpv/control.gleam b/src/mpv/control.gleam index 9db8c55..ee188d9 100644 --- a/src/mpv/control.gleam +++ b/src/mpv/control.gleam @@ -19,6 +19,10 @@ pub type ControlError { ControlError(details: String) } +// TODO this should also have a context: +// `/` in "artist list" "context will should be`control.Search` +// `` 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) { echo key case key { diff --git a/src/mpv/mpv.gleam b/src/mpv/mpv.gleam index cd170fe..cfe668e 100644 --- a/src/mpv/mpv.gleam +++ b/src/mpv/mpv.gleam @@ -9,8 +9,8 @@ import mpv/control.{type Control} import tcp/reason import tcp/tcp.{type Socket} -type State(socket, input, exit) { - State(socket: Socket, inject: Subject(Key), exit: Subject(Nil)) +type State(socket, inject_input, exit) { + State(socket: Socket, inject_input: Subject(Key), exit: Subject(Nil)) } 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) { Error(r) -> Error("Could not connect to mpv: " <> reason.to_string(r)) Ok(socket) -> { - let input_name = process.new_name("input") - let input = process.named_subject(input_name) - - let inject_name = process.new_name("inject") - let inject = process.named_subject(inject_name) + // `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 - actor.new(State(socket, inject, exit)) + actor.new(State(socket, inject_input, exit)) |> actor.on_message(handle_message) |> actor.start { @@ -37,28 +37,15 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) { echo "waiting for input" 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() { - echo "registering?" - let register_input = process.register(process.self(), input_name) - let register_inject = process.register(process.self(), inject_name) + let input = process.new_subject() + let assert Ok(_) = + process.register(process.self(), inject_input_name) - case register_input, register_inject { - Ok(_), Ok(_) -> { - echo "registered both" - input_handler(data, input, inject) - } - _, _ -> { - echo "failed to register" - Error("TODO panic?") - } - } + process.send(input, key.Continue([])) + read_input(data, input, inject_input) }) - process.spawn(fn() { read_input(input) }) - Ok(Nil) } } @@ -72,7 +59,7 @@ fn handle_message( ) -> actor.Next(State(socket, inject, exit), Control) { case control { control.Search -> { - process.send(state.inject, key.Continue(["$"])) + process.send(state.inject_input, key.Continue([key.input_introducer])) actor.continue(state) } control.TogglePlayPause -> { @@ -97,37 +84,40 @@ fn handle_message( } } -fn read_input(input: Subject(Control)) -> Nil { - echo "### read_input" +/// read_input selects from two subjects: `input` and `inject` +/// `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 - key.read_input_until_key([]) + key.read_input_until_key(buffer) |> control.from_key { 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( - actor: Subject(Control), - 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) + process.send(input, key.Continue([])) + read_input(agent, input, inject_input) }