mirror of
https://github.com/kdheepak/taskwarrior-tui.git
synced 2025-08-24 14:36:42 +02:00
refactor: Rewrote get_config to lessen code duplication
This commit is contained in:
parent
f9660bb256
commit
2443d0f7ff
1 changed files with 29 additions and 18 deletions
|
@ -210,31 +210,42 @@ impl KeyConfig {
|
||||||
|
|
||||||
fn get_config(config: &str, data: &str) -> Option<KeyCode> {
|
fn get_config(config: &str, data: &str) -> Option<KeyCode> {
|
||||||
for line in data.split('\n') {
|
for line in data.split('\n') {
|
||||||
if line.starts_with(config) {
|
// Provide leeway for swapped - and _ in keyconfigs
|
||||||
let line = line.trim_start_matches(config).trim_start().trim_end().to_string();
|
let config_variants = vec![config.to_owned(), config.replace('-', "_")];
|
||||||
if has_just_one_char(&line) {
|
|
||||||
return Some(KeyCode::Char(line.chars().next().unwrap()));
|
for config in &config_variants {
|
||||||
} else {
|
let trimmed_line = line
|
||||||
error!("Found multiple characters in {} for {}", line, config);
|
.trim_start_matches(config)
|
||||||
|
.trim_start()
|
||||||
|
.trim_start_matches('=')
|
||||||
|
.trim_end()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
let chars: Vec<char> = trimmed_line.chars().collect();
|
||||||
|
|
||||||
|
match chars.len() {
|
||||||
|
0 => debug!("Found no override key for {}", config),
|
||||||
|
1 => {
|
||||||
|
let key_char = chars.first();
|
||||||
|
match key_char {
|
||||||
|
Some(key_char) => return Some(KeyCode::Char(*key_char)),
|
||||||
|
None => error!("Encountered impossible error. Could not fetch first character in Vector of length 1"),
|
||||||
}
|
}
|
||||||
} else if line.starts_with(&config.replace('-', "_")) {
|
}
|
||||||
let line = line.trim_start_matches(&config.replace('-', "_")).trim_start().trim_end().to_string();
|
_ => error!(
|
||||||
if has_just_one_char(&line) {
|
"Found multiple characters({}) in {} for {}, instead of the expected 1",
|
||||||
return Some(KeyCode::Char(line.chars().next().unwrap()));
|
chars.len(),
|
||||||
} else {
|
line,
|
||||||
error!("Found multiple characters in {} for {}", line, config);
|
config
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_just_one_char(s: &str) -> bool {
|
|
||||||
let mut chars = s.chars();
|
|
||||||
chars.next().is_some() && chars.next().is_none()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue