Skip to content

Commit 5f54a94

Browse files
committed
Fix missing style copy, add Ctrl+A -> Backspace/Delete behavior
1 parent 4c09fdd commit 5f54a94

3 files changed

Lines changed: 51 additions & 20 deletions

File tree

src/app.rs

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -366,23 +366,33 @@ impl App {
366366
// let at_port_selection = matches!(self.menu, Menu::PortSelection);
367367
// TODO soon, redo this variable's name + use
368368
let mut at_port_selection = false;
369+
let mut at_terminal = false;
369370
// Filter for when we decide to handle user *text input*.
370371
match self.menu {
371372
Menu::Terminal(TerminalPrompt::None) => {
372-
match self
373-
.user_input
374-
.input_box
375-
.handle_event(&ratatui::crossterm::event::Event::Key(key))
376-
{
377-
// If we changed something in the value when handling the key event,
378-
// we should clear the user_history selection.
379-
Some(StateChanged {
380-
value,
381-
cursor: _cursor,
382-
}) if value => {
383-
self.user_input.history.clear_selection();
384-
}
385-
_ => (),
373+
at_terminal = true;
374+
match key.code {
375+
// Consuming Ctrl+A so input_box.handle_event doesn't move my cursor.
376+
KeyCode::Char('a') if ctrl_pressed => (),
377+
KeyCode::Delete | KeyCode::Backspace if self.user_input.all_text_selected => (),
378+
379+
_ => match self
380+
.user_input
381+
.input_box
382+
.handle_event(&ratatui::crossterm::event::Event::Key(key))
383+
{
384+
// If we changed something in the value when handling the key event,
385+
// we should clear the user_history selection.
386+
Some(StateChanged { value: true, .. }) => {
387+
self.user_input.history.clear_selection();
388+
self.user_input.all_text_selected = false;
389+
}
390+
391+
Some(StateChanged { cursor: true, .. }) => {
392+
self.user_input.all_text_selected = false;
393+
}
394+
_ => (),
395+
},
386396
}
387397
}
388398
Menu::Terminal(TerminalPrompt::DisconnectPrompt) => (),
@@ -409,6 +419,13 @@ impl App {
409419
}
410420
_ => self.shutdown(),
411421
},
422+
'a' | 'A'
423+
if ctrl_pressed
424+
&& at_terminal
425+
&& !self.user_input.input_box.value().is_empty() =>
426+
{
427+
self.user_input.all_text_selected = true;
428+
}
412429
'w' | 'W' if ctrl_pressed => {
413430
self.buffer.state.text_wrapping = !self.buffer.state.text_wrapping;
414431
self.buffer.scroll_by(0);
@@ -460,7 +477,9 @@ impl App {
460477
// TODO Ctrl+A -> Backspace | Delete to clear input buffer
461478
KeyCode::PageUp if ctrl_pressed || shift_pressed => self.buffer.scroll_by(i32::MAX),
462479
KeyCode::PageDown if ctrl_pressed || shift_pressed => self.buffer.scroll_by(i32::MIN),
463-
KeyCode::Delete | KeyCode::Backspace if ctrl_pressed && shift_pressed => {
480+
KeyCode::Delete | KeyCode::Backspace
481+
if (ctrl_pressed && shift_pressed) || self.user_input.all_text_selected =>
482+
{
464483
self.user_input.reset();
465484
}
466485
KeyCode::PageUp => self.buffer.scroll_page_up(),
@@ -501,6 +520,7 @@ impl App {
501520
}
502521
}
503522
fn up_pressed(&mut self) {
523+
self.user_input.all_text_selected = false;
504524
match self.popup {
505525
None => (),
506526
Some(Popup::PortSettings) => {
@@ -528,6 +548,7 @@ impl App {
528548
self.post_menu_scroll(true);
529549
}
530550
fn down_pressed(&mut self) {
551+
self.user_input.all_text_selected = false;
531552
match self.popup {
532553
None => (),
533554
Some(Popup::PortSettings) => {
@@ -1032,13 +1053,19 @@ impl App {
10321053
frame.render_widget(signals_line, line_area.offset(Offset { x: 3, y: 0 }));
10331054
}
10341055

1035-
let input_style = match &self.failed_send_at {
1036-
Some(instant) if instant.elapsed() < FAILED_SEND_VISUAL_TIME => Style::new().on_red(),
1056+
let input_style = match (&self.failed_send_at, self.user_input.all_text_selected) {
1057+
(Some(instant), _) if instant.elapsed() < FAILED_SEND_VISUAL_TIME => {
1058+
Style::new().on_red()
1059+
}
1060+
(Some(instant), true) if instant.elapsed() < FAILED_SEND_VISUAL_TIME => {
1061+
Style::new().reversed().on_red()
1062+
}
1063+
(_, true) => Style::new().reversed(),
10371064
_ => Style::new(),
10381065
};
10391066

10401067
let input_symbol = Span::raw(">").style(if self.serial_healthy {
1041-
input_style.green()
1068+
input_style.not_reversed().green()
10421069
} else {
10431070
input_style.red()
10441071
});
@@ -1059,7 +1086,7 @@ impl App {
10591086
.scroll((0, scroll as u16))
10601087
.style(input_style);
10611088
frame.render_widget(input_text, input_area);
1062-
if !disconnect_prompt_shown {
1089+
if !disconnect_prompt_shown && self.popup.is_none() {
10631090
frame.set_cursor_position((
10641091
// Put cursor past the end of the input text
10651092
input_area.x

src/history.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub struct History {
1111

1212
pub struct UserInput {
1313
pub input_box: Input,
14+
pub all_text_selected: bool,
1415
pub preserved_input: Option<String>,
1516
pub history: History,
1617
pub clipboard: Clipboard,
@@ -20,6 +21,7 @@ impl Default for UserInput {
2021
fn default() -> Self {
2122
Self {
2223
input_box: Input::default(),
24+
all_text_selected: false,
2325
preserved_input: None,
2426
history: History::new(),
2527
clipboard: Clipboard::new().unwrap(),
@@ -32,6 +34,7 @@ impl UserInput {
3234
self.input_box.reset();
3335
self.history.clear_selection();
3436
self.preserved_input = None;
37+
self.all_text_selected = false;
3538
}
3639
pub fn scroll_history(&mut self, up: bool) {
3740
let entry = self.history.scroll(up);

src/traits.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,14 @@ impl LineHelpers for Line<'_> {
156156
fn borrowed_spans_iter<'a>(&'a self) -> impl DoubleEndedIterator<Item = Span<'a>> {
157157
self.spans
158158
.iter()
159-
.map(|s| Span::styled(Cow::Borrowed(s.content.as_ref()), self.style))
159+
.map(|s| Span::styled(Cow::Borrowed(s.content.as_ref()), s.style))
160160
}
161161
fn new_borrowing<'a>(&'a self) -> Line<'a> {
162162
let mut line = Line::from_iter(self.borrowed_spans_iter());
163163
if self.alignment.is_some() {
164164
line.alignment = self.alignment;
165165
}
166+
line.style = self.style;
166167
line
167168
}
168169
}

0 commit comments

Comments
 (0)