Compare commits
3 Commits
d311eedadb
...
section-co
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3df1a50e51 | ||
|
|
26d9985a38 | ||
|
|
7d20bdad53 |
@@ -4,6 +4,7 @@ import musicplayer/input/input
|
|||||||
import musicplayer/input/key.{type Key}
|
import musicplayer/input/key.{type Key}
|
||||||
import musicplayer/mpv/mpv
|
import musicplayer/mpv/mpv
|
||||||
import musicplayer/musicplayer
|
import musicplayer/musicplayer
|
||||||
|
import musicplayer/ui/ui
|
||||||
|
|
||||||
pub fn main() -> Nil {
|
pub fn main() -> Nil {
|
||||||
let input_keys_name: Name(Key) = process.new_name("input_keys")
|
let input_keys_name: Name(Key) = process.new_name("input_keys")
|
||||||
@@ -15,13 +16,16 @@ pub fn main() -> Nil {
|
|||||||
// inject input
|
// inject input
|
||||||
let input_inject_name: Name(Key) = process.new_name("input_inject_keys")
|
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)
|
input.new(input_keys_name, input_stream_name, input_inject_name)
|
||||||
|
|
||||||
|
let assert Ok(ui) = ui.new()
|
||||||
let assert Ok(mpv) = mpv.new()
|
let assert Ok(mpv) = mpv.new()
|
||||||
|
|
||||||
let exit = process.new_subject()
|
let exit = process.new_subject()
|
||||||
let assert Ok(_) =
|
let assert Ok(_) =
|
||||||
musicplayer.new(
|
musicplayer.new(
|
||||||
|
ui,
|
||||||
mpv,
|
mpv,
|
||||||
input_keys_name,
|
input_keys_name,
|
||||||
input_stream_name,
|
input_stream_name,
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import musicplayer/input/key.{type Key}
|
|||||||
|
|
||||||
pub type Control {
|
pub type Control {
|
||||||
TogglePlayPause
|
TogglePlayPause
|
||||||
|
|
||||||
Search
|
Search
|
||||||
|
|
||||||
Exit
|
Exit
|
||||||
@@ -18,8 +17,8 @@ pub fn from_key(key: Key) -> Result(Control, Nil) {
|
|||||||
fn char_control(char: String) -> Result(Control, Nil) {
|
fn char_control(char: String) -> Result(Control, Nil) {
|
||||||
case char {
|
case char {
|
||||||
" " -> Ok(TogglePlayPause)
|
" " -> Ok(TogglePlayPause)
|
||||||
"q" -> Ok(Exit)
|
|
||||||
"/" -> Ok(Search)
|
"/" -> Ok(Search)
|
||||||
|
"q" -> Ok(Exit)
|
||||||
_ -> Error(Nil)
|
_ -> Error(Nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ pub fn new(
|
|||||||
read_input(input_keys, input_stream, input_inject)
|
read_input(input_keys, input_stream, input_inject)
|
||||||
})
|
})
|
||||||
|
|
||||||
echo "waiting for input"
|
|
||||||
key.start_raw_shell()
|
key.start_raw_shell()
|
||||||
Nil
|
Nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import gleam/otp/actor
|
|||||||
import gleam/result
|
import gleam/result
|
||||||
import gleam/string
|
import gleam/string
|
||||||
|
|
||||||
import musicplayer/mpv/control
|
import musicplayer/mpv/control.{type Control}
|
||||||
import musicplayer/tcp/reason
|
import musicplayer/tcp/reason
|
||||||
import musicplayer/tcp/tcp.{type Socket}
|
import musicplayer/tcp/tcp.{type Socket}
|
||||||
|
|
||||||
@@ -11,7 +11,7 @@ type State(socket) {
|
|||||||
State(socket: Socket)
|
State(socket: Socket)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new() -> Result(Subject(control.Control), String) {
|
pub fn new() -> Result(Subject(Control), String) {
|
||||||
// TODO start up mvp here, currently hi-jacking `naviterm`s socket
|
// TODO start up mvp here, currently hi-jacking `naviterm`s socket
|
||||||
let socket_path = "/tmp/naviterm_mpv"
|
let socket_path = "/tmp/naviterm_mpv"
|
||||||
|
|
||||||
@@ -33,8 +33,8 @@ pub fn new() -> Result(Subject(control.Control), String) {
|
|||||||
|
|
||||||
fn handle_message(
|
fn handle_message(
|
||||||
state: State(socket),
|
state: State(socket),
|
||||||
control: control.Control,
|
control: Control,
|
||||||
) -> actor.Next(State(socket), control.Control) {
|
) -> actor.Next(State(socket), Control) {
|
||||||
case control {
|
case control {
|
||||||
control.TogglePlayPause -> {
|
control.TogglePlayPause -> {
|
||||||
echo "mpv: toggling play/pause"
|
echo "mpv: toggling play/pause"
|
||||||
|
|||||||
@@ -7,9 +7,12 @@ import gleam/string
|
|||||||
import musicplayer/control.{type Control}
|
import musicplayer/control.{type Control}
|
||||||
import musicplayer/input/key.{type Key}
|
import musicplayer/input/key.{type Key}
|
||||||
import musicplayer/mpv/control as mpv_control
|
import musicplayer/mpv/control as mpv_control
|
||||||
|
import musicplayer/ui/control as ui_control
|
||||||
|
import musicplayer/ui/section
|
||||||
|
|
||||||
type State(mpv, input_inject, exit) {
|
type State(ui, mpv, input_inject, exit) {
|
||||||
State(
|
State(
|
||||||
|
ui: Subject(ui_control.Control),
|
||||||
mpv: Subject(mpv_control.Control),
|
mpv: Subject(mpv_control.Control),
|
||||||
input_inject: Subject(Key),
|
input_inject: Subject(Key),
|
||||||
exit: Subject(Nil),
|
exit: Subject(Nil),
|
||||||
@@ -17,6 +20,7 @@ type State(mpv, input_inject, exit) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(
|
pub fn new(
|
||||||
|
ui: Subject(ui_control.Control),
|
||||||
mpv: Subject(mpv_control.Control),
|
mpv: Subject(mpv_control.Control),
|
||||||
input_keys_name: Name(Key),
|
input_keys_name: Name(Key),
|
||||||
input_stream_name: Name(List(String)),
|
input_stream_name: Name(List(String)),
|
||||||
@@ -28,7 +32,7 @@ pub fn new(
|
|||||||
let input_inject = process.named_subject(input_inject_name)
|
let input_inject = process.named_subject(input_inject_name)
|
||||||
|
|
||||||
case
|
case
|
||||||
actor.new(State(mpv, input_inject, exit))
|
actor.new(State(ui, mpv, input_inject, exit))
|
||||||
|> actor.on_message(handle_message)
|
|> actor.on_message(handle_message)
|
||||||
|> actor.start
|
|> actor.start
|
||||||
{
|
{
|
||||||
@@ -51,9 +55,9 @@ pub fn new(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn handle_message(
|
fn handle_message(
|
||||||
state: State(mpv, input_inject, exit),
|
state: State(ui, mpv, input_inject, exit),
|
||||||
control: Control,
|
control: Control,
|
||||||
) -> actor.Next(State(mpv, input_inject, exit), Control) {
|
) -> actor.Next(State(ui, mpv, input_inject, exit), Control) {
|
||||||
case control {
|
case control {
|
||||||
control.Search -> {
|
control.Search -> {
|
||||||
process.send(state.input_inject, key.Continue([key.input_introducer]))
|
process.send(state.input_inject, key.Continue([key.input_introducer]))
|
||||||
@@ -70,17 +74,34 @@ fn handle_message(
|
|||||||
mpv_control.GetPlaybackTime(reply_to)
|
mpv_control.GetPlaybackTime(reply_to)
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
Error(err) -> echo "! could not get playbackTime: " <> err.details
|
Error(err) ->
|
||||||
|
process.send(
|
||||||
|
state.ui,
|
||||||
|
ui_control.UpdateState(
|
||||||
|
section.PlaybackTime,
|
||||||
|
"playback time: N/A (err: " <> err.details <> ")",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
Ok(mpv_control.PlaybackTime(data: playback_time)) ->
|
Ok(mpv_control.PlaybackTime(data: playback_time)) ->
|
||||||
echo "playbacktime from mpv: " <> float.to_string(playback_time)
|
process.send(
|
||||||
|
state.ui,
|
||||||
|
ui_control.UpdateState(
|
||||||
|
section.PlaybackTime,
|
||||||
|
"playback time: " <> float.to_string(playback_time),
|
||||||
|
),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
actor.continue(state)
|
actor.continue(state)
|
||||||
}
|
}
|
||||||
control.Exit -> {
|
control.Exit -> {
|
||||||
// Close socket to `mpv`
|
// Close `mpv` socket
|
||||||
process.call(state.mpv, 1000, fn(reply_to) { mpv_control.Exit(reply_to) })
|
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
|
// End main process
|
||||||
process.send(state.exit, Nil)
|
process.send(state.exit, Nil)
|
||||||
actor.stop()
|
actor.stop()
|
||||||
|
|||||||
9
src/musicplayer/ui/control.gleam
Normal file
9
src/musicplayer/ui/control.gleam
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
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))
|
||||||
|
}
|
||||||
19
src/musicplayer/ui/internal.gleam
Normal file
19
src/musicplayer/ui/internal.gleam
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
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")
|
||||||
|
}
|
||||||
13
src/musicplayer/ui/section.gleam
Normal file
13
src/musicplayer/ui/section.gleam
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import gleam/dict
|
||||||
|
|
||||||
|
pub type Section {
|
||||||
|
Header
|
||||||
|
PlaybackTime
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Content {
|
||||||
|
Content(content: String)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type SectionContent =
|
||||||
|
dict.Dict(Section, Content)
|
||||||
74
src/musicplayer/ui/ui.gleam
Normal file
74
src/musicplayer/ui/ui.gleam
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
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