Restructure mpv/internal package

This commit is contained in:
Alexander Heldt
2025-11-16 16:04:06 +01:00
parent ebdba09bc2
commit 702313eac2
5 changed files with 51 additions and 48 deletions

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,29 +40,15 @@ 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))
} }
// TODO map key to something like Control, to not leak `Continue` etc. // TODO map key to something like Control, to not leak `Continue` etc.
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

@@ -2,7 +2,7 @@ import gleam/erlang/process.{type Subject}
import gleam/otp/actor import gleam/otp/actor
import gleam/string import gleam/string
import mpv/internal.{type Key, Char} 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}
@@ -30,7 +30,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)
} }
@@ -60,7 +60,7 @@ fn handle_message(
} }
fn read_input(subject: Subject(Message)) -> Nil { 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) read_input(subject)
} }

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)
})
}