Map Key to Control
This commit is contained in:
27
src/mpv/control.gleam
Normal file
27
src/mpv/control.gleam
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import mpv/internal.{type Key}
|
||||||
|
import tcp/reason.{type Reason}
|
||||||
|
import tcp/tcp.{type Socket}
|
||||||
|
|
||||||
|
pub type Control {
|
||||||
|
TogglePlayPause
|
||||||
|
Exit
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_key(key: Key) -> Result(Control, Nil) {
|
||||||
|
case key {
|
||||||
|
internal.Char(char) -> char_control(char)
|
||||||
|
_ -> Error(Nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn char_control(char: String) -> Result(Control, Nil) {
|
||||||
|
case char {
|
||||||
|
" " -> Ok(TogglePlayPause)
|
||||||
|
"q" -> Ok(Exit)
|
||||||
|
_ -> Error(Nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn toggle_play_pause(socket: Socket) -> Result(Nil, Reason) {
|
||||||
|
tcp.send(socket, "{\"command\":[\"cycle\",\"pause\"]}\n")
|
||||||
|
}
|
||||||
@@ -41,7 +41,6 @@ pub fn start_raw_shell() {
|
|||||||
shell_start_interactive(#(no_shell, raw))
|
shell_start_interactive(#(no_shell, raw))
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO map key to something like Control, to not leak `Continue` etc.
|
|
||||||
pub fn read_input_until_key(l: List(String)) -> Key {
|
pub fn read_input_until_key(l: List(String)) -> Key {
|
||||||
let l = read_input() |> list.wrap |> list.append(l, _)
|
let l = read_input() |> list.wrap |> list.append(l, _)
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,13 @@
|
|||||||
import gleam/erlang/process.{type Subject}
|
import gleam/erlang/process.{type Subject}
|
||||||
import gleam/otp/actor
|
import gleam/otp/actor
|
||||||
|
import gleam/result
|
||||||
import gleam/string
|
import gleam/string
|
||||||
|
|
||||||
import mpv/internal.{type Key, Char}
|
import mpv/control.{type Control}
|
||||||
import tcp/reason.{type Reason}
|
import mpv/internal
|
||||||
|
import tcp/reason
|
||||||
import tcp/tcp.{type Socket}
|
import tcp/tcp.{type Socket}
|
||||||
|
|
||||||
pub type Message {
|
|
||||||
KeyPress(Key)
|
|
||||||
}
|
|
||||||
|
|
||||||
type State(socket, exit) {
|
type State(socket, exit) {
|
||||||
State(socket: Socket, exit: Subject(Nil))
|
State(socket: Socket, exit: Subject(Nil))
|
||||||
}
|
}
|
||||||
@@ -39,28 +37,34 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toggle_play_pause(socket: Socket) -> Result(Nil, Reason) {
|
|
||||||
tcp.send(socket, "{\"command\":[\"cycle\",\"pause\"]}\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_message(
|
fn handle_message(
|
||||||
state: State(socket, exit),
|
state: State(socket, exit),
|
||||||
message: Message,
|
control: Control,
|
||||||
) -> actor.Next(State(socket, exit), Message) {
|
) -> actor.Next(State(socket, exit), Control) {
|
||||||
case message {
|
case control {
|
||||||
KeyPress(Char("q")) -> {
|
control.TogglePlayPause -> {
|
||||||
|
echo "toggling play/pause"
|
||||||
|
let _ =
|
||||||
|
result.map_error(control.toggle_play_pause(state.socket), fn(r) {
|
||||||
|
echo "Could not toggle play/pause: " <> reason.to_string(r)
|
||||||
|
})
|
||||||
|
actor.continue(state)
|
||||||
|
}
|
||||||
|
control.Exit -> {
|
||||||
process.send(state.exit, Nil)
|
process.send(state.exit, Nil)
|
||||||
actor.stop()
|
actor.stop()
|
||||||
}
|
}
|
||||||
KeyPress(key) -> {
|
|
||||||
echo "key: " <> string.inspect(key)
|
|
||||||
actor.continue(state)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_input(subject: Subject(Message)) -> Nil {
|
fn read_input(subject: Subject(Control)) -> Nil {
|
||||||
internal.read_input_until_key([]) |> KeyPress |> process.send(subject, _)
|
case
|
||||||
|
internal.read_input_until_key([])
|
||||||
|
|> control.from_key
|
||||||
|
{
|
||||||
|
Error(_) -> Nil
|
||||||
|
Ok(control) -> process.send(subject, control)
|
||||||
|
}
|
||||||
|
|
||||||
read_input(subject)
|
read_input(subject)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user