This commit is contained in:
Alexander Heldt
2025-11-15 22:31:29 +01:00
parent 4c8fb9a8f9
commit a2ea7a3b31
4 changed files with 34 additions and 1 deletions

View File

@@ -17,6 +17,7 @@ gleam_stdlib = ">= 0.44.0 and < 2.0.0"
gleam_otp = ">= 1.2.0 and < 2.0.0" gleam_otp = ">= 1.2.0 and < 2.0.0"
gleam_erlang = ">= 1.3.0 and < 2.0.0" gleam_erlang = ">= 1.3.0 and < 2.0.0"
simplifile = ">= 2.3.1 and < 3.0.0" simplifile = ">= 2.3.1 and < 3.0.0"
gleam_json = ">= 3.1.0 and < 4.0.0"
[dev-dependencies] [dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0" gleeunit = ">= 1.0.0 and < 2.0.0"

View File

@@ -4,6 +4,7 @@
packages = [ packages = [
{ name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" }, { name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" },
{ name = "gleam_erlang", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "1124AD3AA21143E5AF0FC5CF3D9529F6DB8CA03E43A55711B60B6B7B3874375C" }, { name = "gleam_erlang", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "1124AD3AA21143E5AF0FC5CF3D9529F6DB8CA03E43A55711B60B6B7B3874375C" },
{ name = "gleam_json", version = "3.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "44FDAA8847BE8FC48CA7A1C089706BD54BADCC4C45B237A992EDDF9F2CDB2836" },
{ name = "gleam_otp", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "BA6A294E295E428EC1562DC1C11EA7530DCB981E8359134BEABC8493B7B2258E" }, { name = "gleam_otp", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "BA6A294E295E428EC1562DC1C11EA7530DCB981E8359134BEABC8493B7B2258E" },
{ name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" }, { name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" },
{ name = "gleeunit", version = "1.9.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "DA9553CE58B67924B3C631F96FE3370C49EB6D6DC6B384EC4862CC4AAA718F3C" }, { name = "gleeunit", version = "1.9.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "DA9553CE58B67924B3C631F96FE3370C49EB6D6DC6B384EC4862CC4AAA718F3C" },
@@ -12,6 +13,7 @@ packages = [
[requirements] [requirements]
gleam_erlang = { version = ">= 1.3.0 and < 2.0.0" } gleam_erlang = { version = ">= 1.3.0 and < 2.0.0" }
gleam_json = { version = ">= 3.1.0 and < 4.0.0" }
gleam_otp = { version = ">= 1.2.0 and < 2.0.0" } gleam_otp = { version = ">= 1.2.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" } gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" } gleeunit = { version = ">= 1.0.0 and < 2.0.0" }

View File

@@ -1,3 +1,6 @@
import gleam/json
import gleam/result
import mpv/internal.{type Key} import mpv/internal.{type Key}
import tcp/reason.{type Reason} import tcp/reason.{type Reason}
import tcp/tcp.{type Socket} import tcp/tcp.{type Socket}
@@ -23,5 +26,28 @@ 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, Reason) {
tcp.send(socket, "{\"command\":[\"cycle\",\"pause\"]}\n") 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)
})
} }

View File

@@ -48,6 +48,10 @@ fn handle_message(
result.map_error(control.toggle_play_pause(state.socket), fn(r) { result.map_error(control.toggle_play_pause(state.socket), fn(r) {
echo "Could not toggle play/pause: " <> reason.to_string(r) echo "Could not toggle play/pause: " <> reason.to_string(r)
}) })
let _ =
result.map(control.get_playback_time(state.socket), fn(playback) {
echo "playback: " <> playback
})
actor.continue(state) actor.continue(state)
} }
control.Exit -> { control.Exit -> {