Skip to content

Commit

Permalink
support for home/end keys
Browse files Browse the repository at this point in the history
  • Loading branch information
lasantosr committed May 15, 2023
1 parent b216af7 commit d69e33c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/common/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ pub trait InteractiveProcess: Process {
// TODO edit
}
// Selection
KeyCode::Home => self.home(),
KeyCode::End => self.end(),
KeyCode::Char(c) if has_ctrl && c == 'k' => self.prev(),
KeyCode::Char(c) if has_ctrl && c == 'j' => self.next(),
KeyCode::Up => self.move_up(),
Expand Down Expand Up @@ -157,6 +159,11 @@ pub trait InteractiveProcess: Process {
/// Moves the selection to the next item
fn next(&mut self);

/// Home button, usually moving selection to the first
fn home(&mut self);
/// End button, usually moving selection to the last
fn end(&mut self);

/// Inserts the given text into the currently selected input, if any
fn insert_text(&mut self, text: String) -> Result<()>;
/// Inserts the given char into the currently selected input, if any
Expand Down
14 changes: 14 additions & 0 deletions src/common/widget/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ where
}
}

/// Selects the first item on the list
pub fn first(&mut self) {
if !self.items.is_empty() {
self.state.select(Some(0));
}
}

/// Selects the last item on the list
pub fn last(&mut self) {
if !self.items.is_empty() {
self.state.select(Some(self.items.len() - 1))
}
}

/// Returns a mutable reference to the current selected item
pub fn current_mut(&mut self) -> Option<&mut T> {
if let Some(selected) = self.state.selected() {
Expand Down
10 changes: 10 additions & 0 deletions src/common/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,16 @@ impl TextInput {
}
}

/// Moves internal cursor to the line beginning
pub fn move_beginning(&mut self) {
self.cursor.x = 0;
}

/// Moves internal cursor to the line end
pub fn move_end(&mut self) {
self.cursor.x = self.current_line_length();
}

/// Inserts the given text at the internal cursor
pub fn insert_text(&mut self, text: impl Into<String>) {
let text = unify_newlines(text.into());
Expand Down
8 changes: 8 additions & 0 deletions src/process/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ impl<'s> InteractiveProcess for LabelProcess<'s> {
self.suggestions.next()
}

fn home(&mut self) {
self.suggestions.first()
}

fn end(&mut self) {
self.suggestions.last()
}

fn insert_text(&mut self, text: String) -> Result<()> {
if let Some(LabelSuggestionItem::New(suggestion)) = self.suggestions.current_mut() {
suggestion.insert_text(text);
Expand Down
8 changes: 8 additions & 0 deletions src/process/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ impl<'s> InteractiveProcess for SaveCommandProcess<'s> {

fn next(&mut self) {}

fn home(&mut self) {
self.current_description.inner_mut().move_beginning()
}

fn end(&mut self) {
self.current_description.inner_mut().move_end()
}

fn insert_text(&mut self, text: String) -> Result<()> {
self.current_description.inner_mut().insert_text(text);
Ok(())
Expand Down
8 changes: 8 additions & 0 deletions src/process/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ impl<'s> InteractiveProcess for SearchProcess<'s> {
self.commands.next()
}

fn home(&mut self) {
self.commands.first()
}

fn end(&mut self) {
self.commands.last()
}

fn insert_text(&mut self, text: String) -> Result<()> {
self.filter.inner_mut().insert_text(text);
self.commands
Expand Down

0 comments on commit d69e33c

Please sign in to comment.