Merge pull request #518 from kdheepak/support-multi-byte-chars

Support multi byte chars for key config
This commit is contained in:
Dheepak Krishnamurthy 2023-08-29 03:21:30 -04:00 committed by GitHub
commit 77b755ce70
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,7 @@
use std::{collections::HashSet, error::Error, hash::Hash}; use std::{collections::HashSet, error::Error, hash::Hash};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use log::{error, info, warn};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::event::KeyCode; use crate::event::KeyCode;
@ -198,13 +199,17 @@ impl KeyConfig {
for line in data.split('\n') { for line in data.split('\n') {
if line.starts_with(config) { if line.starts_with(config) {
let line = line.trim_start_matches(config).trim_start().trim_end().to_string(); let line = line.trim_start_matches(config).trim_start().trim_end().to_string();
if line.len() == 1 { if has_just_one_char(&line) {
return Some(KeyCode::Char(line.chars().next().unwrap())); return Some(KeyCode::Char(line.chars().next().unwrap()));
} else {
error!("Found multiple characters in {} for {}", line, config);
} }
} else if line.starts_with(&config.replace('-', "_")) { } else if line.starts_with(&config.replace('-', "_")) {
let line = line.trim_start_matches(&config.replace('-', "_")).trim_start().trim_end().to_string(); let line = line.trim_start_matches(&config.replace('-', "_")).trim_start().trim_end().to_string();
if line.len() == 1 { if has_just_one_char(&line) {
return Some(KeyCode::Char(line.chars().next().unwrap())); return Some(KeyCode::Char(line.chars().next().unwrap()));
} else {
error!("Found multiple characters in {} for {}", line, config);
} }
} }
} }
@ -212,6 +217,11 @@ impl KeyConfig {
} }
} }
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::*;