Add ability to listen to input
This commit is contained in:
67
src/mpv/internal.gleam
Normal file
67
src/mpv/internal.gleam
Normal file
@@ -0,0 +1,67 @@
|
||||
import gleam/erlang/atom
|
||||
import gleam/list
|
||||
|
||||
pub type Key {
|
||||
Char(String)
|
||||
|
||||
Left
|
||||
Right
|
||||
Up
|
||||
Down
|
||||
|
||||
Continue
|
||||
Empty
|
||||
Unknown
|
||||
}
|
||||
|
||||
pub const esc = "\u{001B}"
|
||||
|
||||
// control sequence introducer
|
||||
pub const csi = "["
|
||||
|
||||
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
|
||||
|
||||
[e] if e == esc -> Continue
|
||||
[char] -> Char(char)
|
||||
|
||||
[] -> Continue
|
||||
_ -> Unknown
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_raw_shell() {
|
||||
let no_shell = atom.create("noshell")
|
||||
let raw = atom.create("raw")
|
||||
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, _)
|
||||
|
||||
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
|
||||
66
src/mpv/mpv.gleam
Normal file
66
src/mpv/mpv.gleam
Normal file
@@ -0,0 +1,66 @@
|
||||
import gleam/erlang/process.{type Subject}
|
||||
import gleam/otp/actor
|
||||
import gleam/string
|
||||
|
||||
import mpv/internal.{type Key, Char}
|
||||
import tcp/reason.{type Reason}
|
||||
import tcp/tcp.{type Socket}
|
||||
|
||||
pub type Message {
|
||||
KeyPress(Key)
|
||||
}
|
||||
|
||||
type State(socket, exit) {
|
||||
State(socket: Socket, exit: Subject(Nil))
|
||||
}
|
||||
|
||||
pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
||||
// TODO start up mvp here, currently hi-jacking `naviterm`s socket
|
||||
let socket_path = "/tmp/naviterm_mpv"
|
||||
|
||||
case tcp.connect(socket_path) {
|
||||
Error(r) -> Error("Could not connect to mpv: " <> reason.to_string(r))
|
||||
Ok(socket) -> {
|
||||
case
|
||||
actor.new(State(socket, exit))
|
||||
|> actor.on_message(handle_message)
|
||||
|> actor.start
|
||||
{
|
||||
Error(start_error) ->
|
||||
Error("Could not start actor: " <> string.inspect(start_error))
|
||||
Ok(actor.Started(data:, ..)) -> {
|
||||
echo "waiting for input"
|
||||
internal.start_raw_shell()
|
||||
process.spawn(fn() { read_input(data) })
|
||||
Ok(Nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn toggle_play_pause(socket: Socket) -> Result(Nil, Reason) {
|
||||
tcp.send(socket, "{\"command\":[\"cycle\",\"pause\"]}\n")
|
||||
}
|
||||
|
||||
fn handle_message(
|
||||
state: State(socket, exit),
|
||||
message: Message,
|
||||
) -> actor.Next(State(socket, exit), Message) {
|
||||
case message {
|
||||
KeyPress(Char("q")) -> {
|
||||
process.send(state.exit, Nil)
|
||||
actor.stop()
|
||||
}
|
||||
KeyPress(key) -> {
|
||||
echo "key: " <> string.inspect(key)
|
||||
actor.continue(state)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_input(subject: Subject(Message)) -> Nil {
|
||||
internal.read_input_until_key([]) |> KeyPress |> process.send(subject, _)
|
||||
|
||||
read_input(subject)
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
import gleam/io
|
||||
import gleam/erlang/process
|
||||
import mpv/mpv
|
||||
|
||||
pub fn main() -> Nil {
|
||||
io.println("musicplayer")
|
||||
let exit = process.new_subject()
|
||||
let assert Ok(_) = mpv.new(exit)
|
||||
process.receive_forever(exit)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user