Compare commits
2 Commits
875eb9ced9
...
89a6f09a90
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
89a6f09a90 | ||
|
|
7c03656dfe |
@@ -1,4 +1,5 @@
|
||||
import gleam/erlang/atom
|
||||
import gleam/erlang/process.{type Subject}
|
||||
import gleam/list
|
||||
import gleam/string
|
||||
|
||||
@@ -57,14 +58,20 @@ pub fn start_raw_shell() {
|
||||
internal_input.shell_start_interactive(#(no_shell, raw))
|
||||
}
|
||||
|
||||
pub fn read_input_until_key(l: List(String)) -> Key {
|
||||
pub fn read_input_until_key(
|
||||
l: List(String),
|
||||
tap_input: Subject(List(String)),
|
||||
) -> Key {
|
||||
case
|
||||
internal_input.read_input()
|
||||
|> list.wrap
|
||||
|> list.append(l, _)
|
||||
|> from_list
|
||||
{
|
||||
Continue(l) -> read_input_until_key(l)
|
||||
Continue(l) -> {
|
||||
process.send(tap_input, l)
|
||||
read_input_until_key(l, tap_input)
|
||||
}
|
||||
k -> k
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,13 @@ 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, inject_input, tap_input, exit) {
|
||||
State(
|
||||
socket: Socket,
|
||||
inject_input: Subject(Key),
|
||||
tap_input: Subject(List(String)),
|
||||
exit: Subject(Nil),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
||||
@@ -26,8 +31,11 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
||||
let inject_input_name = process.new_name("inject_input")
|
||||
let inject_input = process.named_subject(inject_input_name)
|
||||
|
||||
let tap_input_name = process.new_name("tap_input")
|
||||
let tap_input = process.named_subject(tap_input_name)
|
||||
|
||||
case
|
||||
actor.new(State(socket, inject_input, exit))
|
||||
actor.new(State(socket, inject_input, tap_input, exit))
|
||||
|> actor.on_message(handle_message)
|
||||
|> actor.start
|
||||
{
|
||||
@@ -41,7 +49,13 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
||||
let assert Ok(_) =
|
||||
process.register(process.self(), inject_input_name)
|
||||
|
||||
read_input(data, inject_input)
|
||||
read_input(data, inject_input, tap_input)
|
||||
})
|
||||
|
||||
process.spawn(fn() {
|
||||
let assert Ok(_) = process.register(process.self(), tap_input_name)
|
||||
|
||||
input_output_loop(tap_input)
|
||||
})
|
||||
|
||||
Ok(Nil)
|
||||
@@ -52,10 +66,14 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
||||
}
|
||||
|
||||
fn handle_message(
|
||||
state: State(socket, inject, exit),
|
||||
state: State(socket, inject, input_output, exit),
|
||||
control: Control,
|
||||
) -> actor.Next(State(socket, inject, exit), Control) {
|
||||
) -> actor.Next(State(socket, inject, input_output, 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"
|
||||
|
||||
@@ -83,16 +101,20 @@ fn handle_message(
|
||||
/// messages to `inject_input` which will initialize the "input to key" sequence.
|
||||
/// This is useful to ultimately create a `Control` without the user having to
|
||||
/// input all of the character(s) needed.
|
||||
fn read_input(subject: Subject(Control), inject_input: Subject(Key)) -> Nil {
|
||||
fn read_input(
|
||||
subject: Subject(Control),
|
||||
inject_input: Subject(Key),
|
||||
tap_input: Subject(List(String)),
|
||||
) -> Nil {
|
||||
let buffer = case process.receive(inject_input, 1) {
|
||||
Ok(key.Continue(buffer)) -> buffer
|
||||
Ok(_) | Error(_) -> []
|
||||
}
|
||||
|
||||
let _ =
|
||||
key.read_input_until_key(buffer)
|
||||
key.read_input_until_key(buffer, tap_input)
|
||||
|> control.from_key
|
||||
|> result.map(process.send(subject, _))
|
||||
|
||||
read_input(subject, inject_input)
|
||||
read_input(subject, inject_input, tap_input)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user