Compare commits
1 Commits
section-co
...
d311eedadb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d311eedadb |
@@ -4,7 +4,6 @@ import musicplayer/input/input
|
||||
import musicplayer/input/key.{type Key}
|
||||
import musicplayer/mpv/mpv
|
||||
import musicplayer/musicplayer
|
||||
import musicplayer/ui/ui
|
||||
|
||||
pub fn main() -> Nil {
|
||||
let input_keys_name: Name(Key) = process.new_name("input_keys")
|
||||
@@ -16,16 +15,13 @@ pub fn main() -> Nil {
|
||||
// inject input
|
||||
let input_inject_name: Name(Key) = process.new_name("input_inject_keys")
|
||||
|
||||
// TODO should input.new just return the inject_subject?
|
||||
input.new(input_keys_name, input_stream_name, input_inject_name)
|
||||
|
||||
let assert Ok(ui) = ui.new()
|
||||
let assert Ok(mpv) = mpv.new()
|
||||
|
||||
let exit = process.new_subject()
|
||||
let assert Ok(_) =
|
||||
musicplayer.new(
|
||||
ui,
|
||||
mpv,
|
||||
input_keys_name,
|
||||
input_stream_name,
|
||||
|
||||
@@ -2,6 +2,7 @@ import musicplayer/input/key.{type Key}
|
||||
|
||||
pub type Control {
|
||||
TogglePlayPause
|
||||
|
||||
Search
|
||||
|
||||
Exit
|
||||
@@ -17,8 +18,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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ pub fn new(
|
||||
read_input(input_keys, input_stream, input_inject)
|
||||
})
|
||||
|
||||
echo "waiting for input"
|
||||
key.start_raw_shell()
|
||||
Nil
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import gleam/otp/actor
|
||||
import gleam/result
|
||||
import gleam/string
|
||||
|
||||
import musicplayer/mpv/control.{type Control}
|
||||
import musicplayer/mpv/control
|
||||
import musicplayer/tcp/reason
|
||||
import musicplayer/tcp/tcp.{type Socket}
|
||||
|
||||
@@ -11,7 +11,7 @@ type State(socket) {
|
||||
State(socket: Socket)
|
||||
}
|
||||
|
||||
pub fn new() -> Result(Subject(Control), String) {
|
||||
pub fn new() -> Result(Subject(control.Control), String) {
|
||||
// TODO start up mvp here, currently hi-jacking `naviterm`s socket
|
||||
let socket_path = "/tmp/naviterm_mpv"
|
||||
|
||||
@@ -33,8 +33,8 @@ pub fn new() -> Result(Subject(Control), String) {
|
||||
|
||||
fn handle_message(
|
||||
state: State(socket),
|
||||
control: Control,
|
||||
) -> actor.Next(State(socket), Control) {
|
||||
control: control.Control,
|
||||
) -> actor.Next(State(socket), control.Control) {
|
||||
case control {
|
||||
control.TogglePlayPause -> {
|
||||
echo "mpv: toggling play/pause"
|
||||
|
||||
@@ -7,12 +7,9 @@ import gleam/string
|
||||
import musicplayer/control.{type Control}
|
||||
import musicplayer/input/key.{type Key}
|
||||
import musicplayer/mpv/control as mpv_control
|
||||
import musicplayer/ui/control as ui_control
|
||||
import musicplayer/ui/section
|
||||
|
||||
type State(ui, mpv, input_inject, exit) {
|
||||
type State(mpv, input_inject, exit) {
|
||||
State(
|
||||
ui: Subject(ui_control.Control),
|
||||
mpv: Subject(mpv_control.Control),
|
||||
input_inject: Subject(Key),
|
||||
exit: Subject(Nil),
|
||||
@@ -20,7 +17,6 @@ type State(ui, mpv, input_inject, exit) {
|
||||
}
|
||||
|
||||
pub fn new(
|
||||
ui: Subject(ui_control.Control),
|
||||
mpv: Subject(mpv_control.Control),
|
||||
input_keys_name: Name(Key),
|
||||
input_stream_name: Name(List(String)),
|
||||
@@ -32,7 +28,7 @@ pub fn new(
|
||||
let input_inject = process.named_subject(input_inject_name)
|
||||
|
||||
case
|
||||
actor.new(State(ui, mpv, input_inject, exit))
|
||||
actor.new(State(mpv, input_inject, exit))
|
||||
|> actor.on_message(handle_message)
|
||||
|> actor.start
|
||||
{
|
||||
@@ -55,9 +51,9 @@ pub fn new(
|
||||
}
|
||||
|
||||
fn handle_message(
|
||||
state: State(ui, mpv, input_inject, exit),
|
||||
state: State(mpv, input_inject, exit),
|
||||
control: Control,
|
||||
) -> actor.Next(State(ui, mpv, input_inject, exit), Control) {
|
||||
) -> actor.Next(State(mpv, input_inject, exit), Control) {
|
||||
case control {
|
||||
control.Search -> {
|
||||
process.send(state.input_inject, key.Continue([key.input_introducer]))
|
||||
@@ -74,34 +70,17 @@ fn handle_message(
|
||||
mpv_control.GetPlaybackTime(reply_to)
|
||||
})
|
||||
{
|
||||
Error(err) ->
|
||||
process.send(
|
||||
state.ui,
|
||||
ui_control.UpdateState(
|
||||
section.PlaybackTime,
|
||||
"playback time: N/A (err: " <> err.details <> ")",
|
||||
),
|
||||
)
|
||||
|
||||
Error(err) -> echo "! could not get playbackTime: " <> err.details
|
||||
Ok(mpv_control.PlaybackTime(data: playback_time)) ->
|
||||
process.send(
|
||||
state.ui,
|
||||
ui_control.UpdateState(
|
||||
section.PlaybackTime,
|
||||
"playback time: " <> float.to_string(playback_time),
|
||||
),
|
||||
)
|
||||
echo "playbacktime from mpv: " <> float.to_string(playback_time)
|
||||
}
|
||||
|
||||
actor.continue(state)
|
||||
}
|
||||
control.Exit -> {
|
||||
// Close `mpv` socket
|
||||
// Close socket to `mpv`
|
||||
process.call(state.mpv, 1000, fn(reply_to) { mpv_control.Exit(reply_to) })
|
||||
|
||||
// Reset terminal state (show cursor etc.)
|
||||
process.call(state.ui, 1000, fn(reply_to) { ui_control.Exit(reply_to) })
|
||||
|
||||
// End main process
|
||||
process.send(state.exit, Nil)
|
||||
actor.stop()
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
import gleam/erlang/process.{type Subject}
|
||||
|
||||
import musicplayer/ui/section.{type Section}
|
||||
|
||||
pub type Control {
|
||||
UpdateState(section: Section, content: String)
|
||||
|
||||
Exit(reply_to: Subject(Nil))
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
import gleam/io
|
||||
|
||||
pub fn print(content: String) -> Nil {
|
||||
io.print(content <> "\n")
|
||||
}
|
||||
|
||||
// https://en.wikipedia.org/wiki/ANSI_escape_code#Control_Sequence_Introducer_commands
|
||||
|
||||
pub fn clear_screen() -> Nil {
|
||||
io.print("\u{001B}[2J\u{001B}[H")
|
||||
}
|
||||
|
||||
pub fn hide_cursor() -> Nil {
|
||||
io.print("\u{001B}[?25l")
|
||||
}
|
||||
|
||||
pub fn show_cursor() -> Nil {
|
||||
io.print("\u{001B}[?25h")
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import gleam/dict
|
||||
|
||||
pub type Section {
|
||||
Header
|
||||
PlaybackTime
|
||||
}
|
||||
|
||||
pub type Content {
|
||||
Content(content: String)
|
||||
}
|
||||
|
||||
pub type SectionContent =
|
||||
dict.Dict(Section, Content)
|
||||
@@ -1,74 +0,0 @@
|
||||
import gleam/dict
|
||||
import gleam/erlang/process.{type Subject}
|
||||
import gleam/otp/actor
|
||||
import gleam/string
|
||||
|
||||
import musicplayer/ui/control.{type Control}
|
||||
import musicplayer/ui/internal as ui_internal
|
||||
import musicplayer/ui/section.{type Section, type SectionContent, Content}
|
||||
|
||||
pub type State(redraw, content) {
|
||||
State(redraw: Subject(SectionContent), sections: SectionContent)
|
||||
}
|
||||
|
||||
pub fn new() -> Result(Subject(Control), String) {
|
||||
let redraw_name = process.new_name("redraw")
|
||||
let redraw: Subject(String) = process.named_subject(redraw_name)
|
||||
|
||||
let sections =
|
||||
dict.from_list([
|
||||
#(section.Header, Content("musicplayer:")),
|
||||
#(section.PlaybackTime, Content("")),
|
||||
])
|
||||
|
||||
case
|
||||
actor.new(State(redraw, sections))
|
||||
|> actor.on_message(handle_message)
|
||||
|> actor.start
|
||||
{
|
||||
Error(start_error) ->
|
||||
Error("Could not start actor: " <> string.inspect(start_error))
|
||||
Ok(actor.Started(data: ui, ..)) -> {
|
||||
process.spawn(fn() {
|
||||
let assert Ok(_) = process.register(process.self(), redraw_name)
|
||||
|
||||
ui_internal.clear_screen()
|
||||
ui_internal.hide_cursor()
|
||||
|
||||
redraw_loop(redraw)
|
||||
})
|
||||
|
||||
Ok(ui)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_message(
|
||||
state: State(redraw, content),
|
||||
control: Control,
|
||||
) -> actor.Next(State(redraw, content), Control) {
|
||||
case control {
|
||||
control.UpdateState(section, content) -> {
|
||||
let content = dict.insert(state.content, section, content)
|
||||
let state = State(..state, content:)
|
||||
|
||||
actor.send(state.redraw, content)
|
||||
actor.continue(state)
|
||||
}
|
||||
|
||||
control.Exit(reply_to) -> {
|
||||
ui_internal.show_cursor()
|
||||
process.send(reply_to, Nil)
|
||||
actor.stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn redraw_loop(redraw: Subject(SectionContent)) -> Nil {
|
||||
let content = process.receive_forever(redraw)
|
||||
|
||||
ui_internal.clear_screen()
|
||||
ui_internal.print(content)
|
||||
|
||||
redraw_loop(redraw)
|
||||
}
|
||||
Reference in New Issue
Block a user