Skip to content

Commit

Permalink
real bump yansi to v1.0+, guard in feature "cli"
Browse files Browse the repository at this point in the history
  • Loading branch information
mhfan committed May 1, 2024
1 parent 11d9df1 commit cfc9fef
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ rand = "0.8"
#num = "0.4" # rational/complex/bigint/integer/traits etc.
num-traits = "0.2"
#itertools = "0.10"
#yansi = { version = "1.0", features = [ "detect-tty", "detect-env" ] }
yansi = "0.5" # XXX: v1.0+ currently doesn't work on wasm
atty = "0.2"
yansi = { version = "1.0", features = [ "detect-tty", "detect-env" ], optional = true }
#atty = "0.2"

pest = "2.7"
pest_derive = "2.7"
Expand Down Expand Up @@ -78,8 +77,9 @@ chrono = "0.4"

[features]
debug = []
cli = [ "yansi" ]
cxx = [ "dep:cxx", "cxx-build" ] # implied by optional dependency
default = [ "cc", "ahash" ] # "gxhash", "cxx", "serde", "num-rational", "num-bigint",
default = [ "cc", "cli", "ahash" ] # "gxhash", "cxx", "serde", "num-rational", "num-bigint",
dhat-heap = [ "dhat" ]

[[bench]]
Expand Down
25 changes: 14 additions & 11 deletions src/calc24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,10 @@ pub use Calc24Algo::*;
}
}

#[inline] fn deck_traverse(min: i32, max: i32, cnt: u8, mrpt: u8,
nums: &mut Vec<i32>, solve: &mut impl FnMut(&[i32])) {
//#[cfg(feature = "cli")] pub mod cli {} use super::*; //#[cfg(not(target_arch = "wasm32"))]

#[cfg(feature = "cli")] #[inline] fn deck_traverse(min: i32, max: i32, cnt: u8,
mrpt: u8, nums: &mut Vec<i32>, solve: &mut impl FnMut(&[i32])) {
(min..=max).for_each(|x| { let len = nums.len() as _;
if mrpt - 1 < len && nums.iter().fold(0u8, |acc, &n|
if n == x { acc + 1 } else { acc }) == mrpt { return } else { nums.push(x) }
Expand All @@ -675,7 +677,7 @@ pub use Calc24Algo::*;
r"failed on algo-{algo:?}");
}
``` */
pub fn game24_solvable(goal: &Rational, min: i32, max: i32, cnt: u8,
#[cfg(feature = "cli")] pub fn game24_solvable(goal: &Rational, min: i32, max: i32, cnt: u8,
silent: bool, algo: Calc24Algo) -> (u16, u16, u32) {
let mut rcnt = (0, 0, 0);

Expand Down Expand Up @@ -720,7 +722,7 @@ pub fn game24_solvable(goal: &Rational, min: i32, max: i32, cnt: u8,
rcnt.0 + rcnt.1, Paint::magenta(&rcnt.2)); rcnt
}

pub fn game24_cards(goal: &Rational, cnt: u8, algo: Calc24Algo) {
#[cfg(feature = "cli")] pub fn game24_cards(goal: &Rational, cnt: u8, algo: Calc24Algo) {
let court = [ "T", "J", "Q", "K" ]; // ♠Spade, ♡Heart, ♢Diamond, ♣Club
let suits = [ Color::Blue, Color::Red, Color::Magenta, Color::Cyan ];
let (mut rng, mut spos, mut batch)= (rand::thread_rng(), 0, 0);
Expand Down Expand Up @@ -750,7 +752,7 @@ pub fn game24_cards(goal: &Rational, cnt: u8, algo: Calc24Algo) {
let stre = exps.join(", ");

if 0 < batch { batch -= 1; // Iterator::intersperse_with
println!(r"{}", Paint::black(&stre).dimmed().bg(Color::Black));
println!(r"{}", Paint::black(&stre).dim().bg(Color::Black));
if 0 == batch { println!(); } continue
}

Expand All @@ -763,7 +765,7 @@ pub fn game24_cards(goal: &Rational, cnt: u8, algo: Calc24Algo) {

let es = es.trim();
if es.starts_with(['n', 'N']) || es.eq("?") {
println!(r"{}: {stre}", Paint::new(r"Solution").dimmed());
println!(r"{}: {stre}", Paint::new(r"Solution").dim());
if let Ok(n) = es[1..].parse::<u16>() { batch = n; } break
}

Expand All @@ -778,13 +780,13 @@ pub fn game24_cards(goal: &Rational, cnt: u8, algo: Calc24Algo) {
print!(r"{}/{:.1}s: ", Paint::new(r"Bingo").bg(Color::Green),
tnow.elapsed().as_secs_f32());
println!(r"{}", Paint::green(&stre)); break;
} else { print!(r"{}: ", Paint::new(r"Tryagain").dimmed()); }
} else { print!(r"{}: ", Paint::new(r"Tryagain").dim()); }
} println!();
}
}

use yansi::{Paint, Color}; // Style
#[allow(clippy::blocks_in_conditions)]
#[cfg(feature = "cli")] use yansi::{Paint, Color}; // Style
#[cfg(feature = "cli")] #[allow(clippy::blocks_in_conditions)]
pub fn game24_cli() { //#[cfg_attr(coverage_nightly, coverage(off))] // XXX:
fn game24_helper<I, S>(goal: &Rational, nums: I, algo: Calc24Algo, _cxx: bool)
where I: Iterator<Item = S>, S: AsRef<str> { // XXX: use closure instead?
Expand Down Expand Up @@ -838,8 +840,8 @@ pub fn game24_cli() { //#[cfg_attr(coverage_nightly, coverage(off))] // XXX:
size_of::<Oper>(), size_of::<Rational>()); */

println!("\n### Solve {} calculation ###", Paint::magenta(&goal).bold());
loop { print!("\n{}{}{}", Paint::new(r"Input integers/rationals for ").dimmed(),
Paint::yellow(&goal), Paint::new(": ").dimmed());
loop { print!("\n{}{}{}", Paint::new(r"Input integers/rationals for ").dim(),
Paint::yellow(&goal), Paint::new(": ").dim());

let mut nums = String::new(); use std::io::Write;
if let Err(e) = std::io::stdout().flush() { eprintln!(r"Failed to flush: {e}") }
Expand Down Expand Up @@ -966,6 +968,7 @@ pub fn game24_cli() { //#[cfg_attr(coverage_nightly, coverage(off))] // XXX:
assert_eq!(cnt, 10890); //cnt = 0;
}

use yansi::Paint; // Style, Color
#[test] fn solve24() {
let cases = [
( 24, vec![ ], vec![], 0),
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
//std::env::args().skip(1).for_each(|it| eprint!(" {it:?}") );
//env::var("CASE_INSENSITIVE").is_err(); // run time environment

//yansi::whenever(yansi::Condition::TTY_AND_COLOR);
if (cfg!(windows) && !Paint::enable_windows_ascii()) ||
!atty::is(atty::Stream::Stdout) { Paint::disable() }
yansi::whenever(yansi::Condition::TTY_AND_COLOR);
//if (cfg!(windows) && !Paint::enable_windows_ascii()) ||
// !atty::is(atty::Stream::Stdout) { Paint::disable() }

//include_bytes!("relative_path"); //include!("relative_path"); // XXX:
//panic!("Test a panic.");
Expand Down Expand Up @@ -76,7 +76,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
//use std::io::prelude::*;
use std::{io::Write, cmp::Ordering};
let _res = 'label: loop { // unused prefixed with underscore
print!("\n{}", Paint::white(prompt).dimmed());
print!("\n{}", Paint::white(prompt).dim());

let mut guess = String::new();
std::io::stdout().flush().expect("Failed to flush"); //.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/prime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ pub fn prime_factor(x: u64) -> Vec<(u32, u32)> { assert!(x != 0);
//HashMap.entry(n).and_modify(|cnt| *cnt += 1).or_insert(1);

#[cfg_attr(coverage_nightly, coverage(off))] //#[cfg(not(tarpaulin_include))]
#[allow(dead_code)] pub fn factor_prime_cli() {
#[allow(dead_code)] #[cfg(feature = "cli")] pub fn factor_prime_cli() {
println!("\n### Fractorization of prime ###");

loop { use std::io::Write; let mut nstr = String::new();
print!("\n{}", yansi::Paint::white("Input an arbitrary integer: ").dimmed());
print!("\n{}", yansi::Paint::white("Input an arbitrary integer: ").dim());
std::io::stdout().flush().expect("Failed to flush"); //.unwrap();
std::io::stdin().read_line(&mut nstr).expect("Failed to read");

Expand Down

0 comments on commit cfc9fef

Please sign in to comment.