Skip to content

Commit

Permalink
Added supporting non ascii symbols (#12) (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
WindowGenerator authored Oct 14, 2023
1 parent da16056 commit f12c6f4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Args {
/// indicates if test should include numbers
#[arg(short, long)]
pub numbers: Option<bool>,

/// numbers-ratio argument
#[arg(long)]
pub numbers_ratio: Option<f64>,
Expand All @@ -30,5 +30,4 @@ pub struct Args {
/// uppercase-ratio argument
#[arg(long)]
pub uppercase_ratio: Option<f64>,

}
16 changes: 12 additions & 4 deletions src/expected_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rand::{seq::SliceRandom, thread_rng, Rng};
use std::io::Read;

use crate::config::Config;
use crate::helpers::split_by_char_index;

/// Struct used by runner to hold generate the text used for validation and as a placeholder
#[derive(Debug)]
Expand Down Expand Up @@ -91,7 +92,6 @@ fn replace_words_with_numbers(
}

fn create_uppercase_words(string_vec: &mut Vec<String>, pos: usize, uppercase_ratio: f64) {
// let mut string_vec2 = string_vec.clone();
let num_uppercase_words = (uppercase_ratio * string_vec[pos..].len() as f64).round() as usize;
for i in pos..pos + num_uppercase_words {
if string_vec[i] != "" {
Expand All @@ -116,8 +116,8 @@ impl ExpectedInputInterface for ExpectedInput {
/// enough.
fn get_string(&self, len: usize) -> String {
let s = self.str.clone() + " ";
let s = s.repeat(len / s.len() + 1);
let (s, _) = s.split_at(len);
let s = s.repeat((len / s.chars().count()) as usize + 1);
let (s, _) = split_by_char_index(&s, len);

s.to_string()
}
Expand All @@ -135,7 +135,7 @@ mod tests {
let config = Config::default();
let expected_input = ExpectedInput::new(&config).expect("unable to create expected input");

assert_eq!(expected_input.get_string(12).len(), 12);
assert_eq!(expected_input.get_string(12).chars().count(), 12);
}

#[test]
Expand Down Expand Up @@ -207,4 +207,12 @@ mod tests {
"At least 4 items contain only digits"
);
}
#[test]
fn should_work_with_non_ascii_chars() {
let expected_input = ExpectedInput {
str: "Բարեւ Ձեզ".to_string(),
};

assert_eq!(expected_input.get_string(5), "Բարեւ");
}
}
19 changes: 19 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub fn split_by_char_index(string: &str, char_index: usize) -> (&str, &str) {
string
.char_indices()
.nth(char_index)
.map(|(index, _)| (&string[..index], &string[index..]))
.unwrap_or((string, ""))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn should_work_with_non_ascii_chars() {
let (first_part, second_part) = split_by_char_index("Բարեւ Ձեզ", 5);

assert_eq!((first_part, second_part), ("Բարեւ", " Ձեզ"));
}
}
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@
//! ```

mod args;
mod config;
mod color_scheme;
mod config;
mod expected_input;
mod helpers;
mod runner;

use anyhow::{Context, Result};
Expand All @@ -63,8 +64,8 @@ use crossterm::{
self, disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
},
};
use std::io;
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io;

use args::Args;
use config::Config;
Expand Down
32 changes: 19 additions & 13 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use ratatui::{

use crate::config::Config;
use crate::expected_input::ExpectedInputInterface;
use crate::helpers::split_by_char_index;

/// To switch from Normal to Editing press `e`.
/// To switch from Editing to Normal press `<Esc>`.
Expand Down Expand Up @@ -205,23 +206,28 @@ impl Runner {
let input_area = areas[0];
let info_area = areas[1];

let frame_width = frame.size().width as usize;
let input_len = self.input.len();
let current_line_index = (input_len / frame_width) as u16;
let input_current_line_len = input_len % frame_width;
let frame_width: usize = frame.size().width as usize;
let input_chars_count: usize = self.input.chars().count();
let current_line_index = (input_chars_count / frame_width) as u16;
let input_current_line_len = input_chars_count % frame_width;

let expected_input_str = self
.expected_input
.get_string((current_line_index as usize + 2) * frame_width);
let (expected_input_current_line, expected_input_following_lines) =
expected_input_str.split_at(((current_line_index as usize) + 1) * frame_width);

let (expected_input_current_line, expected_input_following_lines) = split_by_char_index(
&expected_input_str,
((current_line_index as usize) + 1) * frame_width,
);

let (expected_input_current_line_already_typed, expected_input_current_line_rest) =
expected_input_current_line.split_at(input_len);
split_by_char_index(&expected_input_current_line, input_chars_count);

let expected_input_str = expected_input_current_line_already_typed.to_string()
+ expected_input_current_line_rest
+ expected_input_following_lines;

self.print_input(frame, expected_input_str, input_area, frame_width);
self.print_input(frame, &expected_input_str, input_area, frame_width);

self.print_block_of_text(
frame,
Expand Down Expand Up @@ -299,14 +305,14 @@ impl Runner {
fn print_input(
&mut self,
frame: &mut impl FrameWrapperInterface,
expected_input: String,
expected_input: &str,
input_area: Rect,
frame_width: usize,
) {
for ((input_char_index, input_char), (_, expected_input_char)) in
self.input.char_indices().zip(expected_input.char_indices())
for ((input_char_index, input_char), expected_input_char) in
self.input.chars().enumerate().zip(expected_input.chars())
{
let input = Paragraph::new(expected_input_char.to_string()).style(
let input: Paragraph<'_> = Paragraph::new(input_char.to_string()).style(
match input_char == expected_input_char {
true => Style::default()
.bg(self.config.colors.correct_match_bg)
Expand Down Expand Up @@ -561,7 +567,7 @@ mod test {

runner.print_input(
&mut frame,
"foo".to_string(),
"foo",
Rect {
x: 0,
y: 0,
Expand Down

0 comments on commit f12c6f4

Please sign in to comment.