wip
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
import gleam/dynamic/decode
|
import gleam/dynamic/decode
|
||||||
|
import gleam/float
|
||||||
import gleam/json
|
import gleam/json
|
||||||
import gleam/result
|
import gleam/result
|
||||||
|
import gleam/string
|
||||||
|
|
||||||
import mpv/internal.{type Key}
|
import mpv/internal.{type Key, Char}
|
||||||
import tcp/reason.{type Reason}
|
import tcp/reason.{type Reason}
|
||||||
import tcp/tcp.{type Socket}
|
import tcp/tcp.{type Socket}
|
||||||
|
|
||||||
@@ -17,7 +19,7 @@ pub type ControlError {
|
|||||||
|
|
||||||
pub fn from_key(key: Key) -> Result(Control, Nil) {
|
pub fn from_key(key: Key) -> Result(Control, Nil) {
|
||||||
case key {
|
case key {
|
||||||
internal.Char(char) -> char_control(char)
|
Char(char) -> char_control(char)
|
||||||
_ -> Error(Nil)
|
_ -> 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 =
|
let command =
|
||||||
json.object([#("command", json.array(["cycle", "pause"], of: json.string))])
|
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
|
// https://mpv.io/manual/master/#command-interface-playback-time
|
||||||
pub type PlaybackTime {
|
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 =
|
let command =
|
||||||
json.object([
|
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) {
|
case send_command(socket, command) {
|
||||||
let decoder = {
|
Error(r) -> Error(ControlError(reason.to_string(r)))
|
||||||
use data <- decode.field("data", decode.int)
|
Ok(json_string) -> parse_playback_time(json_string)
|
||||||
use error <- decode.field("error", decode.string)
|
}
|
||||||
decode.success(PlaybackTime(data:, error:))
|
}
|
||||||
}
|
|
||||||
|
|
||||||
result.map_error(json.parse(from: json_string, using: decoder), fn(r) {
|
fn parse_playback_time(
|
||||||
todo
|
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) {
|
fn send_command(socket: Socket, command: json.Json) -> Result(String, Reason) {
|
||||||
|
|||||||
23
src/mpv/internal/control.gleam
Normal file
23
src/mpv/internal/control.gleam
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
// 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)) },
|
||||||
|
// )
|
||||||
|
// }
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import gleam/erlang/process.{type Subject}
|
import gleam/erlang/process.{type Subject}
|
||||||
|
import gleam/float
|
||||||
import gleam/otp/actor
|
import gleam/otp/actor
|
||||||
import gleam/result
|
import gleam/result
|
||||||
import gleam/string
|
import gleam/string
|
||||||
@@ -44,14 +45,17 @@ fn handle_message(
|
|||||||
case control {
|
case control {
|
||||||
control.TogglePlayPause -> {
|
control.TogglePlayPause -> {
|
||||||
echo "toggling play/pause"
|
echo "toggling play/pause"
|
||||||
|
|
||||||
let _ =
|
let _ =
|
||||||
result.map_error(control.toggle_play_pause(state.socket), fn(r) {
|
result.map_error(control.toggle_play_pause(state.socket), fn(err) {
|
||||||
echo "Could not toggle play/pause: " <> reason.to_string(r)
|
echo "Could not toggle play/pause: " <> err.details
|
||||||
})
|
})
|
||||||
|
|
||||||
let _ =
|
let _ =
|
||||||
result.map(control.get_playback_time(state.socket), fn(playback) {
|
result.map(control.get_playback_time(state.socket), fn(playback) {
|
||||||
echo "playback: " <> playback
|
echo "playback: " <> float.to_string(playback.data)
|
||||||
})
|
})
|
||||||
|
|
||||||
actor.continue(state)
|
actor.continue(state)
|
||||||
}
|
}
|
||||||
control.Exit -> {
|
control.Exit -> {
|
||||||
|
|||||||
30
test/mpv/control_test.gleam
Normal file
30
test/mpv/control_test.gleam
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import gleam/list
|
||||||
|
import gleeunit
|
||||||
|
|
||||||
|
import mpv/control.{type Control}
|
||||||
|
import mpv/internal.{type Key, Char}
|
||||||
|
|
||||||
|
pub fn main() -> Nil {
|
||||||
|
gleeunit.main()
|
||||||
|
}
|
||||||
|
|
||||||
|
type TestCase {
|
||||||
|
TestCase(key: Key, expected: Result(Control, Nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn control_from_key_test() {
|
||||||
|
let test_cases = [
|
||||||
|
TestCase(Char(" "), Ok(control.TogglePlayPause)),
|
||||||
|
TestCase(Char("q"), Ok(control.Exit)),
|
||||||
|
]
|
||||||
|
|
||||||
|
list.each(test_cases, fn(tc) {
|
||||||
|
assert tc.expected == control.from_key(tc.key)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// pub fn parse_playback_time_test() {
|
||||||
|
// let json_string =
|
||||||
|
// "{\"data\":\"123.456789\",\"request_id\":0,\"error\":\"success\"}\n"
|
||||||
|
|
||||||
|
// let assert Ok(_) = control.parse_p
|
||||||
|
// }
|
||||||
Reference in New Issue
Block a user