diff options
author | Philipp Gesang <phg42.2a@gmail.com> | 2015-05-03 10:16:11 +0200 |
---|---|---|
committer | Philipp Gesang <phg42.2a@gmail.com> | 2015-05-03 10:16:30 +0200 |
commit | 4161597a2899f7b0286e7112a6d48f8ddff7e71c (patch) | |
tree | a9e25220aedb7f0b768ee9ffa4ccedb4451bb19d | |
parent | 2e63c7639d3e5729fd967babf9aaa3ea557f740e (diff) | |
download | vtcol-4161597a2899f7b0286e7112a6d48f8ddff7e71c.tar.gz |
vtcol.rs: prepare loading of color scheme files
Doesn’t work yet but the types are all in place. DYK, creating true
fixed-size array is a horrible PITA in Rust.
-rw-r--r-- | vtcol.rs | 44 |
1 files changed, 38 insertions, 6 deletions
@@ -1,8 +1,11 @@ #![allow(unstable)] +extern crate core; extern crate libc; extern crate getopts; +use core::ptr; + type Fd = libc::c_int; const PALETTE_SIZE : usize = 16_us; @@ -88,7 +91,7 @@ enum Scheme<'a> { Default, SolarizedDark, SolarizedLight, - Custom(RawPalette<'a>) + Custom (&'a [&'a str; PALETTE_SIZE]) } impl<'a> std::fmt::String for Scheme<'a> { @@ -204,13 +207,42 @@ impl<'a> Job<'a> { => Scheme::SolarizedLight, "default" | "normal" => Scheme::Default, - garbage => { - panic!("unknown color scheme “{}”, aborting", garbage); - } + any => Scheme::Custom (Job::load_scheme_file(name)), } } fn + load_scheme_file <'b> (name : &String) + -> &'b [&'b str; PALETTE_SIZE] + { + /* Check if file exists + */ + let path = Path::new(name.as_bytes()); + let file = match std::io::File::open(&path) + { + Err(e) => panic!("failed to open {} as file ({})", name, e), + Ok(f) => f + }; + let mut reader = std::io::BufferedReader::new(file); + + /* Parse scheme file + */ + let mut i : usize = 0_us; + while let Ok(line) = reader.read_line() { + let len = line.len(); + if len < 8_us { panic!("invalid line in string: {}", line); }; + if let Some(idx) = line.find_str("#") { + let idx = idx + 1_us; + if idx > len - 6_us { /* no room left for color definition after '#' char */ + panic!("invalid color definition: {}", line); + } + let col = line.slice_chars(idx, idx + 5_us); + } + }; + } + + + fn usage (this : &String, opts: &[getopts::OptGroup]) { let brief = format!("usage: {} [options]", this); @@ -234,7 +266,7 @@ impl<'a> Job<'a> { Scheme::Default => Job::dump_scheme(&DEFAULT_COLORS), Scheme::SolarizedDark => Job::dump_scheme(&SOLARIZED_COLORS_DARK), Scheme::SolarizedLight => Job::dump_scheme(&SOLARIZED_COLORS_LIGHT), - Scheme::Custom(_) => panic!("not implemented! patience, my young padawan!") + Scheme::Custom(cols) => Job::dump_scheme(cols), } } @@ -519,7 +551,7 @@ main () Scheme::Default => Palette::new(&DEFAULT_COLORS), Scheme::SolarizedDark => Palette::new(&SOLARIZED_COLORS_DARK), Scheme::SolarizedLight => Palette::new(&SOLARIZED_COLORS_LIGHT), - Scheme::Custom(_) => panic!("not implemented") + Scheme::Custom (cols) => Palette::new(cols), } }; println!("{}", pal); |