Skip to content

Commit

Permalink
Merge pull request #7 from jnises/nises/window-penalty
Browse files Browse the repository at this point in the history
Use window size to determine age penalty
  • Loading branch information
jnises authored Nov 25, 2023
2 parents a112b86 + 79a0ab0 commit 1c6ec06
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 37 deletions.
20 changes: 13 additions & 7 deletions .github/workflows/test.yml → .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test
name: CI

on:
push:
Expand All @@ -14,23 +14,29 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
- name: checkout
uses: actions/checkout@v2
- name: install toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
components: clippy, rustfmt
- uses: actions-rs/cargo@v1
- name: check
uses: actions-rs/cargo@v1
with:
command: check
- uses: actions-rs/cargo@v1
- name: test
uses: actions-rs/cargo@v1
with:
command: test
- uses: actions-rs/cargo@v1
- name: formatting
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
- uses: actions-rs/cargo@v1
- name: clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "colight"
version = "0.3.0"
version = "0.4.0"
edition = "2021"

[dependencies]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ Colight

![screenshot](docs/screenshot.webp)

Command line utility that colors text based on how compressible it is.
Command line utility that highlights text based on how compressible it is.
Compressibility is determined using a lz77-like algorithm.
The idea is that compressibility is a good indicator of whether a piece of text is interesting or not.

It only works in some terminals.
It only works in true-color terminals.

## Usage

Expand Down
44 changes: 18 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@ use crate::{
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// How long back in history to look for matches
/// How long back in history to look for matches.
///
/// The further back in history a match is found, the lower its compressibility score is.
#[arg(long, default_value_t = 1024)]
window_size: usize,

/// How much to penalize age of matches, higher values will make older matches less likely to be highlighted
#[arg(long, default_value_t = 0.01)]
age_penalty: f32,
}

fn main() -> anyhow::Result<()> {
let args = Args::parse();
assert!(args.window_size > 0);
assert!(args.age_penalty >= 0.0);
let stdin = std::io::stdin();
let si = stdin.lock();
let stdout = StandardStream::stdout(ColorChoice::Auto);
Expand All @@ -43,18 +40,17 @@ where
O: WriteColor,
I: Read,
{
let Args {
window_size,
age_penalty,
} = args;
let Args { window_size } = args;
// refcell so we can reset on scope exit
let soc = RefCell::new(so);
defer! {
soc.borrow_mut().reset().unwrap();
}
let pr = |buffered: VecDeque<u8>, age: Option<usize>| -> anyhow::Result<()> {
let pr = |buffered: VecDeque<u8>, score: f32| -> anyhow::Result<()> {
// score of 1 means buffered is uncompressible, 0 means it is fully compressible
debug_assert!((0.0..=1.0).contains(&score));
if !buffered.is_empty() {
let score = 1f32 / (buffered.len() as f32 + age.unwrap_or(0) as f32 * age_penalty);
//let score = 1f32 / (buffered.len() as f32 + age.unwrap_or(0) as f32 * age_penalty);
let (a, b) = buffered.as_slices();
for line in a
.split_inclusive(|&c| c == b'\n')
Expand All @@ -73,17 +69,19 @@ where
loop {
let mut byte_buf = [0; 1];
if color_stripper.read_exact(&mut byte_buf).is_err() {
pr(searcher.flush(), None)?;
pr(searcher.flush(), 0.0)?;
break;
};
// keep going until we find no more matches
match searcher.search(byte_buf[0]) {
SearchState::Buffering => {}
SearchState::Flushed {
buffer: last_found_needle,
age,
} => {
pr(last_found_needle, age)?;
SearchState::Flushed { buffer, age } => {
let score = 1.0
/ 1f32.max(
buffer.len() as f32
/ (1.0 - (age.unwrap_or(0) as f32 / window_size as f32)),
);
pr(buffer, score)?;
}
}
}
Expand Down Expand Up @@ -133,10 +131,7 @@ mod tests {
[Mon Mar 1 09:20:01 CET 2021] start new app: /Applications/app.app",
),
&mut s,
Args {
window_size: 1024,
age_penalty: 0.0,
},
Args { window_size: 1024 },
)
.unwrap();
}
Expand All @@ -153,10 +148,7 @@ ab
",
),
&mut s,
Args {
window_size: 1024,
age_penalty: 0.0,
},
Args { window_size: 1024 },
)
.unwrap();
}
Expand Down

0 comments on commit 1c6ec06

Please sign in to comment.