Compare commits

1 Commits

Author SHA1 Message Date
Alexander Heldt
efe14200fe wip 2025-11-18 18:42:11 +01:00
3 changed files with 34 additions and 59 deletions

View File

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

View File

@@ -19,10 +19,6 @@ pub type ControlError {
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) {
echo key
case key {

View File

@@ -4,13 +4,13 @@ import gleam/otp/actor
import gleam/result
import gleam/string
import input/key.{type Key}
import input/key
import mpv/control.{type Control}
import tcp/reason
import tcp/tcp.{type Socket}
type State(socket, inject_input, exit) {
State(socket: Socket, inject_input: Subject(Key), exit: Subject(Nil))
type State(socket, exit) {
State(socket: Socket, exit: Subject(Nil))
}
pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
@@ -20,14 +20,8 @@ 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) -> {
// `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_input, exit))
actor.new(State(socket, exit))
|> actor.on_message(handle_message)
|> actor.start
{
@@ -36,16 +30,12 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
Ok(actor.Started(data:, ..)) -> {
echo "waiting for input"
key.start_raw_shell()
let input = process.new_subject()
process.self()
process.receive
process.spawn(fn() {
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)
})
process.spawn(fn() { input_handler(input, data) })
process.spawn(fn() { read_input(data) })
Ok(Nil)
}
}
@@ -54,14 +44,11 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
}
fn handle_message(
state: State(socket, inject, exit),
state: State(socket, exit),
control: Control,
) -> actor.Next(State(socket, inject, exit), Control) {
) -> actor.Next(State(socket, exit), Control) {
case control {
control.Search -> {
process.send(state.inject_input, key.Continue([key.input_introducer]))
actor.continue(state)
}
control.Search -> todo
control.TogglePlayPause -> {
echo "toggling play/pause"
@@ -84,40 +71,32 @@ fn handle_message(
}
}
/// 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
_ -> []
}
// TODO create new input_handler_loop that recieves on a subject and
// TODO read_input reads until a key is found, and then sends it to
// 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
key.read_input_until_key(buffer)
key.read_input_until_key([])
|> control.from_key
{
Error(_) -> Nil
Ok(control) -> process.send(agent, control)
Ok(control) -> process.send(input_handler, control)
}
// currently needed to allow injects to be received. not nice
// maybe timed out select on inject first, then normal?
process.sleep(100)
process.send(input, key.Continue([]))
read_input(agent, input, inject_input)
read_input(input_handler)
}
fn input_handler(
input: Subject(Control),
actor_subject: Subject(Control),
) -> Nil {
case process.receive(input, 1000) {
Error(_) -> Nil
Ok(control) -> process.send(actor_subject, control)
}
}