From 1af4f53013c1b3b57ef9eab0075b5fb03762e8a4 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sun, 25 Jan 2015 13:04:54 +0100 Subject: vtcol.rs: use getopts to determine colorscheme from argv --- vtcol.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/vtcol.rs b/vtcol.rs index 04e4f86..be3081a 100644 --- a/vtcol.rs +++ b/vtcol.rs @@ -1,4 +1,52 @@ +#![allow(unstable)] + extern crate libc; +extern crate getopts; + +#[derive(Show)] +enum Scheme { Default, SolarizedDark, SolarizedLight } + +/* struct Job -- Runtime parameters. + */ +#[derive(Show)] +struct Job { + this : String, /* argv[0] */ + scheme : Scheme, /* The color scheme to switch to. */ +} + +impl Job { + + pub fn + new () + -> Job + { + let argv = std::os::args(); + let opts = &[ + getopts::optopt("s", "scheme", "predefined color scheme", "NAME"), + getopts::optflag("h", "help", "print this message") + ]; + let matches = match getopts::getopts(argv.tail(), opts) + { + Ok(m) => m, + Err(f) => panic!(f.to_string()) + }; + let scheme = match matches.opt_str("s") { + None => Scheme::Default, + Some (name) => { + match name.as_slice() { + "solarized" | "solarized_dark" | "sd" => Scheme::SolarizedDark, + "solarized_light" | "sl" => Scheme::SolarizedLight, + _ => Scheme::Default + } + } + }; + Job { + this : argv[0].clone(), + scheme : scheme + } + } + +} /* [impl Job] */ /* Rust appears to come with two wrappers for ``ioctl(2)``, but neither can be utilized for our * purposes. The one in ``sys`` is part of a private (seriously‽) whereas the one in the @@ -23,7 +71,6 @@ const PIO_CMAP : libc::c_int = 0x00004B71; /* kd.h */ const KB_101 : libc::c_char = 0x0002; /* kd.h */ const O_NOCTTY : libc::c_int = 0o0400; /* fcntl.h */ - static CONSOLE_PATHS : [&'static str; 6] = [ "/proc/self/fd/0", "/dev/tty", @@ -54,11 +101,6 @@ static SOLARIZED_COLORS_LIGHT : [&'static str; PALETTE_SIZE] = [ "657b83", "6c71c4", "586e75", "002b36", ]; -/* - * The palette struct is the type expected by ioctl PIO_CMAP - */ -//struct palette { unsigned char colors[PALETTE_SIZE * 7]; }; - pub struct Palette { colors : [u8; PALETTE_BYTES] } @@ -256,10 +298,16 @@ clear_term (fd : Fd) fn main () { + let job = Job::new(); + println!("job parms: {:?}", job); let color_set : [[&str; 7]; PALETTE_SIZE]; - //let mut pal : Palette = Palette::new(&DEFAULT_COLORS); - let mut pal : Palette = Palette::new(&SOLARIZED_COLORS_DARK); - //let mut pal : Palette = Palette::new(&SOLARIZED_COLORS_LIGHT); + let mut pal : Palette = { + match job.scheme { + Scheme::Default => Palette::new(&DEFAULT_COLORS), + Scheme::SolarizedDark => Palette::new(&SOLARIZED_COLORS_DARK), + Scheme::SolarizedLight => Palette::new(&SOLARIZED_COLORS_LIGHT), + } + }; println!("{}", pal); //println!("{:?}", pal); let fd = get_console_fd(None).unwrap(); @@ -269,5 +317,6 @@ main () panic!("PIO_CMAP, ioctl failed to insert new palette") } clear_term(fd); + println!("terminated from job {:?}", job); } -- cgit v1.2.3