Compare commits
2 Commits
d1053f687b
...
8e15c990fa
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8e15c990fa | ||
|
|
e1dc3c1de7 |
@@ -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 {
|
||||
|
||||
@@ -10,6 +10,8 @@ import tcp/tcp.{type Socket}
|
||||
pub type Control {
|
||||
TogglePlayPause
|
||||
|
||||
Search
|
||||
|
||||
Exit
|
||||
}
|
||||
|
||||
@@ -17,7 +19,12 @@ 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 {
|
||||
key.Char(char) -> char_control(char)
|
||||
_ -> Error(Nil)
|
||||
@@ -28,6 +35,7 @@ fn char_control(char: String) -> Result(Control, Nil) {
|
||||
case char {
|
||||
" " -> Ok(TogglePlayPause)
|
||||
"q" -> Ok(Exit)
|
||||
"/" -> Ok(Search)
|
||||
_ -> Error(Nil)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,13 +4,13 @@ import gleam/otp/actor
|
||||
import gleam/result
|
||||
import gleam/string
|
||||
|
||||
import input/key
|
||||
import input/key.{type Key}
|
||||
import mpv/control.{type Control}
|
||||
import tcp/reason
|
||||
import tcp/tcp.{type Socket}
|
||||
|
||||
type State(socket, exit) {
|
||||
State(socket: Socket, 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,8 +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) -> {
|
||||
// `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, exit))
|
||||
actor.new(State(socket, inject_input, exit))
|
||||
|> actor.on_message(handle_message)
|
||||
|> actor.start
|
||||
{
|
||||
@@ -30,7 +36,16 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
||||
Ok(actor.Started(data:, ..)) -> {
|
||||
echo "waiting for input"
|
||||
key.start_raw_shell()
|
||||
process.spawn(fn() { read_input(data) })
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
Ok(Nil)
|
||||
}
|
||||
}
|
||||
@@ -39,10 +54,14 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
||||
}
|
||||
|
||||
fn handle_message(
|
||||
state: State(socket, exit),
|
||||
state: State(socket, inject, exit),
|
||||
control: Control,
|
||||
) -> actor.Next(State(socket, exit), Control) {
|
||||
) -> actor.Next(State(socket, inject, exit), Control) {
|
||||
case control {
|
||||
control.Search -> {
|
||||
process.send(state.inject_input, key.Continue([key.input_introducer]))
|
||||
actor.continue(state)
|
||||
}
|
||||
control.TogglePlayPause -> {
|
||||
echo "toggling play/pause"
|
||||
|
||||
@@ -65,14 +84,40 @@ fn handle_message(
|
||||
}
|
||||
}
|
||||
|
||||
fn read_input(subject: Subject(Control)) -> Nil {
|
||||
/// 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(subject, control)
|
||||
Ok(control) -> process.send(agent, control)
|
||||
}
|
||||
|
||||
read_input(subject)
|
||||
// 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user