wip
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import gleam/dynamic/decode
|
||||
import gleam/float
|
||||
import gleam/json
|
||||
import gleam/result
|
||||
import gleam/string
|
||||
|
||||
import mpv/key.{type Key}
|
||||
import mpv/key.{type Key, Char}
|
||||
import tcp/reason.{type Reason}
|
||||
import tcp/tcp.{type Socket}
|
||||
|
||||
@@ -17,7 +19,7 @@ pub type ControlError {
|
||||
|
||||
pub fn from_key(key: Key) -> Result(Control, Nil) {
|
||||
case key {
|
||||
key.Char(char) -> char_control(char)
|
||||
Char(char) -> char_control(char)
|
||||
_ -> Error(Nil)
|
||||
}
|
||||
}
|
||||
@@ -30,20 +32,22 @@ fn char_control(char: String) -> Result(Control, Nil) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn toggle_play_pause(socket: Socket) -> Result(Nil, Reason) {
|
||||
pub fn toggle_play_pause(socket: Socket) -> Result(Nil, ControlError) {
|
||||
let command =
|
||||
json.object([#("command", json.array(["cycle", "pause"], of: json.string))])
|
||||
|
||||
result.map(send_command(socket, command), fn(_) { Nil })
|
||||
case send_command(socket, command) {
|
||||
Error(r) -> Error(ControlError(reason.to_string(r)))
|
||||
Ok(_) -> Ok(Nil)
|
||||
}
|
||||
}
|
||||
|
||||
// {\"data\":\"12.945425\",\"request_id\":0,\"error\":\"success\"}\n
|
||||
// https://mpv.io/manual/master/#command-interface-playback-time
|
||||
pub type PlaybackTime {
|
||||
PlaybackTime(data: Int, error: String)
|
||||
PlaybackTime(data: Float)
|
||||
}
|
||||
|
||||
pub fn get_playback_time(socket: Socket) -> Result(String, ControlError) {
|
||||
pub fn get_playback_time(socket: Socket) -> Result(PlaybackTime, ControlError) {
|
||||
let command =
|
||||
json.object([
|
||||
#(
|
||||
@@ -52,17 +56,34 @@ pub fn get_playback_time(socket: Socket) -> Result(String, ControlError) {
|
||||
),
|
||||
])
|
||||
|
||||
result.map(send_command(socket, command), fn(json_string: String) {
|
||||
let decoder = {
|
||||
use data <- decode.field("data", decode.int)
|
||||
use error <- decode.field("error", decode.string)
|
||||
decode.success(PlaybackTime(data:, error:))
|
||||
}
|
||||
case send_command(socket, command) {
|
||||
Error(r) -> Error(ControlError(reason.to_string(r)))
|
||||
Ok(json_string) -> parse_playback_time(json_string)
|
||||
}
|
||||
}
|
||||
|
||||
result.map_error(json.parse(from: json_string, using: decoder), fn(r) {
|
||||
todo
|
||||
})
|
||||
})
|
||||
fn parse_playback_time(
|
||||
json_string: String,
|
||||
) -> Result(PlaybackTime, ControlError) {
|
||||
let decoder = {
|
||||
let float_dececoder = fn(data_string) {
|
||||
case float.parse(data_string) {
|
||||
Error(_) -> decode.failure(0.0, "data")
|
||||
Ok(float_value) -> decode.success(float_value)
|
||||
}
|
||||
}
|
||||
use data <- decode.field(
|
||||
"data",
|
||||
decode.then(decode.string, float_dececoder),
|
||||
)
|
||||
|
||||
decode.success(PlaybackTime(data))
|
||||
}
|
||||
|
||||
result.map_error(
|
||||
json.parse(from: string.trim(json_string), using: decoder),
|
||||
fn(r) { ControlError(string.inspect(r)) },
|
||||
)
|
||||
}
|
||||
|
||||
fn send_command(socket: Socket, command: json.Json) -> Result(String, Reason) {
|
||||
|
||||
Reference in New Issue
Block a user