Skip to content
Merged
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
13 changes: 13 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ pub struct AppConfig {
#[clap(short, long, default_value = "1", value_parser = parse_speed_factor)]
pub speed_factor: f32,

/// Instantly print this many lines
#[clap(short, long = "instant-print-lines", default_value = "0")]
pub instant_print_lines: u32,

/// Exit after running for this long (format example: 2h10min)
#[clap(long, value_parser = humantime::parse_duration)]
pub exit_after_time: Option<instant::Duration>,
Expand All @@ -61,6 +65,9 @@ pub struct AppConfig {

/// Global speed factor
pub speed_factor: f32,

/// Instantly print this many lines
pub instant_print_lines: u32,
}

impl AppConfig {
Expand Down Expand Up @@ -122,6 +129,11 @@ pub fn parse_args() -> AppConfig {
.map(|(_, v)| v.parse::<f32>().unwrap_or(1.0))
.unwrap_or(1.0);

let instant_print_lines: u32 = pairs
.find(|&(ref k, _)| k == "instant-print-lines")
.map(|(_, v)| v.parse::<u32>().unwrap_or(0))
.unwrap_or(0);

let modules_to_run = if temp_modules.is_empty() {
ALL_MODULES.keys().map(|m| m.to_string()).collect()
} else {
Expand All @@ -131,5 +143,6 @@ pub fn parse_args() -> AppConfig {
AppConfig {
modules: modules_to_run,
speed_factor,
instant_print_lines,
}
}
24 changes: 21 additions & 3 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,38 @@ use wasm_bindgen::prelude::*;
#[cfg(not(target_arch = "wasm32"))]
use std::io::{stdout, Write};

use crate::SPEED_FACTOR;
use crate::{INSTANT_PRINT_LINES, SPEED_FACTOR};

use std::sync::atomic::{AtomicU32, Ordering};
static COUNTER: AtomicU32 = AtomicU32::new(0);

#[cfg(not(target_arch = "wasm32"))]
pub async fn csleep(length: u64) {
use std::time;

let speed_factor = *SPEED_FACTOR.lock().await;
let sleep_length = time::Duration::from_millis((1.0 / speed_factor * length as f32) as u64);
let count = COUNTER.fetch_add(1, Ordering::SeqCst);
let sleep_length = if count < INSTANT_PRINT_LINES.load(Ordering::SeqCst) {
// If user passed `--instant-print-lines`, there should be
// no pauses in first `INSTANT_PRINT_LINES` number of lines
time::Duration::new(0, 0)
} else {
time::Duration::from_millis((1.0 / speed_factor * length as f32) as u64)
};
async_std::task::sleep(sleep_length).await;
}

#[cfg(target_arch = "wasm32")]
pub async fn csleep(length: u64) {
let speed_factor = *SPEED_FACTOR.lock().await;
let sleep_length = (1.0 / speed_factor * length as f32) as i32;
let count = COUNTER.fetch_add(1, Ordering::SeqCst);
let sleep_length = if count < INSTANT_PRINT_LINES.load(Ordering::SeqCst) {
// If user passed `--instant-print-lines`, there should be
// no pauses in first `INSTANT_PRINT_LINES` number of lines
0 as i32
} else {
(1.0 / speed_factor * length as f32) as i32
};

let promise = js_sys::Promise::new(&mut move |resolve, _| {
let window = web_sys::window().expect("should have a Window");
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use modules::{Module, ALL_MODULES};
lazy_static::lazy_static! {
pub static ref CTRLC_PRESSED: AtomicBool = AtomicBool::new(false);
pub static ref SPEED_FACTOR: Mutex<f32> = Mutex::new(1.0);
pub static ref INSTANT_PRINT_LINES: AtomicU32 = AtomicU32::new(0);
pub static ref STARTED_AT: Instant = Instant::now();
pub static ref MODULES_RAN: AtomicU32 = AtomicU32::new(0);
}
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
use anyhow::Result;

use genact::args::parse_args;
use genact::{run, SPEED_FACTOR};
use genact::{run, INSTANT_PRINT_LINES, SPEED_FACTOR};

use std::sync::atomic::Ordering;

#[cfg(not(target_arch = "wasm32"))]
use genact::exit_handler;
Expand Down Expand Up @@ -32,6 +34,7 @@ async fn main() -> Result<()> {
}

*SPEED_FACTOR.lock().await = appconfig.speed_factor;
INSTANT_PRINT_LINES.store(appconfig.instant_print_lines, Ordering::SeqCst);

if appconfig.list_modules_and_exit {
println!("Available modules:");
Expand All @@ -57,6 +60,7 @@ async fn main() {

let appconfig = parse_args();
*SPEED_FACTOR.lock().await = appconfig.speed_factor;
INSTANT_PRINT_LINES.store(appconfig.instant_print_lines, Ordering::SeqCst);

run(appconfig).await;
}