refactor: follow clippy suggestions

This commit is contained in:
graelo 2021-03-13 18:51:31 +01:00
parent 7e28931f6c
commit ffd8e9b12d
7 changed files with 38 additions and 69 deletions

View file

@ -6,7 +6,7 @@ use crate::error;
///
/// Keep in mind letters 'n' and 'y' are systematically removed at runtime to
/// prevent conflict with navigation and yank/copy keys.
const ALPHABETS: [(&'static str, &'static str); 21] = [
const ALPHABETS: [(&str, &str); 21] = [
// ("abcd", "abcd"),
("qwerty", "asdfqwerzxcvjklmiuopghtybn"),
("qwerty-homerow", "asdfjklgh"),
@ -47,7 +47,7 @@ pub fn parse_alphabet(src: &str) -> Result<Alphabet, error::ParseError> {
match alphabet_pair {
Some((_name, letters)) => {
let letters = letters.replace(&['n', 'N', 'y', 'Y'][..], "");
Ok(Alphabet(letters.to_string()))
Ok(Alphabet(letters))
}
None => Err(error::ParseError::UnknownAlphabet),
}

View file

@ -44,12 +44,9 @@ impl BridgeOpt {
options: &HashMap<String, String>,
) -> Result<(), error::ParseError> {
for (name, value) in options {
match name.as_ref() {
"@copyrat-capture" => {
if let "@copyrat-capture" = name.as_ref() {
self.capture_region = tmux::CaptureRegion::from_str(&value)?;
}
_ => (),
}
}
// Pass the call to cli_options.
@ -94,41 +91,17 @@ fn main() -> Result<(), error::ParseError> {
// buffer if it was uppercased.
// TODO: consider getting rid of multi-selection mode.
// Execute a command on each group of selections (normal and uppercased).
let (normal_selections, uppercased_selections): (Vec<(String, bool)>, Vec<(String, bool)>) =
selections
.into_iter()
.partition(|(_text, uppercased)| !*uppercased);
let buffer_selections: String = normal_selections
.into_iter()
.map(|(text, _)| text)
.collect::<Vec<_>>()
.join("\n");
if buffer_selections.len() > 0 {
let args = vec!["set-buffer", &buffer_selections];
// Simply execute the command as is, and let the program crash on
// potential errors because it is not our responsibility.
process::execute("tmux", &args).unwrap();
}
let buffer_selections: String = uppercased_selections
.into_iter()
.map(|(text, _)| text)
.collect::<Vec<_>>()
.join("\n");
if buffer_selections.len() > 0 {
let args = vec!["set-buffer", &buffer_selections];
// Simply execute the command as is, and let the program crash on
// potential errors because it is not our responsibility.
process::execute("tmux", &args).unwrap();
match selections {
None => return Ok(()),
Some((text, uppercased)) => {
let args = vec!["set-buffer", &text];
process::execute("tmux", &args)?;
if uppercased {
let args = vec!["paste-buffer", "-t", active_pane.id.as_str()];
// Simply execute the command as is, and let the program crash on
// potential errors because it is not our responsibility.
process::execute("tmux", &args).unwrap();
process::execute("tmux", &args)?;
}
}
}
Ok(())

View file

@ -16,7 +16,7 @@ fn main() {
handle.read_to_string(&mut buffer).unwrap();
// Execute copyrat over the buffer (will take control over stdout).
// This returns the selected matches.
// This returns the selected matche.
let selection: Option<(String, bool)> = run(buffer, &opt);
// Early exit, signaling no selections were found.
@ -37,7 +37,7 @@ fn main() {
.open(target)
.expect("Unable to open the target file");
file.write(text.as_bytes()).unwrap();
file.write_all(text.as_bytes()).unwrap();
}
}
}

View file

@ -12,8 +12,8 @@ pub struct Model<'a> {
pub lines: Vec<&'a str>,
alphabet: &'a Alphabet,
use_all_patterns: bool,
named_patterns: &'a Vec<NamedPattern>,
custom_patterns: &'a Vec<String>,
named_patterns: &'a [NamedPattern],
custom_patterns: &'a [String],
pub reverse: bool,
}
@ -22,8 +22,8 @@ impl<'a> Model<'a> {
buffer: &'a str,
alphabet: &'a Alphabet,
use_all_patterns: bool,
named_patterns: &'a Vec<NamedPattern>,
custom_patterns: &'a Vec<String>,
named_patterns: &'a [NamedPattern],
custom_patterns: &'a [String],
reverse: bool,
) -> Model<'a> {
let lines = buffer.split('\n').collect();
@ -112,7 +112,7 @@ impl<'a> Model<'a> {
loop {
let chunk_matches = all_regexes
.iter()
.filter_map(|(&ref name, regex)| match regex.find_iter(chunk).nth(0) {
.filter_map(|(&ref name, regex)| match regex.find_iter(chunk).next() {
Some(m) => Some((name, regex, m)),
None => None,
})
@ -164,7 +164,7 @@ impl<'a> Model<'a> {
/// If `unique` is `true`, all duplicate matches will have the same hint.
/// For copying matched text, this seems easier and more natural.
/// If `unique` is `false`, duplicate matches will have their own hint.
fn associate_hints(&self, raw_matches: &Vec<RawMatch<'a>>, unique: bool) -> Vec<Match<'a>> {
fn associate_hints(&self, raw_matches: &[RawMatch<'a>], unique: bool) -> Vec<Match<'a>> {
let hints = self.alphabet.make_hints(raw_matches.len());
let mut hints_iter = hints.iter();
@ -175,11 +175,11 @@ impl<'a> Model<'a> {
let mut known: collections::HashMap<&str, &str> = collections::HashMap::new();
for raw_mat in raw_matches {
let hint: &str = known.entry(raw_mat.text).or_insert(
let hint: &str = known.entry(raw_mat.text).or_insert_with(|| {
hints_iter
.next()
.expect("We should have as many hints as necessary, even invisible ones."),
);
.expect("We should have as many hints as necessary, even invisible ones.")
});
result.push(Match {
x: raw_mat.x,
@ -211,7 +211,7 @@ impl<'a> Model<'a> {
/// Builds a `SequenceTrie` that helps determine if a sequence of keys
/// entered by the user corresponds to a match. This kind of lookup
/// directly returns a reference to the corresponding `Match` if any.
pub fn build_lookup_trie(matches: &'a Vec<Match<'a>>) -> SequenceTrie<char, usize> {
pub fn build_lookup_trie(matches: &'a [Match<'a>]) -> SequenceTrie<char, usize> {
let mut trie = SequenceTrie::new();
for (index, mat) in matches.iter().enumerate() {

View file

@ -4,7 +4,7 @@ use crate::error::ParseError;
/// Execute an arbitrary Unix command and return the stdout as a `String` if
/// successful.
pub fn execute(command: &str, args: &Vec<&str>) -> Result<String, ParseError> {
pub fn execute(command: &str, args: &[&str]) -> Result<String, ParseError> {
let output = Command::new(command).args(args).output()?;
if !output.status.success() {

View file

@ -1,13 +1,13 @@
use crate::error;
pub const EXCLUDE_PATTERNS: [(&'static str, &'static str); 1] =
pub const EXCLUDE_PATTERNS: [(&str, &str); 1] =
[("ansi_colors", r"[[:cntrl:]]\[([0-9]{1,2};)?([0-9]{1,2})?m")];
/// Holds all the regex patterns that are currently supported.
///
/// The email address was obtained at https://www.regular-expressions.info/email.html.
/// Others were obtained from Ferran Basora.
pub const PATTERNS: [(&'static str, &'static str); 15] = [
pub const PATTERNS: [(&str, &str); 15] = [
("markdown-url", r"\[[^]]*\]\(([^)]+)\)"),
(
"url",

View file

@ -82,11 +82,9 @@ impl<'a> Ui<'a> {
} else {
self.focus_index -= 1;
}
} else {
if self.focus_index > 0 {
} else if self.focus_index > 0 {
self.focus_index -= 1;
}
}
let new_index = self.focus_index;
(old_index, new_index)
}
@ -101,11 +99,9 @@ impl<'a> Ui<'a> {
} else {
self.focus_index += 1;
}
} else {
if self.focus_index < self.matches.len() - 1 {
} else if self.focus_index < self.matches.len() - 1 {
self.focus_index += 1;
}
}
let new_index = self.focus_index;
(old_index, new_index)
}
@ -137,10 +133,10 @@ impl<'a> Ui<'a> {
/// - This writes directly on the writer, avoiding extra allocation.
fn render_base_text(
stdout: &mut dyn io::Write,
lines: &Vec<&str>,
line_offsets: &Vec<usize>,
lines: &[&str],
line_offsets: &[usize],
colors: &UiColors,
) -> () {
) {
write!(
stdout,
"{bg_color}{fg_color}",
@ -361,7 +357,7 @@ impl<'a> Ui<'a> {
/// # Note
/// Multibyte characters are taken into account, so that the Match's `text`
/// and `hint` are rendered in their proper position.
fn full_render(&self, stdout: &mut dyn io::Write) -> () {
fn full_render(&self, stdout: &mut dyn io::Write) {
// 1. Trim all lines and render non-empty ones.
Ui::render_base_text(
stdout,
@ -557,7 +553,7 @@ impl<'a> Ui<'a> {
/// Compute each line's actual y offset if displayed in a terminal of width
/// `term_width`.
fn get_line_offsets(lines: &Vec<&str>, term_width: u16) -> Vec<usize> {
fn get_line_offsets(lines: &[&str], term_width: u16) -> Vec<usize> {
lines
.iter()
.scan(0, |offset, &line| {