Improve key inputs

Add support proper implementations of -

- backspace
- ctrl-w
- ctrl-u

Also, improved sort and filter behavior.

Fixes: https://github.com/sayanarijit/xplr/issues/77
This commit is contained in:
Arijit Basu 2021-04-18 22:29:03 +05:30 committed by Arijit Basu
parent 468cf7f3df
commit 1bb2622f24
3 changed files with 166 additions and 19 deletions

View File

@ -832,6 +832,12 @@ pub enum ExternalMsg {
/// Example: `SetInputBuffer: foo` /// Example: `SetInputBuffer: foo`
SetInputBuffer(String), SetInputBuffer(String),
/// Remove input buffer's last character.
RemoveInputBufferLastCharacter,
/// Remove input buffer's last word.
RemoveInputBufferLastWord,
/// Reset the input buffer back to null. It will not show in the UI. /// Reset the input buffer back to null. It will not show in the UI.
ResetInputBuffer, ResetInputBuffer,
@ -929,6 +935,9 @@ pub enum ExternalMsg {
/// Example: `RemoveNodeFilterFromInput: RelativePathDoesStartWith` /// Example: `RemoveNodeFilterFromInput: RelativePathDoesStartWith`
RemoveNodeFilterFromInput(NodeFilter), RemoveNodeFilterFromInput(NodeFilter),
/// Remove the last node filter.
RemoveLastNodeFilter,
/// Reset the node filters back to the default configuration. /// Reset the node filters back to the default configuration.
ResetNodeFilters, ResetNodeFilters,
@ -958,6 +967,9 @@ pub enum ExternalMsg {
/// Reverse the node sorters. /// Reverse the node sorters.
ReverseNodeSorters, ReverseNodeSorters,
/// Remove the last node sorter.
RemoveLastNodeSorter,
/// Reset the node sorters back to the default configuration. /// Reset the node sorters back to the default configuration.
ResetNodeSorters, ResetNodeSorters,
@ -1281,6 +1293,10 @@ impl App {
ExternalMsg::BufferInput(input) => self.buffer_input(&input), ExternalMsg::BufferInput(input) => self.buffer_input(&input),
ExternalMsg::BufferInputFromKey => self.buffer_input_from_key(key), ExternalMsg::BufferInputFromKey => self.buffer_input_from_key(key),
ExternalMsg::SetInputBuffer(input) => self.set_input_buffer(input), ExternalMsg::SetInputBuffer(input) => self.set_input_buffer(input),
ExternalMsg::RemoveInputBufferLastCharacter => {
self.remove_input_buffer_last_character()
}
ExternalMsg::RemoveInputBufferLastWord => self.remove_input_buffer_last_word(),
ExternalMsg::ResetInputBuffer => self.reset_input_buffer(), ExternalMsg::ResetInputBuffer => self.reset_input_buffer(),
ExternalMsg::SwitchMode(mode) => self.switch_mode(&mode), ExternalMsg::SwitchMode(mode) => self.switch_mode(&mode),
ExternalMsg::Call(cmd) => self.call(cmd), ExternalMsg::Call(cmd) => self.call(cmd),
@ -1302,12 +1318,14 @@ impl App {
ExternalMsg::RemoveNodeFilter(f) => self.remove_node_filter(f), ExternalMsg::RemoveNodeFilter(f) => self.remove_node_filter(f),
ExternalMsg::RemoveNodeFilterFromInput(f) => self.remove_node_filter_from_input(f), ExternalMsg::RemoveNodeFilterFromInput(f) => self.remove_node_filter_from_input(f),
ExternalMsg::ToggleNodeFilter(f) => self.toggle_node_filter(f), ExternalMsg::ToggleNodeFilter(f) => self.toggle_node_filter(f),
ExternalMsg::RemoveLastNodeFilter => self.remove_last_node_filter(),
ExternalMsg::ResetNodeFilters => self.reset_node_filters(), ExternalMsg::ResetNodeFilters => self.reset_node_filters(),
ExternalMsg::ClearNodeFilters => self.clear_node_filters(), ExternalMsg::ClearNodeFilters => self.clear_node_filters(),
ExternalMsg::AddNodeSorter(f) => self.add_node_sorter(f), ExternalMsg::AddNodeSorter(f) => self.add_node_sorter(f),
ExternalMsg::RemoveNodeSorter(f) => self.remove_node_sorter(f), ExternalMsg::RemoveNodeSorter(f) => self.remove_node_sorter(f),
ExternalMsg::ReverseNodeSorter(f) => self.reverse_node_sorter(f), ExternalMsg::ReverseNodeSorter(f) => self.reverse_node_sorter(f),
ExternalMsg::ToggleNodeSorter(f) => self.toggle_node_sorter(f), ExternalMsg::ToggleNodeSorter(f) => self.toggle_node_sorter(f),
ExternalMsg::RemoveLastNodeSorter => self.remove_last_node_sorter(),
ExternalMsg::ReverseNodeSorters => self.reverse_node_sorters(), ExternalMsg::ReverseNodeSorters => self.reverse_node_sorters(),
ExternalMsg::ResetNodeSorters => self.reset_node_sorters(), ExternalMsg::ResetNodeSorters => self.reset_node_sorters(),
ExternalMsg::ClearNodeSorters => self.clear_node_sorters(), ExternalMsg::ClearNodeSorters => self.clear_node_sorters(),
@ -1500,6 +1518,34 @@ impl App {
Ok(self) Ok(self)
} }
fn remove_input_buffer_last_character(mut self) -> Result<Self> {
if let Some(mut buf) = self.input_buffer {
buf.pop();
self.input_buffer = Some(buf);
};
self.msg_out.push_back(MsgOut::Refresh);
Ok(self)
}
fn remove_input_buffer_last_word(mut self) -> Result<Self> {
if let Some(buf) = self.input_buffer {
let buf = buf
.chars()
.into_iter()
.rev()
.skip_while(|c| !c.is_ascii_alphanumeric())
.skip_while(|c| c.is_ascii_alphanumeric())
.collect::<Vec<char>>()
.into_iter()
.rev()
.collect::<String>();
self.input_buffer = Some(buf);
};
self.msg_out.push_back(MsgOut::Refresh);
Ok(self)
}
fn reset_input_buffer(mut self) -> Result<Self> { fn reset_input_buffer(mut self) -> Result<Self> {
self.input_buffer = None; self.input_buffer = None;
self.msg_out.push_back(MsgOut::Refresh); self.msg_out.push_back(MsgOut::Refresh);
@ -1728,6 +1774,11 @@ impl App {
} }
} }
fn remove_last_node_filter(mut self) -> Result<Self> {
self.explorer_config.filters.pop();
Ok(self)
}
fn reset_node_filters(mut self) -> Result<Self> { fn reset_node_filters(mut self) -> Result<Self> {
self.explorer_config.filters.clear(); self.explorer_config.filters.clear();
@ -1773,6 +1824,11 @@ impl App {
} }
} }
fn remove_last_node_sorter(mut self) -> Result<Self> {
self.explorer_config.sorters.pop();
Ok(self)
}
fn reverse_node_sorters(mut self) -> Result<Self> { fn reverse_node_sorters(mut self) -> Result<Self> {
self.explorer_config.sorters = self self.explorer_config.sorters = self
.explorer_config .explorer_config

View File

@ -391,22 +391,26 @@ modes:
echo Refresh >> "${XPLR_PIPE_MSG_IN:?}" echo Refresh >> "${XPLR_PIPE_MSG_IN:?}"
fi fi
- SwitchMode: default - SwitchMode: default
backspace: backspace:
help: clear help: remove last character
messages:
- RemoveInputBufferLastCharacter
ctrl-w:
help: remove last word
messages:
- RemoveInputBufferLastWord
ctrl-u:
help: remove line
messages: messages:
- SetInputBuffer: "" - SetInputBuffer: ""
esc: esc:
help: cancel help: cancel
messages: messages:
- SwitchMode: default - SwitchMode: default
ctrl-c: ctrl-c:
help: terminate help: terminate
messages: messages:
- Terminate - Terminate
default: default:
messages: messages:
- BufferInputFromKey - BufferInputFromKey
@ -434,7 +438,17 @@ modes:
- SwitchMode: default - SwitchMode: default
backspace: backspace:
help: clear help: remove last character
messages:
- RemoveInputBufferLastCharacter
ctrl-w:
help: remove last word
messages:
- RemoveInputBufferLastWord
ctrl-u:
help: remove line
messages: messages:
- SetInputBuffer: "" - SetInputBuffer: ""
@ -507,7 +521,17 @@ modes:
- SwitchMode: default - SwitchMode: default
backspace: backspace:
help: clear help: remove last character
messages:
- RemoveInputBufferLastCharacter
ctrl-w:
help: remove last word
messages:
- RemoveInputBufferLastWord
ctrl-u:
help: remove line
messages: messages:
- SetInputBuffer: "" - SetInputBuffer: ""
@ -529,26 +553,59 @@ modes:
name: filter name: filter
key_bindings: key_bindings:
on_key: on_key:
backspace: ctrl-x:
help: clear help: remove last filter
messages: messages:
- RemoveLastNodeFilter
- SetInputBuffer: "" - SetInputBuffer: ""
- ClearNodeFilters - Explore
backspace:
help: remove last character
messages:
- RemoveNodeFilterFromInput: IRelativePathDoesContain
- RemoveInputBufferLastCharacter
- AddNodeFilterFromInput: IRelativePathDoesContain
- Explore
ctrl-w:
help: remove last word
messages:
- RemoveNodeFilterFromInput: IRelativePathDoesContain
- RemoveInputBufferLastWord
- AddNodeFilterFromInput: IRelativePathDoesContain
- Explore
ctrl-u:
help: remove line
messages:
- RemoveNodeFilterFromInput: IRelativePathDoesContain
- SetInputBuffer: ""
- AddNodeFilterFromInput: IRelativePathDoesContain
- Explore - Explore
ctrl-r: ctrl-r:
help: reset help: reset filters
messages: messages:
- SetInputBuffer: "" - SetInputBuffer: ""
- ResetNodeFilters - ResetNodeFilters
- Explore - Explore
ctrl-l:
help: clear filters
messages:
- SetInputBuffer: ""
- ClearNodeFilters
- Explore
enter: enter:
help: apply help: apply filter
messages: messages:
- SwitchMode: default - SwitchMode: default
esc: esc:
help: cancel help: cancel
messages: messages:
- RemoveNodeFilterFromInput: IRelativePathDoesContain
- SwitchMode: default - SwitchMode: default
- Explore
ctrl-c:
help: terminate
messages:
- Terminate
default: default:
help: null help: null
messages: messages:
@ -565,20 +622,25 @@ modes:
remaps: {} remaps: {}
on_key: on_key:
backspace: backspace:
help: clear help: remove last sorter
messages: messages:
- ClearNodeSorters - RemoveLastNodeSorter
- Explore - Explore
'!': '!':
help: reverse all help: reverse sorters
messages: messages:
- ReverseNodeSorters - ReverseNodeSorters
- Explore - Explore
ctrl-r: ctrl-r:
help: reset help: reset sorters
messages: messages:
- ResetNodeSorters - ResetNodeSorters
- Explore - Explore
ctrl-l:
help: clear sorters
messages:
- ClearNodeSorters
- Explore
r: r:
help: by relative path help: by relative path
messages: messages:
@ -769,6 +831,11 @@ modes:
help: next visited path help: next visited path
messages: messages:
- NextVisitedPath - NextVisitedPath
ctrl-l:
help: clear screen
messages:
- ClearScreen
- Refresh
q: q:
help: quit help: quit
messages: messages:
@ -828,7 +895,15 @@ modes:
k: up k: up
on_key: on_key:
backspace: backspace:
help: clear help: remove last character
messages:
- RemoveInputBufferLastCharacter
ctrl-w:
help: remove last word
messages:
- RemoveInputBufferLastWord
ctrl-u:
help: remove line
messages: messages:
- SetInputBuffer: "" - SetInputBuffer: ""
ctrl-c: ctrl-c:
@ -980,10 +1055,25 @@ modes:
remaps: {} remaps: {}
on_key: on_key:
backspace: backspace:
help: clear help: remove last character
messages: messages:
- RemoveNodeFilterFromInput: IRelativePathDoesContain - RemoveNodeFilterFromInput: IRelativePathDoesContain
- SetInputBuffer: '' - RemoveInputBufferLastCharacter
- AddNodeFilterFromInput: IRelativePathDoesContain
- Explore
ctrl-w:
help: remove last word
messages:
- RemoveNodeFilterFromInput: IRelativePathDoesContain
- RemoveInputBufferLastWord
- AddNodeFilterFromInput: IRelativePathDoesContain
- Explore
ctrl-u:
help: remove line
messages:
- RemoveNodeFilterFromInput: IRelativePathDoesContain
- SetInputBuffer: ""
- AddNodeFilterFromInput: IRelativePathDoesContain
- Explore - Explore
ctrl-c: ctrl-c:
help: terminate help: terminate

View File

@ -445,6 +445,7 @@ impl Key {
Self::ShiftY => Some('Y'), Self::ShiftY => Some('Y'),
Self::ShiftZ => Some('Z'), Self::ShiftZ => Some('Z'),
Self::Space => Some(' '),
Self::Special(c) => Some(c.to_owned()), Self::Special(c) => Some(c.to_owned()),
_ => None, _ => None,