Files
musicplayer/src/mpv/key.gleam
Alexander Heldt 64ec278767 wip
2025-11-16 20:22:56 +01:00

68 lines
1.3 KiB
Gleam

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(buffer: List(String))
Unknown
}
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
[e, c, "C"] if e == esc && c == csi -> Right
[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(l)
[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([])
_ -> Unknown
}
}
pub fn start_raw_shell() {
let no_shell = atom.create("noshell")
let raw = atom.create("raw")
internal_key.shell_start_interactive(#(no_shell, raw))
}
pub fn read_input_until_key(l: List(String)) -> Key {
case
internal_key.read_input()
|> list.wrap
|> list.append(l, _)
|> from_list
{
Continue(l) -> read_input_until_key(l)
k -> k
}
}