Compare commits
3 Commits
ui
...
efe14200fe
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
efe14200fe | ||
|
|
efc0604c1d | ||
|
|
a048e084e7 |
@@ -1,7 +1,5 @@
|
||||
import gleam/erlang/atom
|
||||
import gleam/erlang/process.{type Subject}
|
||||
import gleam/list
|
||||
import gleam/string
|
||||
|
||||
import input/internal as internal_input
|
||||
|
||||
@@ -24,7 +22,7 @@ pub const esc = "\u{001B}"
|
||||
pub const csi = "["
|
||||
|
||||
// input introducer
|
||||
pub const input_introducer = "::"
|
||||
const input_introducer = "$"
|
||||
|
||||
pub fn from_list(l: List(String)) -> Key {
|
||||
case l {
|
||||
@@ -37,12 +35,9 @@ pub fn from_list(l: List(String)) -> Key {
|
||||
|
||||
[ci] | [ci, _] if ci == input_introducer -> Continue(l)
|
||||
[ii, cmd, tail] if ii == input_introducer -> {
|
||||
case tail {
|
||||
// Return
|
||||
"\r" -> Input(cmd)
|
||||
// Backspace
|
||||
"\u{007F}" -> Continue([ii, string.drop_end(cmd, 1)])
|
||||
_ -> Continue([ii, cmd <> tail])
|
||||
case tail == "\r" {
|
||||
True -> Input(cmd)
|
||||
False -> Continue([ii, cmd <> tail])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,24 +55,14 @@ pub fn start_raw_shell() {
|
||||
internal_input.shell_start_interactive(#(no_shell, raw))
|
||||
}
|
||||
|
||||
pub fn read_input_until_key(
|
||||
l: List(String),
|
||||
input_sink: Subject(List(String)),
|
||||
) -> Key {
|
||||
pub fn read_input_until_key(l: List(String)) -> Key {
|
||||
case
|
||||
internal_input.read_input()
|
||||
|> list.wrap
|
||||
|> list.append(l, _)
|
||||
|> from_list
|
||||
{
|
||||
Continue(l) -> {
|
||||
echo "key:read_input_until_key continue: " <> string.inspect(l)
|
||||
process.send(input_sink, l)
|
||||
read_input_until_key(l, input_sink)
|
||||
}
|
||||
k -> {
|
||||
echo "key:read_input_until_key k: " <> string.inspect(k)
|
||||
k
|
||||
}
|
||||
Continue(l) -> read_input_until_key(l)
|
||||
k -> k
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ pub type ControlError {
|
||||
}
|
||||
|
||||
pub fn from_key(key: Key) -> Result(Control, Nil) {
|
||||
echo key
|
||||
case key {
|
||||
key.Char(char) -> char_control(char)
|
||||
_ -> Error(Nil)
|
||||
@@ -29,8 +30,8 @@ pub fn from_key(key: Key) -> Result(Control, Nil) {
|
||||
fn char_control(char: String) -> Result(Control, Nil) {
|
||||
case char {
|
||||
" " -> Ok(TogglePlayPause)
|
||||
"/" -> Ok(Search)
|
||||
"q" -> Ok(Exit)
|
||||
"/" -> Ok(Search)
|
||||
_ -> Error(Nil)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,40 +4,24 @@ import gleam/otp/actor
|
||||
import gleam/result
|
||||
import gleam/string
|
||||
|
||||
import input/key.{type Key}
|
||||
import input/key
|
||||
import mpv/control.{type Control}
|
||||
import tcp/reason
|
||||
import tcp/tcp.{type Socket}
|
||||
import ui/ui.{type Event}
|
||||
|
||||
type State(socket, inject_input, ui, exit) {
|
||||
State(
|
||||
socket: Socket,
|
||||
inject_input: Subject(Key),
|
||||
ui: Subject(Event),
|
||||
exit: Subject(Nil),
|
||||
)
|
||||
type State(socket, exit) {
|
||||
State(socket: Socket, exit: Subject(Nil))
|
||||
}
|
||||
|
||||
pub fn new(
|
||||
ui: Subject(Event),
|
||||
input_sink: Subject(List(String)),
|
||||
exit: Subject(Nil),
|
||||
) -> Result(Nil, String) {
|
||||
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) -> {
|
||||
// `inject_input` is created by name to allow the process that
|
||||
// owns `read_input` to be able to register it, while the agent
|
||||
// also have a reference to it to be able to inject input
|
||||
let inject_input_name = process.new_name("inject_input")
|
||||
let inject_input = process.named_subject(inject_input_name)
|
||||
|
||||
case
|
||||
actor.new(State(socket, inject_input, ui, exit))
|
||||
actor.new(State(socket, exit))
|
||||
|> actor.on_message(handle_message)
|
||||
|> actor.start
|
||||
{
|
||||
@@ -46,14 +30,12 @@ pub fn new(
|
||||
Ok(actor.Started(data:, ..)) -> {
|
||||
echo "waiting for input"
|
||||
key.start_raw_shell()
|
||||
let input = process.new_subject()
|
||||
process.self()
|
||||
process.receive
|
||||
|
||||
process.spawn(fn() {
|
||||
let assert Ok(_) =
|
||||
process.register(process.self(), inject_input_name)
|
||||
|
||||
read_input(data, inject_input, input_sink)
|
||||
})
|
||||
|
||||
process.spawn(fn() { input_handler(input, data) })
|
||||
process.spawn(fn() { read_input(data) })
|
||||
Ok(Nil)
|
||||
}
|
||||
}
|
||||
@@ -62,16 +44,11 @@ pub fn new(
|
||||
}
|
||||
|
||||
fn handle_message(
|
||||
state: State(socket, inject, ui, exit),
|
||||
state: State(socket, exit),
|
||||
control: Control,
|
||||
) -> actor.Next(State(socket, inject, ui, exit), Control) {
|
||||
) -> actor.Next(State(socket, exit), Control) {
|
||||
case control {
|
||||
control.Search -> {
|
||||
echo "mpv search"
|
||||
process.send(state.inject_input, key.Continue([key.input_introducer]))
|
||||
process.send(state.ui, ui.Search)
|
||||
actor.continue(state)
|
||||
}
|
||||
control.Search -> todo
|
||||
control.TogglePlayPause -> {
|
||||
echo "toggling play/pause"
|
||||
|
||||
@@ -94,25 +71,32 @@ fn handle_message(
|
||||
}
|
||||
}
|
||||
|
||||
/// `read_input` operates by reading from input until a `Key` can be created.
|
||||
/// It is possible to create a `Key` without the users input by sending
|
||||
/// messages to `inject_input` which will initialize the "input to key" sequence.
|
||||
/// This is useful to ultimately create a `Control` without the user having to
|
||||
/// input all of the character(s) needed.
|
||||
fn read_input(
|
||||
subject: Subject(Control),
|
||||
inject_input: Subject(Key),
|
||||
input_sink: Subject(List(String)),
|
||||
) -> Nil {
|
||||
let buffer = case process.receive(inject_input, 1) {
|
||||
Ok(key.Continue(buffer)) -> buffer
|
||||
Ok(_) | Error(_) -> []
|
||||
}
|
||||
// TODO create new input_handler_loop that recieves on a subject and
|
||||
// TODO read_input reads until a key is found, and then sends it to
|
||||
|
||||
let _ =
|
||||
key.read_input_until_key(buffer, input_sink)
|
||||
// TODO a function that can recieve on a subject, and it can receive two types of messages:
|
||||
// 1. a Control message from read_input
|
||||
// 2. an injected controlMessage with pre-defined buffer
|
||||
// it then forwards the message to the actor subject
|
||||
|
||||
fn read_input(input_handler: Subject(Control)) -> Nil {
|
||||
case
|
||||
key.read_input_until_key([])
|
||||
|> control.from_key
|
||||
|> result.map(process.send(subject, _))
|
||||
|
||||
read_input(subject, inject_input, input_sink)
|
||||
{
|
||||
Error(_) -> Nil
|
||||
Ok(control) -> process.send(input_handler, control)
|
||||
}
|
||||
|
||||
read_input(input_handler)
|
||||
}
|
||||
|
||||
fn input_handler(
|
||||
input: Subject(Control),
|
||||
actor_subject: Subject(Control),
|
||||
) -> Nil {
|
||||
case process.receive(input, 1000) {
|
||||
Error(_) -> Nil
|
||||
Ok(control) -> process.send(actor_subject, control)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,8 @@
|
||||
import gleam/erlang/process
|
||||
|
||||
import mpv/mpv
|
||||
import ui/ui
|
||||
|
||||
pub fn main() -> Nil {
|
||||
// 1. user starts search
|
||||
// ui should show "input: "
|
||||
// user presses enter
|
||||
// ui should show "input was: x"
|
||||
|
||||
// 1. listen for control.Search
|
||||
// 2. start listening to tap_input and print io.print "input: "
|
||||
// 3. simultaniously listen for control.Input
|
||||
// 4. print "input was: x"
|
||||
|
||||
// ui need: tap_input
|
||||
// input need: ui subject to send control events from
|
||||
|
||||
// new input process should return `Key`
|
||||
// this key should be sent both to ui and mpv, and they will decide if they can act on it.
|
||||
// its probably ok if both act on it, e.g. "TogglePlayPause" could stop mpv music and display |> or || in ui
|
||||
|
||||
let exit = process.new_subject()
|
||||
|
||||
let assert Ok(#(ui, input_sink)) = ui.new()
|
||||
let assert Ok(_) = mpv.new(ui, input_sink, exit)
|
||||
|
||||
let assert Ok(_) = mpv.new(exit)
|
||||
process.receive_forever(exit)
|
||||
}
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
import gleam/erlang/process.{type Name, type Subject}
|
||||
import gleam/otp/actor
|
||||
import gleam/string
|
||||
|
||||
pub type Event {
|
||||
Search
|
||||
Input(List(String))
|
||||
}
|
||||
|
||||
// TODO in input, split input into events and control?
|
||||
|
||||
pub fn new() -> Result(#(Subject(Event), Subject(List(String))), String) {
|
||||
let input_sink_name: Name(List(String)) = process.new_name("input_sink")
|
||||
let input_sink = process.named_subject(input_sink_name)
|
||||
|
||||
// let input_sink = process.new_subject()
|
||||
|
||||
case
|
||||
actor.new(Nil)
|
||||
|> actor.on_message(handle_message)
|
||||
|> actor.start
|
||||
{
|
||||
Error(start_error) ->
|
||||
Error("Could not start ui actor: " <> string.inspect(start_error))
|
||||
Ok(actor.Started(data: ui, ..)) -> {
|
||||
echo "ui started"
|
||||
|
||||
// let assert Ok(_) = process.register(process.self(), input_sink_name)
|
||||
process.spawn(fn() {
|
||||
let assert Ok(_) = process.register(process.self(), input_sink_name)
|
||||
drain_input_sink(ui, input_sink)
|
||||
})
|
||||
|
||||
Ok(#(ui, input_sink))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_message(state: Nil, event: Event) -> actor.Next(Nil, Event) {
|
||||
case event {
|
||||
Search -> {
|
||||
echo "ui:search"
|
||||
actor.continue(state)
|
||||
}
|
||||
Input(content) -> {
|
||||
echo "ui:input: " <> string.inspect(content)
|
||||
actor.continue(state)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn drain_input_sink(
|
||||
subject: Subject(Event),
|
||||
input_sink: Subject(List(String)),
|
||||
) -> Nil {
|
||||
echo "ui:drain_input_sink"
|
||||
let content = process.receive_forever(input_sink)
|
||||
|
||||
process.send(subject, Input(content))
|
||||
|
||||
drain_input_sink(subject, input_sink)
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import gleam/list
|
||||
import gleeunit
|
||||
|
||||
import input/key.{type Key, Char, csi, esc, input_introducer as ii}
|
||||
import input/key.{type Key, Char, csi, esc}
|
||||
|
||||
pub fn main() -> Nil {
|
||||
gleeunit.main()
|
||||
@@ -27,11 +27,10 @@ pub fn key_from_list_test() {
|
||||
]
|
||||
|
||||
let input_tests = [
|
||||
TestCase([ii], key.Continue([ii])),
|
||||
TestCase([ii, "a"], key.Continue([ii, "a"])),
|
||||
TestCase([ii, "a", "b"], key.Continue([ii, "ab"])),
|
||||
TestCase([ii, "ab", "\u{007F}"], key.Continue([ii, "a"])),
|
||||
TestCase([ii, "ab", "\r"], key.Input("ab")),
|
||||
TestCase(["$"], key.Continue(["$"])),
|
||||
TestCase(["$", "a"], key.Continue(["$", "a"])),
|
||||
TestCase(["$", "a", "b"], key.Continue(["$", "ab"])),
|
||||
TestCase(["$", "ab", "\r"], key.Input("ab")),
|
||||
]
|
||||
|
||||
let test_cases = [base_tests, char_tests, escape_tests, input_tests]
|
||||
|
||||
Reference in New Issue
Block a user