wip-forward-control

This commit is contained in:
Alexander Heldt
2025-12-21 17:35:28 +01:00
parent 9d54495a4b
commit 6bdd0c4ef9
6 changed files with 60 additions and 28 deletions

View File

@@ -1,6 +1,13 @@
import musicplayer/input/key.{type Key} import musicplayer/input/key.{type Key}
pub type Mode {
Idle
Searching
}
pub type Control { pub type Control {
InputKey(Key)
TogglePlayPause TogglePlayPause
Search Search
@@ -11,16 +18,23 @@ pub type Control {
Exit Exit
} }
pub fn from_key(key: Key) -> Result(Control, Nil) { pub fn from_key(key: Key, mode: Mode) -> Result(Control, Nil) {
case mode {
Idle -> idle_from_key(key)
Searching -> todo
}
}
pub fn idle_from_key(key: Key) -> Result(Control, Nil) {
case key { case key {
key.Return -> Ok(Return) key.Return -> Ok(Return)
key.Backspace -> Ok(Backspace) key.Backspace -> Ok(Backspace)
key.Char(char) -> Ok(char_control(char)) key.Char(char) -> Ok(idle_char_control(char))
_ -> Error(Nil) _ -> Error(Nil)
} }
} }
fn char_control(char: String) -> Control { fn idle_char_control(char: String) -> Control {
case char { case char {
" " -> TogglePlayPause " " -> TogglePlayPause
"/" -> Search "/" -> Search

View File

@@ -1,9 +1,8 @@
import gleam/erlang/process.{type Name, type Pid, type Subject} import gleam/erlang/process.{type Name, type Pid, type Subject}
import gleam/otp/actor import gleam/otp/actor
import gleam/result
import gleam/string import gleam/string
import musicplayer/control.{type Control} import musicplayer/control.{type Control, type Mode}
import musicplayer/input/key.{type Key} import musicplayer/input/key.{type Key}
import musicplayer/logging/logging import musicplayer/logging/logging
import musicplayer/mpv/control as mpv_control import musicplayer/mpv/control as mpv_control
@@ -11,11 +10,6 @@ import musicplayer/time/time
import musicplayer/ui/control as ui_control import musicplayer/ui/control as ui_control
import musicplayer/ui/layout import musicplayer/ui/layout
type Mode {
Idle
Searching
}
type Input { type Input {
Input(capturing: Bool, content: String) Input(capturing: Bool, content: String)
} }
@@ -39,7 +33,7 @@ pub fn new(
let input = Input(False, "") let input = Input(False, "")
case case
actor.new(State(Idle, input, ui, mpv)) actor.new(State(control.Idle, input, ui, mpv))
|> actor.on_message(handle_message) |> actor.on_message(handle_message)
|> actor.start |> actor.start
{ {
@@ -61,6 +55,12 @@ pub fn new(
fn handle_message(state: State, control: Control) -> actor.Next(State, Control) { fn handle_message(state: State, control: Control) -> actor.Next(State, Control) {
case control { case control {
control.InputKey(key) -> {
let _ = control.from_key(key, state.mode)
// TODO get control and send it back to self
actor.continue(state)
}
control.Search -> { control.Search -> {
logging.log("musicplayer - initiating search") logging.log("musicplayer - initiating search")
@@ -69,7 +69,7 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
actor.continue( actor.continue(
State( State(
..state, ..state,
mode: Searching, mode: control.Searching,
input: Input(..state.input, capturing: True), input: Input(..state.input, capturing: True),
), ),
) )
@@ -79,8 +79,12 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
logging.log("musicplayer - recieved raw input: " <> content) logging.log("musicplayer - recieved raw input: " <> content)
let content = case state.mode { let content = case state.mode {
Idle -> state.input.content control.Idle ->
Searching -> { case content {
"1" -> state.input.content
_ -> state.input.content
}
control.Searching -> {
let updated = state.input.content <> content let updated = state.input.content <> content
update_search(state.ui, "searching: " <> updated) update_search(state.ui, "searching: " <> updated)
updated updated
@@ -90,11 +94,13 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
actor.continue(State(..state, input: Input(..state.input, content:))) actor.continue(State(..state, input: Input(..state.input, content:)))
} }
control.Backspace -> { control.Backspace -> {
logging.log("musicplayer - recieved backspace") logging.log(
"musicplayer - recieved backspace. mode:" <> string.inspect(state.mode),
)
let content = case state.mode { let content = case state.mode {
Idle -> state.input.content control.Idle -> state.input.content
Searching -> { control.Searching -> {
let updated = string.drop_end(state.input.content, 1) let updated = string.drop_end(state.input.content, 1)
update_search(state.ui, "searching: " <> updated) update_search(state.ui, "searching: " <> updated)
updated updated
@@ -113,12 +119,16 @@ fn handle_message(state: State, control: Control) -> actor.Next(State, Control)
// Note: state.input.content is now the final input, use it // Note: state.input.content is now the final input, use it
// before it is reset // before it is reset
case state.mode { case state.mode {
Idle -> Nil control.Idle -> Nil
Searching -> update_search(state.ui, "") control.Searching -> update_search(state.ui, "")
} }
actor.continue( actor.continue(
State(..state, mode: Idle, input: Input(capturing: False, content: "")), State(
..state,
mode: control.Idle,
input: Input(capturing: False, content: ""),
),
) )
} }
@@ -195,8 +205,8 @@ fn handle_key(musicplayer: Subject(Control), input_keys: Subject(Key)) -> Nil {
process.new_selector() process.new_selector()
|> process.select(input_keys) |> process.select(input_keys)
|> process.selector_receive_forever |> process.selector_receive_forever
|> control.from_key |> fn(key) { control.InputKey(key) }
|> result.map(process.send(musicplayer, _)) |> process.send(musicplayer, _)
handle_key(musicplayer, input_keys) handle_key(musicplayer, input_keys)
} }

View File

@@ -5,6 +5,8 @@ import musicplayer/ui/layout.{type Section}
pub type Control { pub type Control {
UpdateDimensions(columns: Int, rows: Int) UpdateDimensions(columns: Int, rows: Int)
UpdateState(section: Section, content: String) UpdateState(section: Section, content: String)
SetView(view_idx: layout.ViewIdx)
// NodeEvent
Exit(reply_to: Subject(Nil)) Exit(reply_to: Subject(Nil))
} }

View File

@@ -125,13 +125,11 @@ pub fn update_dimensions(layout: Layout, columns: Int, rows: Int) -> Layout {
Layout(..layout, columns:, rows:) Layout(..layout, columns:, rows:)
} }
pub fn render(layout: Layout) -> Nil { pub fn set_view(layout: Layout, view_idx: ViewIdx) -> Layout {
[layout.columns, layout.rows] Layout(..layout, current_view: view_idx)
|> list.map(int.to_string) }
|> string.join(" ")
|> string.append("layout - render: ", _)
|> logging.log
pub fn render(layout: Layout) -> Nil {
let context = let context =
RenderContext( RenderContext(
parent_width: layout.columns, parent_width: layout.columns,
@@ -149,6 +147,7 @@ pub fn render(layout: Layout) -> Nil {
render_loop( render_loop(
view, view,
context, context,
// TODO extract to function `view_index`
Section(string.append("view_", string.inspect(layout.current_view))), Section(string.append("view_", string.inspect(layout.current_view))),
buffer, buffer,
) )

View File

@@ -29,6 +29,7 @@ pub fn text(buffer: Buffer, text: String, x: Int, y: Int) -> Buffer {
} }
pub fn box(buffer: Buffer, x: Int, y: Int, width: Int, height: Int) -> Buffer { pub fn box(buffer: Buffer, x: Int, y: Int, width: Int, height: Int) -> Buffer {
// TODO move box style to `layout.Style`
let box_chars = #("", "", "", "", "", "") let box_chars = #("", "", "", "", "", "")
let #(tl, tr, bl, br, hor, ver) = box_chars let #(tl, tr, bl, br, hor, ver) = box_chars

View File

@@ -120,6 +120,12 @@ fn handle_message(
process.send(reply_to, Nil) process.send(reply_to, Nil)
actor.stop() actor.stop()
} }
control.SetView(view_idx) -> {
let layout = layout.set_view(state.layout, view_idx)
actor.send(state.redraw, layout)
actor.continue(State(..state, layout:))
}
} }
} }