From 563a8e34bac69e21b033c0c6d19fc294080e041b Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 15 Oct 2026 22:39:14 +0200 Subject: [PATCH] feat: edit capture patterns with a cursor Open capture patterns in a dedicated popup with insertion-point navigation, local Backspace/Delete editing, Home/End movement, and cancellable changes. Preserve the existing selector apply flow and document the interaction.\n\nTests: cargo fmt --all -- --check\nTests: cargo check --workspace --all-targets --all-features\nTests: cargo test --workspace --all-targets --all-features\nTests: cargo test --workspace --doc --all-features\nTests: cargo clippy --workspace --all-targets --all-features -- -D warnings\nTests: cargo build --workspace --release --all-features\nTests: cargo check --workspace\nTests: cargo check --workspace --no-default-features\nTests: cargo check -p midi --no-default-features --features midir\nTests: cargo check -p volume_lib --no-default-features --features pulse --- README.md | 6 +- architecture.md | 6 +- task.md | 2 + tui/src/lib.rs | 197 +++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 178 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index c15925d..d9b4158 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,10 @@ separator; D removes a selected separator; and `h` hides a fader. Fader editing provides capture Field/Exact-or-Regex/Pattern controls, group membership and mode, and separate controller, Learn MIDI, and Unlearn MIDI actions for both volume and mute. Learning opens a waiting popup and captures the next supported -fader movement or button press from the selected controller. The editor also -provides per-fader Size (`Slim`, `Standard`, or `Thick`) and visibility. Slim +fader movement or button press from the selected controller. Capture patterns +open in a cursor-aware popup, so individual characters can be inserted or +removed without replacing the whole value. The editor also provides per-fader +Size (`Slim`, `Standard`, or `Thick`) and visibility. Slim faders occupy one terminal row in Vertical mode; Standard remains the compatible default. Horizontal mode presents side-by-side fader strips with bottom-up meters, status, percentage, and bottom labels, with Size controlling strip diff --git a/architecture.md b/architecture.md index 810f18f..0837390 100644 --- a/architecture.md +++ b/architecture.md @@ -427,8 +427,10 @@ added, renamed, moved, or removed. Adding or renaming a separator uses a centered naming dialog, keeping transient text entry out of the mixer status line. Editing a fader presents domain fields rather than a serialized command: -- **Capture:** application selector field, exact/regex mode, and pattern; group - faders instead present member faders and the currently available group mode. +- **Capture:** application selector field, exact/regex mode, and pattern. Pattern + values open in a cursor-aware modal with insertion, Backspace, Delete, + Home/End, accept, and cancel controls; group faders instead present member + faders and the currently available group mode. - **Volume control:** separate controller selection, Learn MIDI, and Unlearn MIDI actions. - **Mute control:** separate controller selection, Learn MIDI, and Unlearn MIDI diff --git a/task.md b/task.md index 2b9d0dc..872deb3 100644 --- a/task.md +++ b/task.md @@ -386,6 +386,8 @@ Dependencies: M8, M2, M1. application IDs. `E` on a mixer fader opens a field-oriented form. Application capture edits Field, Exact/Regex, and Pattern separately while preserving other selectors; + Pattern opens a cursor-aware modal for local insertion and deletion rather + than requiring the full value to be erased; groups show application-fader membership and the currently supported Absolute mode. Volume and Mute show separate controller, Learn MIDI, and Unlearn MIDI actions. Learning opens a modal prompt for a fader movement or button press; diff --git a/tui/src/lib.rs b/tui/src/lib.rs index 377c60b..83c4e77 100644 --- a/tui/src/lib.rs +++ b/tui/src/lib.rs @@ -22,7 +22,7 @@ use ratatui::{ layout::{Alignment, Constraint, Direction, Layout, Rect}, style::{Style, Stylize}, symbols::border, - text::{Line, Text}, + text::{Line, Span, Text}, widgets::{Block, Clear, Gauge, Paragraph, Widget, Wrap}, }; @@ -820,6 +820,7 @@ pub struct LiveTui { settings_selected: usize, fader_editor: Option, capture_editor: Option, + pattern_editor: Option, controller_picker: Option, controller_selections: BTreeMap<(String, String), String>, } @@ -862,6 +863,12 @@ struct CaptureEditor { selected: usize, } +#[derive(Clone, Debug, Eq, PartialEq)] +struct PatternEditor { + value: String, + cursor: usize, +} + #[derive(Clone, Debug, Eq, PartialEq)] struct ControllerPicker { fader: String, @@ -986,6 +993,7 @@ impl LiveTui { settings_selected: 0, fader_editor: None, capture_editor: None, + pattern_editor: None, controller_picker: None, controller_selections: BTreeMap::new(), } @@ -1107,6 +1115,10 @@ impl LiveTui { } return; } + if self.pattern_editor.is_some() { + self.handle_pattern_key(key_event.code); + return; + } if self.capture_editor.is_some() { self.handle_capture_key(key_event.code); return; @@ -1723,34 +1735,80 @@ impl LiveTui { KeyCode::Left if editor.selected == 0 => editor.field = editor.field.saturating_sub(1), KeyCode::Right if editor.selected == 0 => editor.field = (editor.field + 1).min(3), KeyCode::Left | KeyCode::Right if editor.selected == 1 => editor.regex = !editor.regex, - KeyCode::Backspace if editor.selected == 2 => { - editor.pattern.pop(); - } - KeyCode::Char(character) if editor.selected == 2 => editor.pattern.push(character), KeyCode::Enter if editor.selected < 2 => editor.selected += 1, KeyCode::Enter => { - let edit = ConfigurationEdit::SetApplicationSelector { - id: editor.application.clone(), - index: editor.selector, - field: selector_field_name(editor.field).to_owned(), - pattern: editor.pattern.clone(), - regex: editor.regex, - }; - match self - .client - .edit_configuration(edit, self.snapshot.configuration.revision) - { - Ok(snapshot) => { - self.capture_editor = None; - self.replace_snapshot(snapshot); - } - Err(error) => self.client_error = Some(error.to_string()), - } + self.pattern_editor = Some(PatternEditor { + cursor: editor.pattern.chars().count(), + value: editor.pattern.clone(), + }); } _ => {} } } + fn handle_pattern_key(&mut self, code: KeyCode) { + if code == KeyCode::Esc { + self.pattern_editor = None; + return; + } + let Some(editor) = &mut self.pattern_editor else { + return; + }; + let character_count = editor.value.chars().count(); + match code { + KeyCode::Left => editor.cursor = editor.cursor.saturating_sub(1), + KeyCode::Right => editor.cursor = (editor.cursor + 1).min(character_count), + KeyCode::Home => editor.cursor = 0, + KeyCode::End => editor.cursor = character_count, + KeyCode::Backspace if editor.cursor > 0 => { + let previous = editor.cursor - 1; + remove_character(&mut editor.value, previous); + editor.cursor = previous; + } + KeyCode::Delete if editor.cursor < character_count => { + remove_character(&mut editor.value, editor.cursor); + } + KeyCode::Char(character) => { + let byte = character_byte_index(&editor.value, editor.cursor); + editor.value.insert(byte, character); + editor.cursor += 1; + } + KeyCode::Enter => { + let Some(pattern) = self.pattern_editor.take() else { + return; + }; + if let Some(capture) = &mut self.capture_editor { + capture.pattern = pattern.value; + } + self.submit_capture_editor(); + } + _ => {} + } + } + + fn submit_capture_editor(&mut self) { + let Some(editor) = &self.capture_editor else { + return; + }; + let edit = ConfigurationEdit::SetApplicationSelector { + id: editor.application.clone(), + index: editor.selector, + field: selector_field_name(editor.field).to_owned(), + pattern: editor.pattern.clone(), + regex: editor.regex, + }; + match self + .client + .edit_configuration(edit, self.snapshot.configuration.revision) + { + Ok(snapshot) => { + self.capture_editor = None; + self.replace_snapshot(snapshot); + } + Err(error) => self.client_error = Some(error.to_string()), + } + } + fn handle_controller_picker_key(&mut self, code: KeyCode) { if code == KeyCode::Esc { self.controller_picker = None; @@ -2054,6 +2112,9 @@ impl Widget for &LiveTui { if self.capture_editor.is_some() { self.render_capture_editor(area, buf); } + if self.pattern_editor.is_some() { + self.render_pattern_editor(area, buf); + } if self.controller_picker.is_some() { self.render_controller_picker(area, buf); } @@ -2193,6 +2254,21 @@ fn selector_field_index(field: &str) -> usize { } } +fn character_byte_index(value: &str, character_index: usize) -> usize { + value + .char_indices() + .nth(character_index) + .map_or(value.len(), |(byte, _)| byte) +} + +fn remove_character(value: &mut String, character_index: usize) { + let start = character_byte_index(value, character_index); + let end = character_byte_index(value, character_index.saturating_add(1)); + if start < end { + value.drain(start..end); + } +} + impl LiveTui { fn render_separator_editor(&self, area: Rect, buf: &mut Buffer) { let Some(editor) = &self.editor else { @@ -2357,7 +2433,7 @@ impl LiveTui { Block::bordered() .title(" Edit capture ".fg(theme.accent).bold()) .title_bottom( - " ↑↓ field · ←→ choice · type pattern · Enter next/save · Esc back " + " ↑↓ field · ←→ choice · Enter next/edit pattern · Esc back " .fg(theme.muted), ) .border_set(border::DOUBLE) @@ -2367,6 +2443,48 @@ impl LiveTui { .render(popup, buf); } + fn render_pattern_editor(&self, area: Rect, buf: &mut Buffer) { + let Some(editor) = &self.pattern_editor else { + return; + }; + let theme = self.theme.palette(); + let popup = centered_rect(area, 72, 7); + let characters = editor.value.chars().collect::>(); + let cursor = editor.cursor.min(characters.len()); + let capacity = usize::from(popup.width.saturating_sub(4)).saturating_sub(1); + let mut start = cursor.saturating_sub(capacity / 2); + if characters.len().saturating_sub(start) < capacity { + start = characters.len().saturating_sub(capacity); + } + let end = start.saturating_add(capacity).min(characters.len()); + let before = characters[start..cursor].iter().collect::(); + let after = characters[cursor..end].iter().collect::(); + + Clear.render(popup, buf); + Paragraph::new(Text::from(vec![ + Line::from("Pattern"), + Line::from(vec![ + Span::raw(before), + "▏".fg(theme.accent).bold(), + Span::raw(after), + ]) + .style(Style::default().fg(theme.foreground)), + Line::from("Use the cursor to insert or remove individual characters.").fg(theme.muted), + ])) + .block( + Block::bordered() + .title(" Edit capture pattern ".fg(theme.accent).bold()) + .title_bottom( + " ←→/Home/End move · Backspace/Delete edit · Enter apply · Esc cancel " + .fg(theme.muted), + ) + .border_set(border::DOUBLE) + .border_style(Style::default().fg(theme.accent)) + .style(Style::default().bg(theme.background)), + ) + .render(popup, buf); + } + fn render_controller_picker(&self, area: Rect, buf: &mut Buffer) { let Some(picker) = &self.controller_picker else { return; @@ -3633,12 +3751,33 @@ mod tests { app.handle_key_event(press(KeyCode::Down)); app.handle_key_event(press(KeyCode::Right)); app.handle_key_event(press(KeyCode::Down)); - for _ in 0.."Old Player".len() { - app.handle_key_event(press(KeyCode::Backspace)); - } - for character in "Qobuz".chars() { - app.handle_key_event(press(KeyCode::Char(character))); + app.handle_key_event(press(KeyCode::Enter)); + let mut pattern_editor = Buffer::empty(area); + (&app).render(area, &mut pattern_editor); + let pattern_rendered = pattern_editor + .content + .iter() + .map(|cell| cell.symbol()) + .collect::(); + assert!(pattern_rendered.contains("Edit capture pattern")); + assert!(pattern_rendered.contains('▏')); + + app.handle_key_event(press(KeyCode::Esc)); + assert!(app.pattern_editor.is_none()); + assert_eq!( + app.capture_editor + .as_ref() + .map(|editor| editor.pattern.as_str()), + Some("Old Player") + ); + app.handle_key_event(press(KeyCode::Enter)); + + app.handle_key_event(press(KeyCode::Home)); + for _ in 0..4 { + app.handle_key_event(press(KeyCode::Right)); } + app.handle_key_event(press(KeyCode::Delete)); + app.handle_key_event(press(KeyCode::Char('p'))); app.handle_key_event(press(KeyCode::Enter)); app.handle_key_event(press(KeyCode::Down)); @@ -3680,7 +3819,7 @@ mod tests { pattern, regex: true, .. - } if id == "music" && field == "application.id" && pattern == "Qobuz" + } if id == "music" && field == "application.id" && pattern == "Old player" ))); assert!(edits.iter().any(|edit| matches!( edit,