Skip to content

Commit

Permalink
Add environment variable to customize context size (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
parasyte authored Feb 24, 2025
1 parent a0d8707 commit a5bd545
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
//! all truncation, otherwise it sets the maximum number of characters before truncation
//! kicks in.
//!
//! # Context Size
//!
//! Diffs displayed by assertions have a default context size of 4 (show up to 4 lines above and
//! below changes). This can be changed with the `SIMILAR_ASSERTS_CONTEXT_SIZE` environment
//! variable.
//!
//! # Manual Diff Printing
//!
//! If you want to build your own comparison macros and you need a quick and simple
Expand All @@ -72,6 +78,7 @@
//! ```
use std::borrow::Cow;
use std::fmt::{self, Display};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

use console::{style, Style};
Expand All @@ -87,17 +94,27 @@ pub mod print;

/// The maximum number of characters a string can be long before truncating.
fn get_max_string_length() -> usize {
use std::sync::atomic::{AtomicUsize, Ordering};
static TRUNCATE: AtomicUsize = AtomicUsize::new(!0);
let rv = TRUNCATE.load(Ordering::Relaxed);
get_usize_from_env(&TRUNCATE, "SIMILAR_ASSERTS_MAX_STRING_LENGTH", 200)
}

/// The context size for diff groups.
fn get_context_size() -> usize {
static CONTEXT_SIZE: AtomicUsize = AtomicUsize::new(!0);
get_usize_from_env(&CONTEXT_SIZE, "SIMILAR_ASSERTS_CONTEXT_SIZE", 4)
}

/// Parse a `usize` value from an environment variable, cached in a static atomic.
fn get_usize_from_env(value: &'static AtomicUsize, var: &str, default: usize) -> usize {
let rv = value.load(Ordering::Relaxed);
if rv != !0 {
return rv;
}
let rv: usize = std::env::var("SIMILAR_ASSERTS_MAX_STRING_LENGTH")
let rv: usize = std::env::var(var)
.ok()
.and_then(|x| x.parse().ok())
.unwrap_or(200);
TRUNCATE.store(rv, Ordering::Relaxed);
.unwrap_or(default);
value.store(rv, Ordering::Relaxed);
rv
}

Expand Down Expand Up @@ -318,7 +335,7 @@ impl fmt::Display for SimpleDiff<'_> {
style("+").green().dim(),
style(self.right_label).green(),
)?;
for (idx, group) in diff.grouped_ops(4).into_iter().enumerate() {
for (idx, group) in diff.grouped_ops(get_context_size()).into_iter().enumerate() {
if idx > 0 {
writeln!(f, "@ {}", style("~~~").dim())?;
}
Expand Down

0 comments on commit a5bd545

Please sign in to comment.