diff options
Diffstat (limited to 'srchr/src/layout.rs')
| -rw-r--r-- | srchr/src/layout.rs | 156 |
1 files changed, 4 insertions, 152 deletions
diff --git a/srchr/src/layout.rs b/srchr/src/layout.rs index 9662b84..d30e38b 100644 --- a/srchr/src/layout.rs +++ b/srchr/src/layout.rs @@ -1,34 +1,9 @@ +use crate::config::*; use crate::corpus::*; - +use crate::output::*; use rand::prelude::*; use std::fmt; -pub const NUM_KEYS: usize = 30; -pub const ROW_LENGTH: usize = 10; - -pub const KEY_CHARS: [char; NUM_KEYS] = [ - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', - 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '/', '.', ',', '\'', -]; - -pub fn canonicalise(inp: char) -> Option<char> { - if inp.is_ascii_alphabetic() { - return Some(inp.to_ascii_uppercase()); - } else { - match inp { - '.' => Some('.'), - '>' => Some('.'), - ',' => Some(','), - '<' => Some(','), - '/' => Some('/'), - '?' => Some('/'), - '\'' => Some('\''), - '"' => Some('\''), - _ => None, - } - } -} - #[derive(Copy, Clone)] pub struct Prelayout { standard_columns: [[char; 3]; 6], @@ -123,10 +98,6 @@ impl Prelayout { } } -const KEY_TO_FINGER: [usize; NUM_KEYS] = [ - 0, 1, 2, 3, 3, 4, 4, 5, 6, 7, 0, 1, 2, 3, 3, 4, 4, 5, 6, 7, 0, 1, 2, 3, 3, 4, 4, 5, 6, 7, -]; - #[derive(Copy, Clone)] pub struct Layout { keys: [char; NUM_KEYS], @@ -202,12 +173,8 @@ impl Layout { Layout { keys } } - pub fn get_index(&self, c: char) -> usize { - let mut found_key = 0; - while self.keys[found_key] != c { - found_key += 1 - } - found_key + pub fn get_keys(&self) -> &[char] { + &self.keys } fn char_array_to_layout(keys: [char; NUM_KEYS]) -> Layout { @@ -246,118 +213,3 @@ impl fmt::Display for Layout { formatter.write_str(&format_block_output(self.keys.into_iter())) } } - -#[derive(Copy, Clone)] -pub struct Evaluation { - keypress: [u32; NUM_KEYS], - total_keypress: u32, - sfb: [u32; 8], -} - -impl Evaluation { - pub fn new(keypress: [u32; NUM_KEYS], total_keypress: u32, sfb: [u32; 8]) -> Evaluation { - Evaluation { - keypress, - sfb, - total_keypress, - } - } - - fn output_eval(&self) -> String { - let mut result = String::new(); - let tot = self.total_keypress as f32; - - result += &"Percent per key:\n"; - result += &format_block_output(self.keypress.into_iter().map(|k| 100.0 * k as f32 / tot)); - - let mut finger_usages = [0; 8]; - for (i, &c) in self.keypress.iter().enumerate() { - finger_usages[KEY_TO_FINGER[i]] += c; - } - - result += "\nFinger usage: "; - let mut lh: f32 = 0.0; - let mut rh: f32 = 0.0; - for (i, &u) in finger_usages.iter().enumerate() { - let f = u as f32 / tot * 100.0; - result += &format!("{:>5.2}%{}", f, if i < 7 { ", " } else { "" }); - if i % 10 < 4 { - lh += f; - } else { - rh += f; - } - } - - result += &format!("\nHand usage: {:.2}% vs {:.2}%", lh, rh); - - result += "\nSame finger bigrams: "; - for (i, &u) in self.sfb.iter().enumerate() { - result += &format!( - "{:>6.3}%{}", - u as f32 / tot * 100.0, - if i < 7 { ", " } else { "" } - ); - } - - let sfb = self.sfb.iter().sum::<u32>(); - result += &format!("\nTotal sfb: {:.2}% ({})", sfb as f32 / tot * 100.0, sfb); - - return result; - } -} - -impl fmt::Display for Evaluation { - fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str(&self.output_eval()) - } -} - -// This is a silly amount of work to genericise the below... -trait ToMyString { - const BLANK_STRING: &'static str; - fn to_my_string(&self) -> String; -} - -impl ToMyString for char { - const BLANK_STRING: &'static str = " "; - fn to_my_string(&self) -> String { - self.to_string() - } -} - -impl ToMyString for u32 { - const BLANK_STRING: &'static str = " "; - fn to_my_string(&self) -> String { - format!("{:5}", *self) - } -} - -impl ToMyString for f32 { - const BLANK_STRING: &'static str = " "; - fn to_my_string(&self) -> String { - format!("{:>4.1}", *self) - } -} - -fn format_block_output<T: Iterator<Item = S>, S: ToMyString>(things: T) -> String { - let mut result = String::new(); - for (i, t) in things.enumerate() { - result += &t.to_my_string(); - if i < NUM_KEYS - 1 { - result.push(' '); - } - if (i + 1) % 10 == 0 { - result.push('\n'); - if NUM_KEYS < 30 && i == 19 { - result += S::BLANK_STRING; - result.push(' ') - } - } else if (i < 20 && (i + 1) % 5 == 0) || (i == (NUM_KEYS - 20) / 2 + 19) { - result.push(' '); - } - if i + 1 >= NUM_KEYS { - break; - } - } - return result; -} |
