This commit is contained in:
Alexander Heldt
2025-11-16 17:45:28 +01:00
parent 417b5a2559
commit 64ec278767
4 changed files with 39 additions and 15 deletions

View File

@@ -1,17 +1,17 @@
import gleam/erlang/atom
import gleam/list
import mpv/internal/key as internal_key
pub type Key {
Char(String)
Input(String)
Left
Right
Up
Down
Continue
Continue(buffer: List(String))
Unknown
}
@@ -20,6 +20,9 @@ pub const esc = "\u{001B}"
// control sequence introducer
pub const csi = "["
// input introducer
const input_introducer = "$"
pub fn from_list(l: List(String)) -> Key {
case l {
[e, c, "D"] if e == esc && c == csi -> Left
@@ -27,12 +30,20 @@ pub fn from_list(l: List(String)) -> Key {
[e, c, "A"] if e == esc && c == csi -> Up
[e, c, "B"] if e == esc && c == csi -> Down
[e, c] if e == esc && c == csi -> Continue
[e, c] if e == esc && c == csi -> Continue(l)
[e] if e == esc -> Continue
[ci] | [ci, _] if ci == input_introducer -> Continue(l)
[ii, cmd, tail] if ii == input_introducer -> {
case tail == "\r" {
True -> Input(cmd)
False -> Continue([ii, cmd <> tail])
}
}
[e] if e == esc -> Continue(l)
[char] -> Char(char)
[] -> Continue
[] -> Continue([])
_ -> Unknown
}
}
@@ -44,10 +55,13 @@ pub fn start_raw_shell() {
}
pub fn read_input_until_key(l: List(String)) -> Key {
let l = internal_key.read_input() |> list.wrap |> list.append(l, _)
case from_list(l) {
Continue -> read_input_until_key(l)
case
internal_key.read_input()
|> list.wrap
|> list.append(l, _)
|> from_list
{
Continue(l) -> read_input_until_key(l)
k -> k
}
}