Compare commits
4 Commits
ff60904715
...
ui
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d041b6b3b1 | ||
|
|
b69852f7ba | ||
|
|
3f86b881c3 | ||
|
|
fee776b352 |
@@ -62,7 +62,7 @@ pub fn start_raw_shell() {
|
||||
|
||||
pub fn read_input_until_key(
|
||||
l: List(String),
|
||||
tap_input: Subject(List(String)),
|
||||
input_sink: Subject(List(String)),
|
||||
) -> Key {
|
||||
case
|
||||
internal_input.read_input()
|
||||
@@ -71,9 +71,13 @@ pub fn read_input_until_key(
|
||||
|> from_list
|
||||
{
|
||||
Continue(l) -> {
|
||||
process.send(tap_input, l)
|
||||
read_input_until_key(l, tap_input)
|
||||
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
|
||||
}
|
||||
k -> k
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ import tcp/tcp.{type Socket}
|
||||
pub type Control {
|
||||
TogglePlayPause
|
||||
|
||||
Search
|
||||
|
||||
Exit
|
||||
}
|
||||
|
||||
@@ -27,6 +29,7 @@ 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,17 +8,22 @@ 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, tap_input, exit) {
|
||||
type State(socket, inject_input, ui, exit) {
|
||||
State(
|
||||
socket: Socket,
|
||||
inject_input: Subject(Key),
|
||||
tap_input: Subject(List(String)),
|
||||
ui: Subject(Event),
|
||||
exit: Subject(Nil),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
||||
pub fn new(
|
||||
ui: Subject(Event),
|
||||
input_sink: Subject(List(String)),
|
||||
exit: Subject(Nil),
|
||||
) -> Result(Nil, String) {
|
||||
// TODO start up mvp here, currently hi-jacking `naviterm`s socket
|
||||
let socket_path = "/tmp/naviterm_mpv"
|
||||
|
||||
@@ -31,11 +36,8 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
||||
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, tap_input, exit))
|
||||
actor.new(State(socket, inject_input, ui, exit))
|
||||
|> actor.on_message(handle_message)
|
||||
|> actor.start
|
||||
{
|
||||
@@ -49,7 +51,7 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
||||
let assert Ok(_) =
|
||||
process.register(process.self(), inject_input_name)
|
||||
|
||||
read_input(data, inject_input, tap_input)
|
||||
read_input(data, inject_input, input_sink)
|
||||
})
|
||||
|
||||
Ok(Nil)
|
||||
@@ -60,10 +62,16 @@ pub fn new(exit: Subject(Nil)) -> Result(Nil, String) {
|
||||
}
|
||||
|
||||
fn handle_message(
|
||||
state: State(socket, inject, input_output, exit),
|
||||
state: State(socket, inject, ui, exit),
|
||||
control: Control,
|
||||
) -> actor.Next(State(socket, inject, input_output, exit), Control) {
|
||||
) -> actor.Next(State(socket, inject, ui, 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"
|
||||
|
||||
@@ -94,7 +102,7 @@ fn handle_message(
|
||||
fn read_input(
|
||||
subject: Subject(Control),
|
||||
inject_input: Subject(Key),
|
||||
tap_input: Subject(List(String)),
|
||||
input_sink: Subject(List(String)),
|
||||
) -> Nil {
|
||||
let buffer = case process.receive(inject_input, 1) {
|
||||
Ok(key.Continue(buffer)) -> buffer
|
||||
@@ -102,9 +110,9 @@ fn read_input(
|
||||
}
|
||||
|
||||
let _ =
|
||||
key.read_input_until_key(buffer, tap_input)
|
||||
key.read_input_until_key(buffer, input_sink)
|
||||
|> control.from_key
|
||||
|> result.map(process.send(subject, _))
|
||||
|
||||
read_input(subject, inject_input, tap_input)
|
||||
read_input(subject, inject_input, input_sink)
|
||||
}
|
||||
|
||||
@@ -1,8 +1,30 @@
|
||||
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(_) = mpv.new(exit)
|
||||
|
||||
let assert Ok(#(ui, input_sink)) = ui.new()
|
||||
let assert Ok(_) = mpv.new(ui, input_sink, exit)
|
||||
|
||||
process.receive_forever(exit)
|
||||
}
|
||||
|
||||
62
src/ui/ui.gleam
Normal file
62
src/ui/ui.gleam
Normal file
@@ -0,0 +1,62 @@
|
||||
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