From 4161597a2899f7b0286e7112a6d48f8ddff7e71c Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sun, 3 May 2015 10:16:11 +0200 Subject: vtcol.rs: prepare loading of color scheme files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Doesn’t work yet but the types are all in place. DYK, creating true fixed-size array is a horrible PITA in Rust. --- vtcol.rs | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/vtcol.rs b/vtcol.rs index dba2245..2cd1bd7 100644 --- a/vtcol.rs +++ b/vtcol.rs @@ -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,12 +207,41 @@ 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]) { @@ -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); -- cgit v1.2.3