summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Gesang <phg@phi-gamma.net>2021-11-11 00:33:57 +0100
committerPhilipp Gesang <phg@phi-gamma.net>2021-11-11 00:34:51 +0100
commitfbffcbbaaf3529572f7f55ff0c21735b9a29562b (patch)
treebe8030dd91bb450f7006c9b54d3a1a9f1f1ef8c7
parentd2959aa212572d8f3107d70bee4657824d690f49 (diff)
downloadvtcol-fbffcbbaaf3529572f7f55ff0c21735b9a29562b.tar.gz
avoid hard coding builtin names
-rw-r--r--Cargo.toml2
-rw-r--r--src/vtcol.rs150
2 files changed, 104 insertions, 48 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 9a7e672..82eae4e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,7 +5,7 @@
[package]
name = "vtcol"
description = "Set Linux console color scheme"
-version = "0.42.4"
+version = "0.42.5"
authors = [ "Philipp Gesang <phg@phi-gamma.net>" ]
repository = "https://github.com/phi-gamma/vtcol"
keywords = [ "linux", "virtual_terminal", "tty", "console", "system" ]
diff --git a/src/vtcol.rs b/src/vtcol.rs
index 75bc699..5f9cc17 100644
--- a/src/vtcol.rs
+++ b/src/vtcol.rs
@@ -16,9 +16,9 @@ macro_rules! vrb {
}
const PALETTE_SIZE: usize = 16;
-const PALETTE_BYTES: usize = PALETTE_SIZE * 3; // 16 * sizeof(int)
+const PALETTE_BYTES: usize = PALETTE_SIZE * 3; /* 16 * sizeof(int) */
-const RAW_COLEXPR_SIZE: usize = 6; // e. g. 0xBADF00
+const RAW_COLEXPR_SIZE: usize = 6; /* e. g. 0xBADF00 */
type RawPalette<'a> = [&'a str; PALETTE_SIZE];
@@ -103,17 +103,83 @@ impl fmt::Display for Color
}
} /* [impl fmt::Display for Color] */
+#[derive(Debug, Clone)]
+struct Builtin
+{
+ names: &'static [&'static str],
+ palette: &'static RawPalette<'static>,
+}
+
+const BUILTIN_SCHEMES: &[Builtin] = &[
+ Builtin::solarized(),
+ Builtin::solarized_light(),
+ Builtin::default(),
+ Builtin::phosphor(),
+];
+
+impl Builtin
+{
+ fn name(&self) -> &'static str { self.names.iter().next().unwrap() }
+
+ const fn solarized() -> Self
+ {
+ Self {
+ names: &["solarized", "solarized_dark", "sd"],
+ palette: &SOLARIZED_COLORS_DARK,
+ }
+ }
+
+ const fn solarized_light() -> Self
+ {
+ Self {
+ names: &["solarized_light", "sl"],
+ palette: &SOLARIZED_COLORS_LIGHT,
+ }
+ }
+
+ const fn default() -> Self
+ {
+ Self {
+ names: &["default", "normal", "linux"],
+ palette: &DEFAULT_COLORS,
+ }
+ }
+
+ const fn phosphor() -> Self
+ {
+ Self { names: &["phosphor", "matrix"], palette: &MONOCHROME_PHOSPHOR }
+ }
+}
+
+impl TryFrom<&str> for Builtin
+{
+ type Error = anyhow::Error;
+
+ fn try_from(name: &str) -> Result<Self, Self::Error>
+ {
+ for b in BUILTIN_SCHEMES {
+ if b.names.contains(&name) {
+ return Ok(b.clone());
+ }
+ }
+
+ Err(anyhow!("no such builtin: {}", name))
+ }
+}
+
+impl<'a> fmt::Display for Builtin
+{
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
+ {
+ write!(f, "{}", self.name())
+ }
+}
+
#[derive(Debug)]
enum Scheme
{
- /** Vanilla Linux colors. */
- Default,
- /** The dark (default) version of the Solarized scheme. */
- SolarizedDark,
- /** The light version of the Solarized theme. */
- SolarizedLight,
- /** Bright green monochrome terminal. */
- Phosphor,
+ /** One of the predefined schemes. */
+ Builtin(Builtin),
/** Custom ``Palette``. */
Palette(Palette),
/** Load from file. */
@@ -125,10 +191,7 @@ impl<'a> fmt::Display for Scheme
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
match self {
- Self::Default => write!(f, "default"),
- Self::SolarizedDark => write!(f, "solarized_dark"),
- Self::SolarizedLight => write!(f, "solarized_light"),
- Self::Phosphor => write!(f, "phosphor"),
+ Self::Builtin(b) => write!(f, "{}", b),
Self::Custom(None) => write!(f, "<read stdin>"),
Self::Custom(Some(fname)) => write!(f, "{}", fname.display()),
Self::Palette(pal) => write!(f, "palette: {}", pal),
@@ -152,13 +215,9 @@ impl From<&str> for Scheme
{
fn from(name: &str) -> Scheme
{
- match name {
- "solarized" | "solarized_dark" | "sd" => Self::SolarizedDark,
- "solarized_light" | "sl" => Self::SolarizedLight,
- "phosphor" | "matrix" => Self::Phosphor,
- "default" | "normal" => Self::Default,
- path => Self::from_path(path),
- }
+ Builtin::try_from(name)
+ .map(Self::Builtin)
+ .unwrap_or_else(|_| Self::from_path(name))
}
}
@@ -168,16 +227,16 @@ impl From<Palette> for Scheme
fn from(pal: Palette) -> Scheme
{
if pal == Palette::from(&DEFAULT_COLORS) {
- return Self::Default;
+ return Self::Builtin(Builtin::default());
}
if pal == Palette::from(&SOLARIZED_COLORS_DARK) {
- return Self::SolarizedDark;
+ return Self::Builtin(Builtin::solarized());
}
if pal == Palette::from(&SOLARIZED_COLORS_LIGHT) {
- return Self::SolarizedLight;
+ return Self::Builtin(Builtin::solarized_light());
}
if pal == Palette::from(&MONOCHROME_PHOSPHOR) {
- return Self::Phosphor;
+ return Self::Builtin(Builtin::phosphor());
}
Self::Palette(pal)
@@ -327,24 +386,20 @@ impl<'a> Job
}
}
- fn schemes()
+ fn list_schemes()
{
- println!("Available color schemes:");
- println!(" * solarized_dark");
- println!(" * solarized_light");
- println!(" * phosphor");
- println!(" * default");
+ println!("{} color schemes available:", BUILTIN_SCHEMES.len());
+ for s in BUILTIN_SCHEMES {
+ println!(" * {}", s.name());
+ }
}
fn dump(scm: Scheme)
{
vrb!("Dumping color scheme {}", scm);
match scm {
- Scheme::Default => Self::dump_scheme(&DEFAULT_COLORS),
- Scheme::SolarizedDark => Self::dump_scheme(&SOLARIZED_COLORS_DARK),
- Scheme::SolarizedLight =>
- Self::dump_scheme(&SOLARIZED_COLORS_LIGHT),
- Scheme::Phosphor => Self::dump_scheme(&MONOCHROME_PHOSPHOR),
+ Scheme::Builtin(Builtin { palette, .. }) =>
+ Self::dump_scheme(palette),
Scheme::Custom(None) => Self::dump_palette(Palette::from_stdin()),
Scheme::Custom(Some(fname)) =>
Self::dump_palette(Palette::from_file(&fname)),
@@ -364,7 +419,7 @@ impl<'a> Job
{
match self {
Self::Dump(scm) => Self::dump(scm),
- Self::List => Self::schemes(),
+ Self::List => Self::list_schemes(),
Self::Set(scm) => Self::set_scheme(scm)?,
Self::Get => Self::get_scheme()?,
Self::Toggle(one, two) => Self::toggle_scheme(one, two)?,
@@ -434,7 +489,7 @@ extern "C" {
) -> libc::c_int;
}
-static CONSOLE_PATHS: [&str; 6] = [
+const CONSOLE_PATHS: [&str; 6] = [
"/proc/self/fd/0",
"/dev/tty",
"/dev/tty0",
@@ -443,31 +498,35 @@ static CONSOLE_PATHS: [&str; 6] = [
"/dev/console",
];
-static DEFAULT_COLORS: RawPalette = [
+/** Vanilla Linux colors. */
+const DEFAULT_COLORS: RawPalette = [
"000000", "aa0000", "00aa00", "aa5500", "0000aa", "aa00aa", "00aaaa",
"aaaaaa", "555555", "ff5555", "55ff55", "ffff55", "5555ff", "ff55ff",
"55ffff", "ffffff",
];
-static SOLARIZED_COLORS_DARK: RawPalette = [
+/** The dark (default) version of the Solarized scheme. */
+const SOLARIZED_COLORS_DARK: RawPalette = [
"002b36", "dc322f", "859900", "b58900", "268bd2", "d33682", "2aa198",
"eee8d5", "002b36", "cb4b16", "586e75", "657b83", "839496", "6c71c4",
"93a1a1", "fdf6e3",
];
-static SOLARIZED_COLORS_LIGHT: RawPalette = [
+/** The light version of the Solarized theme. */
+const SOLARIZED_COLORS_LIGHT: RawPalette = [
"eee8d5", "dc322f", "859900", "b58900", "268bd2", "d33682", "2aa198",
"073642", "fdf6e3", "cb4b16", "93a1a1", "839496", "657b83", "6c71c4",
"586e75", "002b36",
];
-static MONOCHROME_PHOSPHOR: RawPalette = [
+/** Bright green monochrome terminal. */
+const MONOCHROME_PHOSPHOR: RawPalette = [
"000000", "68fc68", "68fc68", "68fc68", "68fc68", "68fc68", "68fc68",
"68fc68", "68fc68", "68fc68", "68fc68", "68fc68", "68fc68", "68fc68",
"68fc68", "68fc68",
];
-static DUMMY_COLORS: RawPalette = [
+const DUMMY_COLORS: RawPalette = [
"000000", "ffffff", "000000", "ffffff", "000000", "ffffff", "000000",
"ffffff", "000000", "ffffff", "000000", "ffffff", "000000", "ffffff",
"000000", "ffffff",
@@ -645,10 +704,7 @@ impl From<&Scheme> for Palette
fn from(scm: &Scheme) -> Self
{
match scm {
- Scheme::Default => Self::from(&DEFAULT_COLORS),
- Scheme::SolarizedDark => Self::from(&SOLARIZED_COLORS_DARK),
- Scheme::SolarizedLight => Self::from(&SOLARIZED_COLORS_LIGHT),
- Scheme::Phosphor => Self::from(&MONOCHROME_PHOSPHOR),
+ Scheme::Builtin(Builtin { palette, .. }) => Self::from(*palette),
Scheme::Custom(None) => Self::from_stdin(),
Scheme::Custom(Some(ref fname)) => Self::from_file(fname),
Scheme::Palette(pal) => pal.clone(),