From 702313eac25a2675fe3e095ab656ad9899e48cc0 Mon Sep 17 00:00:00 2001 From: Alexander Heldt Date: Sun, 16 Nov 2025 16:04:06 +0100 Subject: [PATCH] Restructure `mpv/internal` package --- src/mpv/internal/key.gleam | 15 ++++++++++++++ src/mpv/{internal.gleam => key.gleam} | 20 ++++-------------- src/mpv/mpv.gleam | 6 +++--- test/mpv/key_test.gleam | 29 +++++++++++++++++++++++++++ test/mpv/mpv_test.gleam | 29 --------------------------- 5 files changed, 51 insertions(+), 48 deletions(-) create mode 100644 src/mpv/internal/key.gleam rename src/mpv/{internal.gleam => key.gleam} (63%) create mode 100644 test/mpv/key_test.gleam delete mode 100644 test/mpv/mpv_test.gleam diff --git a/src/mpv/internal/key.gleam b/src/mpv/internal/key.gleam new file mode 100644 index 0000000..bb8fbc1 --- /dev/null +++ b/src/mpv/internal/key.gleam @@ -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 diff --git a/src/mpv/internal.gleam b/src/mpv/key.gleam similarity index 63% rename from src/mpv/internal.gleam rename to src/mpv/key.gleam index 36c3ae9..1f40752 100644 --- a/src/mpv/internal.gleam +++ b/src/mpv/key.gleam @@ -1,6 +1,8 @@ import gleam/erlang/atom import gleam/list +import mpv/internal/key as internal_key + pub type Key { Char(String) @@ -38,29 +40,15 @@ pub fn from_list(l: List(String)) -> Key { pub fn start_raw_shell() { let no_shell = atom.create("noshell") let raw = atom.create("raw") - shell_start_interactive(#(no_shell, raw)) + internal_key.shell_start_interactive(#(no_shell, raw)) } // TODO map key to something like Control, to not leak `Continue` etc. 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) { Continue -> read_input_until_key(l) 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 diff --git a/src/mpv/mpv.gleam b/src/mpv/mpv.gleam index 403f195..cb78784 100644 --- a/src/mpv/mpv.gleam +++ b/src/mpv/mpv.gleam @@ -2,7 +2,7 @@ import gleam/erlang/process.{type Subject} import gleam/otp/actor import gleam/string -import mpv/internal.{type Key, Char} +import mpv/key.{type Key, Char} import tcp/reason.{type Reason} import tcp/tcp.{type Socket} @@ -30,7 +30,7 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) { Error("Could not start actor: " <> string.inspect(start_error)) Ok(actor.Started(data:, ..)) -> { echo "waiting for input" - internal.start_raw_shell() + key.start_raw_shell() process.spawn(fn() { read_input(data) }) Ok(Nil) } @@ -60,7 +60,7 @@ fn handle_message( } fn read_input(subject: Subject(Message)) -> Nil { - internal.read_input_until_key([]) |> KeyPress |> process.send(subject, _) + key.read_input_until_key([]) |> KeyPress |> process.send(subject, _) read_input(subject) } diff --git a/test/mpv/key_test.gleam b/test/mpv/key_test.gleam new file mode 100644 index 0000000..c291d88 --- /dev/null +++ b/test/mpv/key_test.gleam @@ -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) + }) +} diff --git a/test/mpv/mpv_test.gleam b/test/mpv/mpv_test.gleam deleted file mode 100644 index c46fbf6..0000000 --- a/test/mpv/mpv_test.gleam +++ /dev/null @@ -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) - }) -}