Compare commits
6 Commits
254765c232
...
4bacbfe21f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4bacbfe21f | ||
|
|
7a22e0f389 | ||
|
|
12e0c3069b | ||
|
|
a2ea7a3b31 | ||
|
|
4c8fb9a8f9 | ||
|
|
ebdba09bc2 |
@@ -1,7 +1,9 @@
|
|||||||
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 mpv/internal/control as control_internal
|
||||||
import tcp/reason.{type Reason}
|
import tcp/reason.{type Reason}
|
||||||
import tcp/tcp.{type Socket}
|
import tcp/tcp.{type Socket}
|
||||||
|
|
||||||
@@ -10,9 +12,13 @@ pub type Control {
|
|||||||
Exit
|
Exit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type ControlError {
|
||||||
|
ControlError(details: String)
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -25,15 +31,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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://mpv.io/manual/master/#command-interface-playback-time
|
// https://mpv.io/manual/master/#command-interface-playback-time
|
||||||
pub fn get_playback_time(socket: Socket) -> Result(String, Reason) {
|
pub type PlaybackTime {
|
||||||
|
PlaybackTime(data: Float)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_playback_time(socket: Socket) -> Result(PlaybackTime, ControlError) {
|
||||||
let command =
|
let command =
|
||||||
json.object([
|
json.object([
|
||||||
#(
|
#(
|
||||||
@@ -42,7 +55,14 @@ pub fn get_playback_time(socket: Socket) -> Result(String, Reason) {
|
|||||||
),
|
),
|
||||||
])
|
])
|
||||||
|
|
||||||
send_command(socket, command)
|
case send_command(socket, command) {
|
||||||
|
Error(r) -> Error(ControlError(reason.to_string(r)))
|
||||||
|
Ok(json_string) ->
|
||||||
|
case control_internal.parse_playback_time(json_string) {
|
||||||
|
Error(e) -> Error(ControlError(string.inspect(e)))
|
||||||
|
Ok(data) -> Ok(PlaybackTime(data))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_command(socket: Socket, command: json.Json) -> Result(String, Reason) {
|
fn send_command(socket: Socket, command: json.Json) -> Result(String, Reason) {
|
||||||
|
|||||||
25
src/mpv/internal/control.gleam
Normal file
25
src/mpv/internal/control.gleam
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import gleam/dynamic/decode
|
||||||
|
import gleam/float
|
||||||
|
import gleam/json
|
||||||
|
import gleam/string
|
||||||
|
|
||||||
|
pub fn parse_playback_time(
|
||||||
|
json_string: String,
|
||||||
|
) -> Result(Float, json.DecodeError) {
|
||||||
|
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(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
json.parse(from: string.trim(json_string), using: decoder)
|
||||||
|
}
|
||||||
@@ -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 -> {
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ pub type Reason {
|
|||||||
/// from `send`
|
/// from `send`
|
||||||
Closed
|
Closed
|
||||||
|
|
||||||
Overflow
|
|
||||||
|
|
||||||
/// Address already in use
|
/// Address already in use
|
||||||
Eaddrinuse
|
Eaddrinuse
|
||||||
/// Cannot assign requested address
|
/// Cannot assign requested address
|
||||||
@@ -160,7 +158,6 @@ pub type Reason {
|
|||||||
|
|
||||||
pub fn to_string(reason: Reason) -> String {
|
pub fn to_string(reason: Reason) -> String {
|
||||||
case reason {
|
case reason {
|
||||||
Overflow -> "overflow"
|
|
||||||
Closed -> "Connection closed (closed)"
|
Closed -> "Connection closed (closed)"
|
||||||
Eacces -> "Permission denied (eacces)"
|
Eacces -> "Permission denied (eacces)"
|
||||||
Eaddrinuse -> "Address already in use (eaddrinuse)"
|
Eaddrinuse -> "Address already in use (eaddrinuse)"
|
||||||
|
|||||||
33
test/mpv/control_test.gleam
Normal file
33
test/mpv/control_test.gleam
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import gleam/list
|
||||||
|
import gleeunit
|
||||||
|
|
||||||
|
import mpv/control.{type Control}
|
||||||
|
import mpv/internal.{type Key, Char}
|
||||||
|
import mpv/internal/control as control_internal
|
||||||
|
|
||||||
|
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(data) = control_internal.parse_playback_time(json_string)
|
||||||
|
assert data == 123.456789
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user