Compare commits
3 Commits
ui
...
ff60904715
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ff60904715 | ||
|
|
4602669c6f | ||
|
|
02d8aa5396 |
@@ -62,7 +62,7 @@ pub fn start_raw_shell() {
|
||||
|
||||
pub fn read_input_until_key(
|
||||
l: List(String),
|
||||
input_sink: Subject(List(String)),
|
||||
tap_input: Subject(List(String)),
|
||||
) -> Key {
|
||||
case
|
||||
internal_input.read_input()
|
||||
@@ -71,13 +71,9 @@ pub fn read_input_until_key(
|
||||
|> 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
|
||||
process.send(tap_input, l)
|
||||
read_input_until_key(l, tap_input)
|
||||
}
|
||||
k -> k
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,6 @@ import tcp/tcp.{type Socket}
|
||||
pub type Control {
|
||||
TogglePlayPause
|
||||
|
||||
Search
|
||||
|
||||
Exit
|
||||
}
|
||||
|
||||
@@ -29,7 +27,6 @@ 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)
|
||||
_ -> Error(Nil)
|
||||
}
|
||||
|
||||
@@ -8,22 +8,17 @@ import input/key.{type 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) {
|
||||
type State(socket, inject_input, tap_input, exit) {
|
||||
State(
|
||||
socket: Socket,
|
||||
inject_input: Subject(Key),
|
||||
ui: Subject(Event),
|
||||
tap_input: Subject(List(String)),
|
||||
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"
|
||||
|
||||
@@ -36,8 +31,11 @@ pub fn new(
|
||||
let inject_input_name = process.new_name("inject_input")
|
||||
let inject_input = process.named_subject(inject_input_name)
|
||||
|
||||
let tap_input_name = process.new_name("tap_input")
|
||||
let tap_input = process.named_subject(tap_input_name)
|
||||
|
||||
case
|
||||
actor.new(State(socket, inject_input, ui, exit))
|
||||
actor.new(State(socket, inject_input, tap_input, exit))
|
||||
|> actor.on_message(handle_message)
|
||||
|> actor.start
|
||||
{
|
||||
@@ -51,7 +49,7 @@ pub fn new(
|
||||
let assert Ok(_) =
|
||||
process.register(process.self(), inject_input_name)
|
||||
|
||||
read_input(data, inject_input, input_sink)
|
||||
read_input(data, inject_input, tap_input)
|
||||
})
|
||||
|
||||
Ok(Nil)
|
||||
@@ -62,16 +60,10 @@ pub fn new(
|
||||
}
|
||||
|
||||
fn handle_message(
|
||||
state: State(socket, inject, ui, exit),
|
||||
state: State(socket, inject, input_output, exit),
|
||||
control: Control,
|
||||
) -> actor.Next(State(socket, inject, ui, exit), Control) {
|
||||
) -> actor.Next(State(socket, inject, input_output, 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.TogglePlayPause -> {
|
||||
echo "toggling play/pause"
|
||||
|
||||
@@ -102,7 +94,7 @@ fn handle_message(
|
||||
fn read_input(
|
||||
subject: Subject(Control),
|
||||
inject_input: Subject(Key),
|
||||
input_sink: Subject(List(String)),
|
||||
tap_input: Subject(List(String)),
|
||||
) -> Nil {
|
||||
let buffer = case process.receive(inject_input, 1) {
|
||||
Ok(key.Continue(buffer)) -> buffer
|
||||
@@ -110,9 +102,9 @@ fn read_input(
|
||||
}
|
||||
|
||||
let _ =
|
||||
key.read_input_until_key(buffer, input_sink)
|
||||
key.read_input_until_key(buffer, tap_input)
|
||||
|> control.from_key
|
||||
|> result.map(process.send(subject, _))
|
||||
|
||||
read_input(subject, inject_input, input_sink)
|
||||
read_input(subject, inject_input, tap_input)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user