From de9089c4681471b9cf6144f5ada88c4c803ecf12 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Wed, 10 Nov 2021 23:48:36 +0100 Subject: add toggle subcommand --- Cargo.toml | 2 +- README.rst | 5 +++++ src/vtcol.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 6dfc369..9a7e672 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ [package] name = "vtcol" description = "Set Linux console color scheme" -version = "0.42.3" +version = "0.42.4" authors = [ "Philipp Gesang " ] repository = "https://github.com/phi-gamma/vtcol" keywords = [ "linux", "virtual_terminal", "tty", "console", "system" ] diff --git a/README.rst b/README.rst index 58ec09a..1809902 100644 --- a/README.rst +++ b/README.rst @@ -42,6 +42,11 @@ To show the current scheme of the active console use the ``get`` subcommand: :: $ vtcol get solarized_dark +It is also possible to use vtcol to switch between two themes by means of the +``toggle`` subcommand. E. g. to cycle between “dark mode” and “light mode”: :: + + $ vtcol toggle solarized solarized_light + To view a scheme’s definition, for instance in order to verify that **vtcol** parses it correctly, use the ``dump`` command. :: diff --git a/src/vtcol.rs b/src/vtcol.rs index 45cbbd4..81f586f 100644 --- a/src/vtcol.rs +++ b/src/vtcol.rs @@ -185,6 +185,8 @@ enum Job Set(Scheme), /** Get currently active scheme. */ Get, + /** Toggle between two schemes. */ + Toggle(Scheme, Scheme), } impl<'a> Job @@ -231,6 +233,22 @@ impl<'a> Job .subcommand( SubCommand::with_name("get").about("get current color scheme"), ) + .subcommand( + SubCommand::with_name("toggle") + .about("toggle between two schemes") + .arg( + Arg::with_name("one") + .value_name("NAME1") + .help("predefined color scheme") + .takes_value(true), + ) + .arg( + Arg::with_name("two") + .value_name("NAME2") + .help("predefined color scheme") + .takes_value(true), + ), + ) .arg( Arg::with_name("verbose") .short("v") @@ -276,6 +294,18 @@ impl<'a> Job Ok(Self::Set(scheme)) }, ("get", _) => Ok(Self::Get), + ("toggle", Some(subm)) => { + match (subm.value_of("one"), subm.value_of("two")) { + (Some(one), Some(two)) => { + vrb!("toggle schemes [{}] and [{}]", one, two); + Ok(Self::Toggle(Scheme::from(one), Scheme::from(two))) + }, + _ => + Err(anyhow!( + "please supply two schemes to toggle between" + )), + } + }, (junk, _) => Err(anyhow!( "invalid subcommand [{}]; try ``{} --help``", @@ -323,6 +353,7 @@ impl<'a> Job Self::List => Self::schemes(), Self::Set(scm) => Self::set_scheme(scm)?, Self::Get => Self::get_scheme()?, + Self::Toggle(one, two) => Self::toggle_scheme(one, two)?, } Ok(()) @@ -357,6 +388,23 @@ impl<'a> Job Ok(()) } + + /** Toggle between two schemes. Defaults to ``one`` in case neither scheme + is active. + */ + fn toggle_scheme(one: Scheme, two: Scheme) -> Result<()> + { + let fd = get_console_fd()?; + vrb!("console fd: {}", fd); + + let pal = ioctl_gio_cmap(fd)?; + + if pal == Palette::from(&one) { + Self::set_scheme(two) + } else { + Self::set_scheme(one) + } + } } /* [impl Job] */ /* Rust appears to come with two wrappers for ``ioctl(2)``, but neither can be utilized for our -- cgit v1.2.3