Skip to content

Commit

Permalink
Make clippy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Jan 14, 2023
1 parent 7d5488e commit 9bc846e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
7 changes: 2 additions & 5 deletions src/paging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ impl<'a> Paging<'a> {
// Subtract -2 because we need space to render the prompt, if paging is active
let capacity = max_capacity
.unwrap_or(std::usize::MAX)
.min(term_size.0 as usize)
// Safeguard in case term_size or max_length is 2 or less. Guarantees no unwanted wrapping behavior.
.max(3)
.clamp(3, term_size.0 as usize)
- 2;
let pages = (items_len as f64 / capacity as f64).ceil() as usize;

Expand All @@ -53,8 +51,7 @@ impl<'a> Paging<'a> {
self.capacity = self
.max_capacity
.unwrap_or(std::usize::MAX)
.min(self.current_term_size.0 as usize)
.max(3)
.clamp(3, self.current_term_size.0 as usize)
- 2;
self.pages = (self.items_len as f64 / self.capacity as f64).ceil() as usize;
}
Expand Down
22 changes: 14 additions & 8 deletions src/prompts/input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Debug, io, iter, str::FromStr};
use std::{cmp::Ordering, fmt::Debug, io, iter, str::FromStr};

#[cfg(feature = "completion")]
use crate::completion::Completion;
Expand All @@ -11,6 +11,8 @@ use crate::{

use console::{Key, Term};

type ValidatorCallback<'a, T> = Box<dyn FnMut(&T) -> Option<String> + 'a>;

/// Renders an input prompt.
///
/// ## Example usage
Expand Down Expand Up @@ -46,7 +48,7 @@ pub struct Input<'a, T> {
initial_text: Option<String>,
theme: &'a dyn Theme,
permit_empty: bool,
validator: Option<Box<dyn FnMut(&T) -> Option<String> + 'a>>,
validator: Option<ValidatorCallback<'a, T>>,
#[cfg(feature = "history")]
history: Option<&'a mut dyn History<T>>,
#[cfg(feature = "completion")]
Expand Down Expand Up @@ -386,7 +388,7 @@ where
let diff_pos_x = new_pos_x as i64 - old_pos_x as i64;
//println!("new_pos_x = {}, old_pos_x = {}, diff = {}", new_pos_x, old_pos_x, diff_pos_x);
if diff_pos_x < 0 {
term.move_cursor_left((diff_pos_x * -1) as usize)?;
term.move_cursor_left(-diff_pos_x as usize)?;
} else {
term.move_cursor_right((diff_pos_x) as usize)?;
}
Expand Down Expand Up @@ -419,7 +421,7 @@ where
let old_pos_x = (prompt_len + position) % line_size;
let diff_pos_x = new_pos_x as i64 - old_pos_x as i64;
if diff_pos_x < 0 {
term.move_cursor_left((diff_pos_x * -1) as usize)?;
term.move_cursor_left(-diff_pos_x as usize)?;
} else {
term.move_cursor_right((diff_pos_x) as usize)?;
}
Expand All @@ -432,10 +434,14 @@ where
let new_pos_x = (prompt_len + chars.len()) % line_size;
let old_pos_x = (prompt_len + position) % line_size;
let diff_pos_x = new_pos_x as i64 - old_pos_x as i64;
if diff_pos_x < 0 {
term.move_cursor_left((diff_pos_x * -1 - 1) as usize)?;
} else if diff_pos_x > 0 {
term.move_cursor_right((diff_pos_x) as usize)?;
match diff_pos_x.cmp(&0) {
Ordering::Less => {
term.move_cursor_left((-diff_pos_x - 1) as usize)?;
}
Ordering::Equal => {}
Ordering::Greater => {
term.move_cursor_right((diff_pos_x) as usize)?;
}
}
position = chars.len();
}
Expand Down

0 comments on commit 9bc846e

Please sign in to comment.