Compare commits

2 Commits

Author SHA1 Message Date
Alexander Heldt
8e15c990fa iwp 2025-11-18 23:03:10 +01:00
Alexander Heldt
e1dc3c1de7 wip 2025-11-18 21:25:35 +01:00
3 changed files with 58 additions and 33 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

@@ -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,12 +36,16 @@ 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()
let input = process.new_subject()
process.self()
process.receive
process.spawn(fn() { input_handler(input, data) }) process.spawn(fn() {
process.spawn(fn() { read_input(data) }) let input = process.new_subject()
let assert Ok(_) =
process.register(process.self(), inject_input_name)
process.send(input, key.Continue([]))
read_input(data, input, inject_input)
})
Ok(Nil) Ok(Nil)
} }
} }
@@ -44,11 +54,14 @@ 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.Search -> todo control.Search -> {
process.send(state.inject_input, key.Continue([key.input_introducer]))
actor.continue(state)
}
control.TogglePlayPause -> { control.TogglePlayPause -> {
echo "toggling play/pause" echo "toggling play/pause"
@@ -71,32 +84,40 @@ fn handle_message(
} }
} }
// TODO create new input_handler_loop that recieves on a subject and /// read_input selects from two subjects: `input` and `inject`
// TODO read_input reads until a key is found, and then sends it to /// `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
_ -> []
}
// TODO a function that can recieve on a subject, and it can receive two types of messages:
// 1. a Control message from read_input
// 2. an injected controlMessage with pre-defined buffer
// it then forwards the message to the actor subject
fn read_input(input_handler: Subject(Control)) -> Nil {
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_handler, control) Ok(control) -> process.send(agent, control)
} }
read_input(input_handler) // 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([]))
input: Subject(Control), read_input(agent, input, inject_input)
actor_subject: Subject(Control),
) -> Nil {
case process.receive(input, 1000) {
Error(_) -> Nil
Ok(control) -> process.send(actor_subject, control)
}
} }