Compare commits

6 Commits

Author SHA1 Message Date
Alexander Heldt
3fb92d4654 wip 2025-11-16 16:12:19 +01:00
Alexander Heldt
c3df22ecd8 wip 2025-11-16 16:09:14 +01:00
Alexander Heldt
2dee0247dc wip 2025-11-16 16:08:19 +01:00
Alexander Heldt
d50a920490 json 2025-11-16 16:08:18 +01:00
Alexander Heldt
c42bf67d73 Map Key to Control 2025-11-16 16:07:49 +01:00
Alexander Heldt
702313eac2 Restructure mpv/internal package 2025-11-16 16:04:06 +01:00
7 changed files with 55 additions and 52 deletions

View File

@@ -2,8 +2,8 @@ import gleam/json
import gleam/result import gleam/result
import gleam/string import gleam/string
import mpv/internal.{type Key, Char} import mpv/internal/control as internal_control
import mpv/internal/control as control_internal import mpv/key.{type Key, Char}
import tcp/reason.{type Reason} import tcp/reason.{type Reason}
import tcp/tcp.{type Socket} import tcp/tcp.{type Socket}
@@ -58,7 +58,7 @@ pub fn get_playback_time(socket: Socket) -> Result(PlaybackTime, ControlError) {
case send_command(socket, command) { case send_command(socket, command) {
Error(r) -> Error(ControlError(reason.to_string(r))) Error(r) -> Error(ControlError(reason.to_string(r)))
Ok(json_string) -> Ok(json_string) ->
case control_internal.parse_playback_time(json_string) { case internal_control.parse_playback_time(json_string) {
Error(e) -> Error(ControlError(string.inspect(e))) Error(e) -> Error(ControlError(string.inspect(e)))
Ok(data) -> Ok(PlaybackTime(data)) Ok(data) -> Ok(PlaybackTime(data))
} }

View File

@@ -0,0 +1,15 @@
import gleam/erlang/atom
pub fn read_input() -> String {
io_get_chars("", 1)
}
pub type NotUsed
// https://www.erlang.org/doc/apps/stdlib/shell.html#start_interactive/1
@external(erlang, "shell", "start_interactive")
pub fn shell_start_interactive(options: #(atom.Atom, atom.Atom)) -> NotUsed
// https://www.erlang.org/doc/apps/stdlib/io.html#get_line/1
@external(erlang, "io", "get_chars")
fn io_get_chars(prompt: String, count: Int) -> String

View File

@@ -1,6 +1,8 @@
import gleam/erlang/atom import gleam/erlang/atom
import gleam/list import gleam/list
import mpv/internal/key as internal_key
pub type Key { pub type Key {
Char(String) Char(String)
@@ -38,28 +40,14 @@ pub fn from_list(l: List(String)) -> Key {
pub fn start_raw_shell() { pub fn start_raw_shell() {
let no_shell = atom.create("noshell") let no_shell = atom.create("noshell")
let raw = atom.create("raw") let raw = atom.create("raw")
shell_start_interactive(#(no_shell, raw)) internal_key.shell_start_interactive(#(no_shell, raw))
} }
pub fn read_input_until_key(l: List(String)) -> Key { pub fn read_input_until_key(l: List(String)) -> Key {
let l = read_input() |> list.wrap |> list.append(l, _) let l = internal_key.read_input() |> list.wrap |> list.append(l, _)
case from_list(l) { case from_list(l) {
Continue -> read_input_until_key(l) Continue -> read_input_until_key(l)
k -> k k -> k
} }
} }
fn read_input() -> String {
io_get_chars("", 1)
}
pub type NotUsed
// https://www.erlang.org/doc/apps/stdlib/shell.html#start_interactive/1
@external(erlang, "shell", "start_interactive")
fn shell_start_interactive(options: #(atom.Atom, atom.Atom)) -> NotUsed
// https://www.erlang.org/doc/apps/stdlib/io.html#get_line/1
@external(erlang, "io", "get_chars")
fn io_get_chars(prompt: String, count: Int) -> String

View File

@@ -5,7 +5,7 @@ import gleam/result
import gleam/string import gleam/string
import mpv/control.{type Control} import mpv/control.{type Control}
import mpv/internal import mpv/key
import tcp/reason import tcp/reason
import tcp/tcp.{type Socket} import tcp/tcp.{type Socket}
@@ -29,7 +29,7 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
Error("Could not start actor: " <> string.inspect(start_error)) Error("Could not start actor: " <> string.inspect(start_error))
Ok(actor.Started(data:, ..)) -> { Ok(actor.Started(data:, ..)) -> {
echo "waiting for input" echo "waiting for input"
internal.start_raw_shell() key.start_raw_shell()
process.spawn(fn() { read_input(data) }) process.spawn(fn() { read_input(data) })
Ok(Nil) Ok(Nil)
} }
@@ -67,7 +67,7 @@ fn handle_message(
fn read_input(subject: Subject(Control)) -> Nil { fn read_input(subject: Subject(Control)) -> Nil {
case case
internal.read_input_until_key([]) key.read_input_until_key([])
|> control.from_key |> control.from_key
{ {
Error(_) -> Nil Error(_) -> Nil

View File

@@ -2,8 +2,8 @@ import gleam/list
import gleeunit import gleeunit
import mpv/control.{type Control} import mpv/control.{type Control}
import mpv/internal.{type Key, Char}
import mpv/internal/control as control_internal import mpv/internal/control as control_internal
import mpv/key.{type Key, Char}
pub fn main() -> Nil { pub fn main() -> Nil {
gleeunit.main() gleeunit.main()

29
test/mpv/key_test.gleam Normal file
View File

@@ -0,0 +1,29 @@
import gleam/list
import gleeunit
import mpv/key.{type Key, Char, csi, esc}
pub fn main() -> Nil {
gleeunit.main()
}
type TestCase {
TestCase(input: List(String), expected: Key)
}
pub fn key_from_list_test() {
let test_cases = [
TestCase(["c"], Char("c")),
TestCase([esc, csi, "D"], key.Left),
TestCase([esc, csi, "C"], key.Right),
TestCase([esc, csi, "A"], key.Up),
TestCase([esc, csi, "B"], key.Down),
TestCase([esc, csi], key.Continue),
TestCase([esc], key.Continue),
TestCase([], key.Continue),
]
list.each(test_cases, fn(tc) {
assert tc.expected == key.from_list(tc.input)
})
}

View File

@@ -1,29 +0,0 @@
import gleam/list
import gleeunit
import mpv/internal.{Char, csi, esc}
pub fn main() -> Nil {
gleeunit.main()
}
type TestCase {
TestCase(input: List(String), expected: internal.Key)
}
pub fn mpv_key_from_list_test() {
let test_cases = [
TestCase(["c"], Char("c")),
TestCase([esc, csi, "D"], internal.Left),
TestCase([esc, csi, "C"], internal.Right),
TestCase([esc, csi, "A"], internal.Up),
TestCase([esc, csi, "B"], internal.Down),
TestCase([esc, csi], internal.Continue),
TestCase([esc], internal.Continue),
TestCase([], internal.Continue),
]
list.each(test_cases, fn(tc) {
assert tc.expected == internal.from_list(tc.input)
})
}