diff options
author | Philipp Gesang <phg42.2a@gmail.com> | 2015-02-07 17:32:28 +0100 |
---|---|---|
committer | Philipp Gesang <phg42.2a@gmail.com> | 2015-02-07 17:32:28 +0100 |
commit | aaaad6329a2c949dedc15465940c442ae9a2ec6b (patch) | |
tree | a9ca9d05953ddc6b50b5f3c3607b419a44502187 | |
parent | ed6ce958d5f3d5aa495d4314288b995fb7bb358c (diff) | |
download | vtcol-aaaad6329a2c949dedc15465940c442ae9a2ec6b.tar.gz |
vtcol.rs: implement --dump option for printing individual schemes
-rw-r--r-- | vtcol.rs | 110 |
1 files changed, 98 insertions, 12 deletions
@@ -6,6 +6,22 @@ extern crate getopts; #[derive(Show)] enum Scheme { Default, SolarizedDark, SolarizedLight } +impl std::fmt::String for Scheme { + + fn + fmt (&self, f : &mut std::fmt::Formatter) -> std::fmt::Result + { + let id : &str = match (*self) + { + Scheme::Default => "default", + Scheme::SolarizedDark => "solarized_dark", + Scheme::SolarizedLight => "solarized_light" + }; + write!(f, "{}", id) + } + +} /* [impl String for Scheme] */ + extern { fn exit (code : libc::c_int) -> !; } /* struct Job -- Runtime parameters. @@ -26,35 +42,48 @@ impl Job { let this = argv[0].clone(); let opts = &[ getopts::optopt("s", "scheme", "predefined color scheme", "NAME"), + getopts::optopt("d", "dump", "dump predefined scheme", "NAME"), getopts::optflag("l", "list", "list available color schemes"), getopts::optflag("h", "help", "print this message") ]; + let matches = match getopts::getopts(argv.tail(), opts) { Ok(m) => m, Err(f) => panic!(f.to_string()) }; + if matches.opt_present("l") { Job::schemes(); unsafe { exit(0) }; }; - let scheme = match matches.opt_str("s") { + + if matches.opt_present("d") + { + match matches.opt_str("d") { + None => + { + Job::usage(&this, opts); + panic!("no color scheme given, aborting") + }, + Some (name) => + { + let scm = Job::pick_scheme(&name); + Job::dump(scm); + unsafe { exit(0) }; + } + }; + } + + let scheme = match matches.opt_str("s") + { None => { Job::usage(&this, opts); panic!("no color scheme given, aborting") }, - Some (name) => { - match name.as_slice() { - "solarized" | "solarized_dark" | "sd" => Scheme::SolarizedDark, - "solarized_light" | "sl" => Scheme::SolarizedLight, - "default" | "normal" => Scheme::Default, - garbage => { - Job::usage(&this, opts); - panic!("unknown color scheme “{}”, aborting", garbage); - } - } - } + Some (name) => Job::pick_scheme(&name) }; + Job { this : this, scheme : scheme @@ -62,6 +91,23 @@ impl Job { } fn + pick_scheme (name : &String) + -> Scheme + { + match name.as_slice() { + "solarized" | "solarized_dark" | "sd" + => Scheme::SolarizedDark, + "solarized_light" | "sl" + => Scheme::SolarizedLight, + "default" | "normal" + => Scheme::Default, + garbage => { + panic!("unknown color scheme “{}”, aborting", garbage); + } + } + } + + fn usage (this : &String, opts: &[getopts::OptGroup]) { let brief = format!("usage: {} [options]", this); @@ -77,6 +123,24 @@ impl Job { println!(" · default"); } + fn + dump (scm : Scheme) + { + println!("Dumping color scheme {}", scm); + match scm { + Scheme::Default => Job::dump_scheme(&DEFAULT_COLORS), + Scheme::SolarizedDark => Job::dump_scheme(&SOLARIZED_COLORS_DARK), + Scheme::SolarizedLight => Job::dump_scheme(&SOLARIZED_COLORS_LIGHT) + } + } + + fn + dump_scheme (colors : &[&str; PALETTE_SIZE]) + { + let pal : Palette = Palette::new(colors); + pal.dump() + } + } /* [impl Job] */ /* Rust appears to come with two wrappers for ``ioctl(2)``, but neither can be utilized for our @@ -136,6 +200,28 @@ pub struct Palette { colors : [u8; PALETTE_BYTES] } +impl Palette +{ + + fn + dump (&self) + { + let mut i : usize = 0; + let mut buf : [u8; 3] = [ 0u8, 0u8, 0u8 ]; + for col in self.colors.iter() + { + let idx : usize = i % 3; + buf[idx] = *col; + if idx == 2us { + println!("[{:02.x}] 0x{:02.X}{:02.X}{:02.X}", + i / 3, buf[0us], buf[1us], buf[2us]); + } + i = i + 1; + } + } + +} /* [impl Palette] */ + fn nibble_of_char (chr : u8) |