Compare commits
2 Commits
4bacbfe21f
...
254765c232
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
254765c232 | ||
|
|
4c73244787 |
@@ -1,9 +1,7 @@
|
|||||||
import gleam/json
|
import gleam/json
|
||||||
import gleam/result
|
import gleam/result
|
||||||
import gleam/string
|
|
||||||
|
|
||||||
import mpv/internal.{type Key, Char}
|
import mpv/internal.{type Key}
|
||||||
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}
|
||||||
|
|
||||||
@@ -12,13 +10,9 @@ 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 {
|
||||||
Char(char) -> char_control(char)
|
internal.Char(char) -> char_control(char)
|
||||||
_ -> Error(Nil)
|
_ -> Error(Nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -31,22 +25,15 @@ fn char_control(char: String) -> Result(Control, Nil) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toggle_play_pause(socket: Socket) -> Result(Nil, ControlError) {
|
pub fn toggle_play_pause(socket: Socket) -> Result(Nil, Reason) {
|
||||||
let command =
|
let command =
|
||||||
json.object([#("command", json.array(["cycle", "pause"], of: json.string))])
|
json.object([#("command", json.array(["cycle", "pause"], of: json.string))])
|
||||||
|
|
||||||
case send_command(socket, command) {
|
result.map(send_command(socket, command), fn(_) { Nil })
|
||||||
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 type PlaybackTime {
|
pub fn get_playback_time(socket: Socket) -> Result(String, Reason) {
|
||||||
PlaybackTime(data: Float)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_playback_time(socket: Socket) -> Result(PlaybackTime, ControlError) {
|
|
||||||
let command =
|
let command =
|
||||||
json.object([
|
json.object([
|
||||||
#(
|
#(
|
||||||
@@ -55,14 +42,7 @@ pub fn get_playback_time(socket: Socket) -> Result(PlaybackTime, ControlError) {
|
|||||||
),
|
),
|
||||||
])
|
])
|
||||||
|
|
||||||
case send_command(socket, command) {
|
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) {
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
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,5 +1,4 @@
|
|||||||
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
|
||||||
@@ -45,17 +44,14 @@ 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(err) {
|
result.map_error(control.toggle_play_pause(state.socket), fn(r) {
|
||||||
echo "Could not toggle play/pause: " <> err.details
|
echo "Could not toggle play/pause: " <> reason.to_string(r)
|
||||||
})
|
})
|
||||||
|
|
||||||
let _ =
|
let _ =
|
||||||
result.map(control.get_playback_time(state.socket), fn(playback) {
|
result.map(control.get_playback_time(state.socket), fn(playback) {
|
||||||
echo "playback: " <> float.to_string(playback.data)
|
echo "playback: " <> playback
|
||||||
})
|
})
|
||||||
|
|
||||||
actor.continue(state)
|
actor.continue(state)
|
||||||
}
|
}
|
||||||
control.Exit -> {
|
control.Exit -> {
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ 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
|
||||||
@@ -158,6 +160,7 @@ 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)"
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
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