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/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

View File

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