From 84a552f3265d82e9fa553e9ddd3597a56739cbee Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Mon, 22 Nov 2021 00:24:59 +0100 Subject: implement fading vtcol fade --ms 1000 --frequency 10 --to solarized & dmesg --- src/vtcol.rs | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 97 insertions(+), 2 deletions(-) (limited to 'src/vtcol.rs') diff --git a/src/vtcol.rs b/src/vtcol.rs index b0c4b85..3694753 100644 --- a/src/vtcol.rs +++ b/src/vtcol.rs @@ -1,13 +1,17 @@ pub mod lib; -use vtcol::{Console, Palette, Scheme}; +use vtcol::{Console, Fade, Palette, Scheme}; use anyhow::{anyhow, Result}; use std::{io::{self, BufWriter}, - sync::atomic::{AtomicBool, Ordering}}; + sync::atomic::{AtomicBool, Ordering}, + time::Duration}; static VERBOSITY: AtomicBool = AtomicBool::new(false); +const DEFAULT_FADE_DURATION_MS: u64 = 500; +const DEFAULT_FADE_UPDATE_HZ: u8 = 25; + macro_rules! vrb { ( $( $e:expr ),* ) => {( if VERBOSITY.load(Ordering::SeqCst) { println!( $( $e ),* ) } @@ -29,6 +33,8 @@ enum Job Get, /** Toggle between two schemes. */ Toggle(Scheme, Scheme), + /** Fade from current scheme to another. */ + Fade(Option, Scheme, Duration, u8), } impl<'a> Job @@ -91,6 +97,43 @@ impl<'a> Job .takes_value(true), ), ) + .subcommand( + SubCommand::with_name("fade") + .about("fade from one scheme to another") + .arg( + Arg::with_name("from") + .short("f") + .long("from") + .value_name("NAME1") + .help("initial color scheme (default: current)") + .takes_value(true), + ) + .arg( + Arg::with_name("to") + .short("t") + .long("to") + .value_name("NAME2") + .help("final color scheme") + .takes_value(true) + .required(true), + ) + .arg( + Arg::with_name("ms") + .value_name("MS") + .short("m") + .long("ms") + .help("how long (in ms) the fade should take") + .takes_value(true), + ) + .arg( + Arg::with_name("frequency") + .value_name("HZ") + .short("h") + .long("frequency") + .help("rate (HZ/s) of intermediate scheme changes") + .takes_value(true), + ), + ) .arg( Arg::with_name("verbose") .short("v") @@ -148,6 +191,31 @@ impl<'a> Job )), } }, + ("fade", Some(subm)) => { + let dur: u64 = if let Some(ms) = subm.value_of("ms") { + ms.parse()? + } else { + DEFAULT_FADE_DURATION_MS + }; + let hz: u8 = if let Some(ms) = subm.value_of("frequency") { + ms.parse()? + } else { + DEFAULT_FADE_UPDATE_HZ + }; + let dur = Duration::from_millis(dur); + + match (subm.value_of("from"), subm.value_of("to")) { + (_, None) => + Err(anyhow!("please supply color scheme to fade to")), + (from, Some(to)) => + Ok(Self::Fade( + from.map(Scheme::from), + Scheme::from(to), + dur, + hz, + )), + } + }, (junk, _) => Err(anyhow!( "invalid subcommand [{}]; try ``{} --help``", @@ -212,6 +280,7 @@ impl<'a> Job Self::Set(scm) => Self::set_scheme(scm)?, Self::Get => Self::get_scheme()?, Self::Toggle(one, two) => Self::toggle_scheme(one, two)?, + Self::Fade(from, to, ms, hz) => Self::fade(from, to, ms, hz)?, } Ok(()) @@ -257,6 +326,32 @@ impl<'a> Job Self::set_scheme(one) } } + + /** Fade from one scheme to another. + + If ``from`` is ``None``, the current palette is used as starting point. */ + fn fade( + from: Option, + to: Scheme, + dur: Duration, + hz: u8, + ) -> Result<()> + { + let fd = Console::current()?; + vrb!("console fd: {}", fd); + + let from = if let Some(from) = from { + Palette::try_from(&from)? + } else { + fd.current_palette()? + }; + let to = Palette::try_from(&to)?; + + let fade = Fade::new(from, to, dur, hz); + + fade.commence(&fd)?; + Ok(()) + } } /* [impl Job] */ fn main() -> Result<()> -- cgit v1.2.3