Skip to content

Commit

Permalink
feat: implement scroll up and down indicators for Text prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelmello committed Aug 21, 2021
1 parent 949d842 commit fa9bc57
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
10 changes: 6 additions & 4 deletions src/prompts/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,16 @@ impl<'a> TextPrompt<'a> {
.map(|(i, val)| OptionAnswer::new(i, val))
.collect::<Vec<OptionAnswer>>();

let display_cursor = self.cursor_index > 0;
let list_index = self.cursor_index.saturating_sub(1);
let page = paginate(self.page_size, &choices, list_index);
let mut page = paginate(self.page_size, &choices, list_index);

for (idx, opt) in page.content.iter().enumerate() {
backend.render_suggestion(&opt.value, display_cursor && page.selection == idx)?;
let cursor_on_input = self.cursor_index == 0;
if cursor_on_input {
page.selection = usize::MAX;
}

backend.render_suggestions(page)?;

if let Some(message) = self.help_message {
backend.render_help_message(message)?;
} else if !choices.is_empty() {
Expand Down
22 changes: 9 additions & 13 deletions src/ui/backend.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashSet, fmt::Display, io::Result};
use std::{collections::HashSet, io::Result};

use super::{key::Key, RenderConfig, Terminal};
use crate::{input::Input, option_answer::OptionAnswer, ui::Styled, utils::Page};
Expand All @@ -22,7 +22,7 @@ pub trait TextBackend: CommonBackend {
default: Option<&str>,
cur_input: &Input,
) -> Result<()>;
fn render_suggestion<T: Display>(&mut self, content: T, focused: bool) -> Result<()>;
fn render_suggestions(&mut self, page: Page<OptionAnswer>) -> Result<()>;
}

pub trait SelectBackend: CommonBackend {
Expand Down Expand Up @@ -304,20 +304,16 @@ where
self.print_prompt_with_input(prompt, default, cur_input)
}

fn render_suggestion<D: Display>(&mut self, content: D, focused: bool) -> Result<()> {
match focused {
true => self
.terminal
.write_styled(&self.render_config.highlighted_option_prefix)?,
false => self.terminal.write(' ')?,
}
fn render_suggestions(&mut self, page: Page<OptionAnswer>) -> Result<()> {
for (idx, option) in page.content.iter().enumerate() {
self.print_option_prefix(idx, &page)?;

self.terminal.write(' ')?;
self.terminal.write(' ')?;

self.terminal
.write_styled(&Styled::new(content).with_style_sheet(self.render_config.option))?;
self.print_option_value(option)?;

self.new_line()?;
self.new_line()?;
}

Ok(())
}
Expand Down

0 comments on commit fa9bc57

Please sign in to comment.