From 974879f55c6cbf5b85f02adc041b8730ad41ad43 Mon Sep 17 00:00:00 2001 From: mark <> Date: Thu, 22 Jan 2026 23:08:28 +0100 Subject: [PATCH] v1.0.0 --- .gitignore | 1 + Cargo.lock | 397 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 8 ++ LICENSE | 5 + README.md | 26 ++++ src/main.rs | 221 +++++++++++++++++++++++++++++ 6 files changed, 658 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..0b11067 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,397 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "bitflags" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" + +[[package]] +name = "block2" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdeb9d870516001442e364c5220d3574d2da8dc765554b4a617230d33fa58ef5" +dependencies = [ + "objc2", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + +[[package]] +name = "convert_case" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "633458d4ef8c78b72454de2d54fd6ab2e60f9e02be22f3c6104cdc8a4e0fceb9" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "crossterm" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8b9f2e4c67f833b660cdb0a3523065869fb35570177239812ed4c905aeff87b" +dependencies = [ + "bitflags", + "crossterm_winapi", + "derive_more", + "document-features", + "mio", + "parking_lot", + "rustix", + "signal-hook", + "signal-hook-mio", + "winapi", +] + +[[package]] +name = "crossterm_winapi" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b" +dependencies = [ + "winapi", +] + +[[package]] +name = "ctrlc" +version = "3.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73736a89c4aff73035ba2ed2e565061954da00d4970fc9ac25dcc85a2a20d790" +dependencies = [ + "dispatch2", + "nix", + "windows-sys", +] + +[[package]] +name = "derive_more" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d751e9e49156b02b44f9c1815bcb94b984cdcc4396ecc32521c739452808b134" +dependencies = [ + "derive_more-impl", +] + +[[package]] +name = "derive_more-impl" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "799a97264921d8623a957f6c3b9011f3b5492f557bbb7a5a19b7fa6d06ba8dcb" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "rustc_version", + "syn", +] + +[[package]] +name = "dispatch2" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89a09f22a6c6069a18470eb92d2298acf25463f14256d24778e1230d789a2aec" +dependencies = [ + "bitflags", + "block2", + "libc", + "objc2", +] + +[[package]] +name = "document-features" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4b8a88685455ed29a21542a33abd9cb6510b6b129abadabdcef0f4c55bc8f61" +dependencies = [ + "litrs", +] + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "libc" +version = "0.2.180" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" + +[[package]] +name = "linux-raw-sys" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" + +[[package]] +name = "litrs" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11d3d7f243d5c5a8b9bb5d6dd2b1602c0cb0b9db1621bafc7ed66e35ff9fe092" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" + +[[package]] +name = "mio" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a69bcab0ad47271a0234d9422b131806bf3968021e5dc9328caf2d4cd58557fc" +dependencies = [ + "libc", + "log", + "wasi", + "windows-sys", +] + +[[package]] +name = "nix" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", +] + +[[package]] +name = "objc2" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c2599ce0ec54857b29ce62166b0ed9b4f6f1a70ccc9a71165b6154caca8c05" +dependencies = [ + "objc2-encode", +] + +[[package]] +name = "objc2-encode" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef25abbcd74fb2609453eb695bd2f860d389e457f67dc17cafc8b8cbc89d0c33" + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc74d9a594b72ae6656596548f56f667211f8a97b3d4c3d467150794690dc40a" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags", +] + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + +[[package]] +name = "rustix" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "146c9e247ccc180c1f61615433868c99f3de3ae256a30a43b49f67c2d9171f34" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "semver" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d767eb0aabc880b29956c35734170f26ed551a859dbd361d140cdbeca61ab1e2" + +[[package]] +name = "signal-hook" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d881a16cf4426aa584979d30bd82cb33429027e42122b169753d6ef1085ed6e2" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-mio" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b75a19a7a740b25bc7944bdee6172368f988763b744e3d4dfe753f6b4ece40cc" +dependencies = [ + "libc", + "mio", + "signal-hook", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "syn" +version = "2.0.114" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" + +[[package]] +name = "unicode-segmentation" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "watsch" +version = "0.1.0" +dependencies = [ + "crossterm", + "ctrlc", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..450bda0 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "watsch" +version = "1.0.0" +edition = "2024" + +[dependencies] +crossterm = "0.29.0" +ctrlc = "3.5.1" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0e77b64 --- /dev/null +++ b/LICENSE @@ -0,0 +1,5 @@ +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9b01cf5 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# watsch + +`watsch` is a `watch` alternative + +``` +Usage: watsch [options] [--] +Options: + -c, --concat + Print program output directly without clearing the screen or resetting the cursor position + -f, --fast, --force + After exits, immediately start it again (same as setting all timing options to zero, currently -i 0 -d 0) + -i, --interval (seconds, default: 1, value: numbers >= 0) + Minimum wait time after is started before starting it again + -d, --delay (seconds, default: 0.1, value: numbers >= 0) + Minimum wait time after exits before starting it again + -x, --fail, --exit + Fail if an error occurs (--fail-exec --fail-with) + --fail-exec + Fail if can't be executed (exit with nonzero exit code instead of retrying) + --fail-with + Fail if fails (exit with 's exit code if it is nonzero) +Notes: + An option's effect may be partially or entirely negated by options specified after it. + This program should prevent flickering by using synchronized updates, + but only some terminals and multiplexers seem to implement this. +``` diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..ff5c943 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,221 @@ +use crossterm::{ + cursor::{Hide as HideCursor, MoveTo, Show as ShowCursor}, + execute, + terminal::{ + BeginSynchronizedUpdate, Clear, ClearType, EndSynchronizedUpdate, EnterAlternateScreen, + LeaveAlternateScreen, + }, +}; +use std::{ + ffi::{OsStr, OsString}, + process::{Command, ExitCode}, + sync::{Arc, Condvar, Mutex}, + thread::sleep, + time::{Duration, Instant}, +}; + +fn main() -> ExitCode { + // options + let mut program = None; + let mut interval = Duration::from_secs(1); + let mut delay = Duration::from_secs_f64(0.1); + let mut concat = false; + let mut fail_exec = false; + let mut fail_with = false; + + // parse args + let mut exit_code = ExitCode::FAILURE; + let mut args = std::env::args_os().peekable(); + let call_name = args.next(); + while let Some(arg) = args.next() { + if let Some(arg) = arg.to_str().filter(|&arg| arg.starts_with('-')) { + match arg { + "--" => { + program = args.next(); + break; + } + "-h" | "--help" => { + exit_code = ExitCode::SUCCESS; + showhelp(&mut args); + } + "-c" | "--concat" => { + concat = true; + } + "-f" | "--fast" | "--force" => { + interval = Duration::ZERO; + delay = Duration::ZERO; + } + "-i" | "--interval" => { + arg_f64(&mut interval, arg, &mut args); + } + "-d" | "--delay" => { + arg_f64(&mut delay, arg, &mut args); + } + "-x" | "--fail" | "--exit" => { + fail_exec = true; + fail_with = true; + } + "--fail-exec" => { + fail_exec = true; + } + "--fail-with" => { + fail_with = true; + } + _ => { + eprintln!("\nUnknown option {arg}\n"); + showhelp(&mut args); + } + } + } else { + program = Some(arg); + break; + } + } + let program = if let Some(p) = program { + p + } else { + eprintln!( + "Usage: {} [options] [--] {}{}{}{}{}{}{}{}{}{}{}{}", + call_name + .as_ref() + .map_or(OsStr::new("untear"), |n| n) + .display(), + "\nOptions:", + "\n -c, --concat\n Print program output directly without clearing the screen or resetting the cursor position", + "\n -f, --fast, --force\n After exits, immediately start it again (same as setting all timing options to zero, currently -i 0 -d 0)", + "\n -i, --interval (seconds, default: 1, value: numbers >= 0)\n Minimum wait time after is started before starting it again", + "\n -d, --delay (seconds, default: 0.1, value: numbers >= 0)\n Minimum wait time after exits before starting it again", + "\n -x, --fail, --exit\n Fail if an error occurs (--fail-exec --fail-with)", + "\n --fail-exec\n Fail if can't be executed (exit with nonzero exit code instead of retrying)", + "\n --fail-with\n Fail if fails (exit with 's exit code if it is nonzero)", + "\nNotes:", + "\n An option's effect may be partially or entirely negated by options specified after it.", + "\n This program should prevent flickering by using synchronized updates,", + "\n but only some terminals and multiplexers seem to implement this.", + ); + return exit_code; + }; + exit_code = ExitCode::SUCCESS; + let args = args.collect::>(); + + let exit = Arc::new((Mutex::new(false), Condvar::new())); + { + let exit = Arc::clone(&exit); + ctrlc::set_handler(move || { + *exit.0.lock().unwrap() = true; + exit.1.notify_one(); + }) + .expect("failed to set ctrl-c handler"); + } + + if !concat { + let _ = execute!(std::io::stdout(), EnterAlternateScreen, HideCursor); + } + + let mut next_run = Instant::now(); + loop { + if *exit.0.lock().unwrap() { + break; + } + + if concat { + let _ = execute!(std::io::stdout(), BeginSynchronizedUpdate); + } else { + let _ = execute!( + std::io::stdout(), + BeginSynchronizedUpdate, + MoveTo(0, 0), + Clear(ClearType::FromCursorDown), + ); + } + + // spawn command and handle failures + let failure = match Command::new(&program).args(&args).status() { + Ok(s) => { + if s.success() { + false + } else if fail_with { + exit_code = s.code().map_or(ExitCode::FAILURE, |code| { + ExitCode::from((code & 0xFF) as u8) + }); + true + } else { + false + } + } + Err(e) => { + eprintln!("{e}"); + if fail_exec { + exit_code = ExitCode::FAILURE; + true + } else { + false + } + } + }; + + let _ = execute!(std::io::stdout(), EndSynchronizedUpdate); + + if failure || *exit.0.lock().unwrap() { + break; + } + + // wait until start + interval or now + delay, whichever is later, + // but handle ctrl+c using a condvar which the ctrlc handler may notify + let mut now = Instant::now(); + next_run = (now + delay).max(next_run + interval); + if loop { + let dur = next_run.saturating_duration_since(now); + // if more than 50ms left, rely on the condvar, else sleep + // normally because the condvar timeout is less accurate + if dur > Duration::from_millis(50) { + match exit.1.wait_timeout_while( + exit.0.lock().unwrap(), + dur - Duration::from_millis(25), + |v| !*v, + ) { + Ok((exit, _res)) => { + if *exit { + break true; + } + } + Err(_) => break true, + } + } else { + sleep(dur); + break false; + } + now = Instant::now(); + } { + break; + } + } + + if !concat { + let _ = execute!(std::io::stdout(), LeaveAlternateScreen, ShowCursor); + } + + exit_code +} + +// args parsing helper functions + +fn showhelp(args: &mut I) { + for _ in args {} +} + +fn arg_f64>(t: &mut Duration, arg: &str, args: &mut I) { + if let Some(arg) = args.next() { + if let Some(arg) = arg + .to_str() + .and_then(|arg| arg.trim().parse().ok().filter(|v| *v >= 0.0)) + { + *t = Duration::from_secs_f64(arg); + } else { + eprintln!("\nExpected a positive number after {}\n", arg.display()); + showhelp(args); + } + } else { + eprintln!("\nExpected another argument after {arg}\n"); + } +}