54 lines
1.2 KiB
Gleam
54 lines
1.2 KiB
Gleam
import gleam/json
|
|
import gleam/result
|
|
|
|
import mpv/key.{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 {
|
|
key.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) {
|
|
let command =
|
|
json.object([#("command", json.array(["cycle", "pause"], of: json.string))])
|
|
|
|
result.map(send_command(socket, command), fn(_) { Nil })
|
|
}
|
|
|
|
// https://mpv.io/manual/master/#command-interface-playback-time
|
|
pub fn get_playback_time(socket: Socket) -> Result(String, Reason) {
|
|
let command =
|
|
json.object([
|
|
#(
|
|
"command",
|
|
json.array(["get_property_string", "playback-time"], of: json.string),
|
|
),
|
|
])
|
|
|
|
send_command(socket, command)
|
|
}
|
|
|
|
fn send_command(socket: Socket, command: json.Json) -> Result(String, Reason) {
|
|
result.try(tcp.send(socket, json.to_string(command) <> "\n"), fn(_) {
|
|
let timeout_ms = 10_000
|
|
tcp.receive(socket, timeout_ms)
|
|
})
|
|
}
|