Map Key to Control
This commit is contained in:
@@ -1,15 +1,13 @@
|
||||
import gleam/erlang/process.{type Subject}
|
||||
import gleam/otp/actor
|
||||
import gleam/result
|
||||
import gleam/string
|
||||
|
||||
import mpv/key.{type Key, Char}
|
||||
import tcp/reason.{type Reason}
|
||||
import mpv/control.{type Control}
|
||||
import mpv/key
|
||||
import tcp/reason
|
||||
import tcp/tcp.{type Socket}
|
||||
|
||||
pub type Message {
|
||||
KeyPress(Key)
|
||||
}
|
||||
|
||||
type State(socket, exit) {
|
||||
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(
|
||||
state: State(socket, exit),
|
||||
message: Message,
|
||||
) -> actor.Next(State(socket, exit), Message) {
|
||||
case message {
|
||||
KeyPress(Char("q")) -> {
|
||||
control: Control,
|
||||
) -> actor.Next(State(socket, exit), Control) {
|
||||
case control {
|
||||
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)
|
||||
actor.stop()
|
||||
}
|
||||
KeyPress(key) -> {
|
||||
echo "key: " <> string.inspect(key)
|
||||
actor.continue(state)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_input(subject: Subject(Message)) -> Nil {
|
||||
key.read_input_until_key([]) |> KeyPress |> process.send(subject, _)
|
||||
fn read_input(subject: Subject(Control)) -> Nil {
|
||||
case
|
||||
key.read_input_until_key([])
|
||||
|> control.from_key
|
||||
{
|
||||
Error(_) -> Nil
|
||||
Ok(control) -> process.send(subject, control)
|
||||
}
|
||||
|
||||
read_input(subject)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user