feat: add focus wrap around

This commit is contained in:
graelo 2020-06-01 10:06:50 +02:00
parent 8464c451e3
commit b223e280f1
2 changed files with 31 additions and 5 deletions

View file

@ -44,6 +44,7 @@ pub fn run(buffer: String, opt: &CliOpt) -> Option<(String, bool)> {
let mut viewbox = view::View::new( let mut viewbox = view::View::new(
&mut model, &mut model,
opt.unique_hint, opt.unique_hint,
opt.focus_wrap_around,
&opt.hint_alignment, &opt.hint_alignment,
&opt.colors, &opt.colors,
hint_style, hint_style,
@ -95,6 +96,10 @@ pub struct CliOpt {
#[clap(short = "a", long, arg_enum, default_value = "leading")] #[clap(short = "a", long, arg_enum, default_value = "leading")]
hint_alignment: view::HintAlignment, hint_alignment: view::HintAlignment,
/// Move focus back to first/last match.
#[clap(long)]
focus_wrap_around: bool,
/// Optional hint styling. /// Optional hint styling.
/// ///
/// Underline or surround the hint for increased visibility. /// Underline or surround the hint for increased visibility.

View file

@ -14,6 +14,7 @@ pub struct View<'a> {
matches: Vec<model::Match<'a>>, matches: Vec<model::Match<'a>>,
lookup_trie: SequenceTrie<char, usize>, lookup_trie: SequenceTrie<char, usize>,
focus_index: usize, focus_index: usize,
focus_wrap_around: bool,
hint_alignment: &'a HintAlignment, hint_alignment: &'a HintAlignment,
rendering_colors: &'a ViewColors, rendering_colors: &'a ViewColors,
hint_style: Option<HintStyle>, hint_style: Option<HintStyle>,
@ -115,6 +116,7 @@ impl<'a> View<'a> {
pub fn new( pub fn new(
model: &'a mut model::Model<'a>, model: &'a mut model::Model<'a>,
unique_hint: bool, unique_hint: bool,
focus_wrap_around: bool,
hint_alignment: &'a HintAlignment, hint_alignment: &'a HintAlignment,
rendering_colors: &'a ViewColors, rendering_colors: &'a ViewColors,
hint_style: Option<HintStyle>, hint_style: Option<HintStyle>,
@ -128,6 +130,7 @@ impl<'a> View<'a> {
matches, matches,
lookup_trie, lookup_trie,
focus_index, focus_index,
focus_wrap_around,
hint_alignment, hint_alignment,
rendering_colors, rendering_colors,
hint_style, hint_style,
@ -136,15 +139,31 @@ impl<'a> View<'a> {
/// Move focus onto the previous hint. /// Move focus onto the previous hint.
pub fn prev(&mut self) { pub fn prev(&mut self) {
if self.focus_index > 0 { if self.focus_wrap_around {
self.focus_index -= 1; if self.focus_index == 0 {
self.focus_index = self.matches.len() - 1;
} else {
self.focus_index -= 1;
}
} else {
if self.focus_index > 0 {
self.focus_index -= 1;
}
} }
} }
/// Move focus onto the next hint. /// Move focus onto the next hint.
pub fn next(&mut self) { pub fn next(&mut self) {
if self.focus_index < self.matches.len() - 1 { if self.focus_wrap_around {
self.focus_index += 1; if self.focus_index == self.matches.len() - 1 {
self.focus_index = 0;
} else {
self.focus_index += 1;
}
} else {
if self.focus_index < self.matches.len() - 1 {
self.focus_index += 1;
}
} }
} }
@ -380,7 +399,6 @@ impl<'a> View<'a> {
/// # Panics /// # Panics
/// ///
/// - This function panics if termion cannot read the entered keys on stdin. /// - This function panics if termion cannot read the entered keys on stdin.
/// - This function also panics if the user types Insert on a line without hints.
fn listen(&mut self, reader: &mut dyn io::Read, writer: &mut dyn io::Write) -> Event { fn listen(&mut self, reader: &mut dyn io::Read, writer: &mut dyn io::Write) -> Event {
use termion::input::TermRead; // Trait for `reader.keys().next()`. use termion::input::TermRead; // Trait for `reader.keys().next()`.
@ -783,6 +801,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
matches: vec![], // no matches matches: vec![], // no matches
lookup_trie: SequenceTrie::new(), lookup_trie: SequenceTrie::new(),
focus_index: 0, focus_index: 0,
focus_wrap_around: false,
hint_alignment: &hint_alignment, hint_alignment: &hint_alignment,
rendering_colors: &rendering_colors, rendering_colors: &rendering_colors,
hint_style: None, hint_style: None,
@ -829,6 +848,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
let reverse = true; let reverse = true;
let mut model = model::Model::new(&lines, &alphabet, &named_pat, &custom_regexes, reverse); let mut model = model::Model::new(&lines, &alphabet, &named_pat, &custom_regexes, reverse);
let unique_hint = false; let unique_hint = false;
let wrap_around = false;
let rendering_colors = ViewColors { let rendering_colors = ViewColors {
text_fg: Box::new(color::Black), text_fg: Box::new(color::Black),
@ -846,6 +866,7 @@ Barcelona https://en.wikipedia.org/wiki/Barcelona - ";
let view = View::new( let view = View::new(
&mut model, &mut model,
unique_hint, unique_hint,
wrap_around,
&hint_alignment, &hint_alignment,
&rendering_colors, &rendering_colors,
hint_style, hint_style,