Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 18 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@ readme = "README.md"
repository = "https://github.com/thatvegandev/fsrx.git"
homepage = "https://github.com/thatvegandev/fsrx"
license = "MIT"
authors = ["Colby Thomas <thatvegandev@gmail.com>"]
authors = ["Colby Thomas <thatvegandev@gmail.com>", "John Warren <john@thebestjohn.com>"]
exclude = [".github/*"]
keywords = ["reading", "utility", "terminal", "cli", "fast"]
default_features = ["cli"]

[features]
default = ["cli"]
cli = ["clap", "atty"]

[lib]
name = "fsrx_lib"
path = "src/lib.rs"

[[bin]]
name = "fsrx"
path = "src/main.rs"
required-features = ["cli"]

[dependencies]
clap = { version = "3.2.19", features = ["derive"], optional = true}
atty = { version = "0.2.14", optional = true}
ansi_term = "0.12.1"
regex = "1.6.0"
unicode-segmentation = "1.9.0"
clap = { version = "3.2.8", features = ["derive"] }
atty = "0.2.14"
unicode-segmentation = "1.9.0"
96 changes: 96 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use ansi_term::Style;
use regex::Regex;
use unicode_segmentation::UnicodeSegmentation;

#[derive(Clone, Debug)]
pub struct FSRXStyler {
pub fixation: f32,
pub contrast: bool,
pub saccade_mode: usize,
saccade_iter: i32,
re: Regex,
}

pub enum Intensity {
L, // low
M, // medium
H, // high
}

impl FSRXStyler {
pub fn new(fixation: Intensity, contrast: bool, saccade_mode: Intensity) -> FSRXStyler {
let reg = Regex::new(r"[\w\\']+").unwrap();
let mode = match saccade_mode {
Intensity::H => 1,
Intensity::M => 2,
Intensity::L => 3,
};
let fix = match fixation {
Intensity::H => 0.7,
Intensity::M => 0.5,
Intensity::L => 0.3,
};
FSRXStyler {
fixation: fix,
contrast,
saccade_mode: mode,
saccade_iter: -1,
re: reg,
}
}
pub fn style_line(&mut self, unstyled_line: &str) -> String {
let mut last_end_idx: usize = 0;
let mut styled_line = "".to_owned();

for reg_match in self.re.find_iter(unstyled_line) {
let start_idx = reg_match.start();
let end_idx = reg_match.end();
styled_line.push_str(
self.style_substr(&unstyled_line[last_end_idx..start_idx], false, false)
.as_str(),
);

self.saccade_iter += 1;
// println!("{:?}", &self.saccade_iter);
let saccade_hit = self.saccade_iter % (self.saccade_mode as i32) == 0;
styled_line.push_str(
self.style_substr(&unstyled_line[start_idx..end_idx], true, saccade_hit)
.as_str(),
);
last_end_idx = end_idx;
}
styled_line.push_str(
self.style_substr(
&unstyled_line[last_end_idx..unstyled_line.len()],
false,
false,
)
.as_str(),
);
styled_line
}

fn style_substr(&self, substr: &str, should_process: bool, saccade_hit: bool) -> String {
let mid_point = (substr.len() as f32 * self.fixation).ceil() as usize;
let styled_text = UnicodeSegmentation::graphemes(substr, true)
.collect::<Vec<&str>>()
.iter()
.enumerate()
.map(|(i, x)| -> String {
let curr_char = x.to_owned();
if i < mid_point && saccade_hit && should_process {
if self.contrast {
format!("{}", Style::new().bold().paint(curr_char))
} else {
String::from(curr_char)
}
} else {
format!("{}", Style::new().dimmed().paint(curr_char))
}
})
.collect::<Vec<String>>()
.join("");

styled_text
}
}
Loading