From 6243b83d111c7cd8690d643ee93e48091a3fe9e1 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Tue, 9 Nov 2021 23:52:24 +0100 Subject: deal with bitrot and archaic idioms Mostly adapting to match ergonomics. --- src/vtcol.rs | 184 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 90 insertions(+), 94 deletions(-) diff --git a/src/vtcol.rs b/src/vtcol.rs index c3344ce..4b38a8c 100644 --- a/src/vtcol.rs +++ b/src/vtcol.rs @@ -1,23 +1,24 @@ -use std::sync::Mutex; -use std::path::{Path, PathBuf}; use lazy_static::lazy_static; +use std::{fmt, + path::{Path, PathBuf}, + sync::Mutex}; type Fd = libc::c_int; lazy_static! { - static ref VERBOSITY: Mutex = false; + static ref VERBOSITY: Mutex = Mutex::new(false); } macro_rules! vrb { ( $( $e:expr ),* ) => {( - if VERBOSITY.lock().unwrap() { println!( $( $e ),* ) } + if *VERBOSITY.lock().unwrap() { println!( $( $e ),* ) } )} } -const PALETTE_SIZE: usize = 16_usize; -const PALETTE_BYTES: usize = PALETTE_SIZE * 3_usize; // 16 * sizeof(int) +const PALETTE_SIZE: usize = 16; +const PALETTE_BYTES: usize = PALETTE_SIZE * 3; // 16 * sizeof(int) -const RAW_COLEXPR_SIZE: usize = 6_usize; // e. g. 0xBADF00 +const RAW_COLEXPR_SIZE: usize = 6; // e. g. 0xBADF00 type RawPalette<'a> = [&'a str; PALETTE_SIZE]; @@ -74,10 +75,13 @@ impl Color s.to_string() } } +} /* [impl Color] */ - fn to_string(&self) -> String +impl fmt::Display for Color +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { + let c = match *self { Color::Black(b) => Color::format_brightness(b, "black"), Color::Red(b) => Color::format_brightness(b, "red"), Color::Green(b) => Color::format_brightness(b, "green"), @@ -86,9 +90,11 @@ impl Color Color::Magenta(b) => Color::format_brightness(b, "magenta"), Color::Cyan(b) => Color::format_brightness(b, "cyan"), Color::White(b) => Color::format_brightness(b, "white"), - } + }; + + write!(f, "{}", c) } -} /* [impl Color] */ +} /* [impl fmt::Display for Color] */ #[derive(Debug)] enum Scheme @@ -99,24 +105,19 @@ enum Scheme Custom(Option), } -impl<'a> std::fmt::Display for Scheme +impl<'a> fmt::Display for Scheme { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let id: &str = match *self { - Scheme::Default => "default", - Scheme::SolarizedDark => "solarized_dark", - Scheme::SolarizedLight => "solarized_light", - Scheme::Custom(None) => "", - Scheme::Custom(Some(fname)) => fname.display(), - }; - write!(f, "{}", id) + match self { + Scheme::Default => write!(f, "default"), + Scheme::SolarizedDark => write!(f, "solarized_dark"), + Scheme::SolarizedLight => write!(f, "solarized_light"), + Scheme::Custom(None) => write!(f, ""), + Scheme::Custom(Some(fname)) => write!(f, "{}", fname.display()), + } } -} /* [impl std::fmt::String for Scheme] */ - -extern "C" { - fn exit(code: libc::c_int) -> !; -} +} /* [impl fmt::String for Scheme] */ /* struct Job -- Runtime parameters. */ @@ -184,7 +185,7 @@ impl<'a> Job .get_matches(); if matches.is_present("v") { - VERBOSITY.lock().unwrap() = true; + *VERBOSITY.lock().unwrap() = true; } if matches.is_present("l") { @@ -240,7 +241,7 @@ impl<'a> Job Scheme::SolarizedLight => Job::dump_scheme(&SOLARIZED_COLORS_LIGHT), Scheme::Custom(None) => Job::dump_palette(Palette::from_stdin()), Scheme::Custom(Some(fname)) => - Job::dump_palette(Palette::from_file(&fname)), + Job::dump_palette(Palette::from_file(&fname)), } } @@ -266,7 +267,7 @@ extern "C" { ) -> libc::c_int; } -static CONSOLE_PATHS: [&'static str; 6] = [ +static CONSOLE_PATHS: [&str; 6] = [ "/proc/self/fd/0", "/dev/tty", "/dev/tty0", @@ -275,25 +276,25 @@ static CONSOLE_PATHS: [&'static str; 6] = [ "/dev/console", ]; -static DEFAULT_COLORS: RawPalette<'static> = [ +static DEFAULT_COLORS: RawPalette = [ "000000", "aa0000", "00aa00", "aa5500", "0000aa", "aa00aa", "00aaaa", "aaaaaa", "555555", "ff5555", "55ff55", "ffff55", "5555ff", "ff55ff", "55ffff", "ffffff", ]; -static SOLARIZED_COLORS_DARK: RawPalette<'static> = [ +static 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<'static> = [ +static SOLARIZED_COLORS_LIGHT: RawPalette = [ "eee8d5", "dc322f", "859900", "b58900", "268bd2", "d33682", "2aa198", "073642", "fdf6e3", "cb4b16", "93a1a1", "839496", "657b83", "6c71c4", "586e75", "002b36", ]; -static DUMMY_COLORS: RawPalette<'static> = [ +static DUMMY_COLORS: RawPalette = [ "000000", "ffffff", "000000", "ffffff", "000000", "ffffff", "000000", "ffffff", "000000", "ffffff", "000000", "ffffff", "000000", "ffffff", "000000", "ffffff", @@ -308,21 +309,19 @@ impl Palette { fn dump(&self) { - let mut i: usize = 0_usize; let mut buf: [u8; 3] = [0u8, 0u8, 0u8]; - for col in self.colors.iter() { + for (i, col) in self.colors.iter().enumerate() { let idx: usize = i % 3; buf[idx] = *col; - if idx == 2_usize { + if idx == 2 { println!( "{:>15} => 0x{:02.X}{:02.X}{:02.X}", Color::of_value((i / 3) as u8).to_string(), - buf[0_usize], - buf[1_usize], - buf[2_usize] + buf[0], + buf[1], + buf[2] ); } - i = i + 1; } } } /* [impl Palette] */ @@ -330,17 +329,16 @@ impl Palette fn nibble_of_char(chr: u8) -> u8 { match chr as char { - '0'...'9' => chr - '0' as u8, - 'a'...'f' => chr - 'a' as u8 + 10, - 'A'...'F' => chr - 'A' as u8 + 10, + '0'..='9' => chr - '0' as u8, + 'a'..='f' => chr - 'a' as u8 + 10, + 'A'..='F' => chr - 'A' as u8 + 10, _ => 0, } } macro_rules! byte_of_hex { ($ar:ident, $off:expr) => { - (nibble_of_char($ar[$off])) << 4 - | nibble_of_char($ar[$off + 1_usize]) as u8 + (nibble_of_char($ar[$off])) << 4 | nibble_of_char($ar[$off + 1]) as u8 }; } @@ -357,16 +355,16 @@ impl Palette { pub fn new(colors: &[&str; PALETTE_SIZE]) -> Palette { - let mut idx: usize = 0_usize; + let mut idx: usize = 0; let mut pal: [u8; PALETTE_BYTES] = [0; PALETTE_BYTES]; for def in colors.iter() { let (r, g, b) = rgb_of_hex_triplet(*def); - pal[idx + 0_usize] = r; - pal[idx + 1_usize] = g; - pal[idx + 2_usize] = b; + pal[idx + 0] = r; + pal[idx + 1] = g; + pal[idx + 2] = b; //println!(">> {} -> {:X} {:X} {:X}", def, r, g, b); - idx = idx + 3_usize; + idx = idx + 3; } Palette { colors: pal } @@ -378,44 +376,42 @@ impl Palette /* [Palette::dummy] */ - pub fn from_buffered_reader(reader: &mut std::io::BufRead) -> Palette + pub fn from_buffered_reader(reader: &mut dyn std::io::BufRead) -> Palette { - let mut pal_idx: usize = 0_usize; + let mut pal_idx: usize = 0; let mut pal: [u8; PALETTE_BYTES] = [0; PALETTE_BYTES]; let mut line: String = String::new(); while reader.read_line(&mut line).is_ok() { let len = line.len(); - if len == 0_usize { + if len == 0 { break; - } else if len >= 8_usize { + } else if len >= 8 { if let Some(off) = line.find('#') { - if off != 0_usize { + if off != 0 { /* Palette index specified, number prepended */ - let str_idx = - unsafe { line.slice_unchecked(0_usize, off) }; let parse_res: Result = - std::str::FromStr::from_str(str_idx); + std::str::FromStr::from_str(&line[0..off]); match parse_res { Ok(new_idx) => if new_idx < PALETTE_SIZE { - pal_idx = new_idx * 3_usize; + pal_idx = new_idx * 3; }, _ => (), } } - let off = off + 1_usize; - if off > len - 6_usize { + let off = off + 1; + if off > len - 6 { /* no room left for color definition after '#' char */ panic!("invalid color definition: {}", line); } let col = &line[off..(off + RAW_COLEXPR_SIZE)]; let (r, g, b) = rgb_of_hex_triplet(col); - pal[pal_idx + 0_usize] = r; - pal[pal_idx + 1_usize] = g; - pal[pal_idx + 2_usize] = b; - pal_idx = (pal_idx + 3_usize) % PALETTE_BYTES; + pal[pal_idx + 0] = r; + pal[pal_idx + 1] = g; + pal[pal_idx + 2] = b; + pal_idx = (pal_idx + 3) % PALETTE_BYTES; } } line.truncate(0); @@ -431,7 +427,8 @@ impl Palette /* Check if file exists */ let file = match std::fs::File::open(&fname) { - Err(e) => panic!("failed to open {} as file ({})", fname, e), + Err(e) => + panic!("failed to open {} as file ({})", fname.display(), e), Ok(f) => f, }; let mut reader = std::io::BufReader::new(file); @@ -455,39 +452,39 @@ impl Palette } /* [Palette::from_stdin] */ } /* [impl Palette] */ -impl std::fmt::Display for Palette +impl fmt::Display for Palette { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let mut i: usize = 0_usize; + let mut i: usize = 0; while i < PALETTE_BYTES { let _ = write!(f, "{}", if i == 0 { "(" } else { "\n " }); - let r = self.colors[i + 0_usize]; - let g = self.colors[i + 1_usize]; - let b = self.colors[i + 2_usize]; + let r = self.colors[i + 0]; + let g = self.colors[i + 1]; + let b = self.colors[i + 2]; let _ = write!( f, "((r 0x{:02.X}) (g 0x{:02.X}) (b 0x{:02.x}))", r, g, b ); - i = i + 3_usize; + i = i + 3; } - write!(f, ")\n") + writeln!(f, ")") } -} /* [impl std::fmt::Display for Palette] */ +} /* [impl fmt::Display for Palette] */ -impl std::fmt::Debug for Palette +impl fmt::Debug for Palette { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let mut i: u8 = 0_u8; while (i as usize) < PALETTE_BYTES { - let r = self.colors[i as usize + 0_usize]; - let g = self.colors[i as usize + 1_usize]; - let b = self.colors[i as usize + 2_usize]; - let _ = write!( + let r = self.colors[i as usize + 0]; + let g = self.colors[i as usize + 1]; + let b = self.colors[i as usize + 2]; + let _ = writeln!( f, - "{} => 0x{:02.X}{:02.X}{:02.X}\n", + "{} => 0x{:02.X}{:02.X}{:02.X}", Color::of_value(i).to_string(), r, g, @@ -497,7 +494,7 @@ impl std::fmt::Debug for Palette } std::result::Result::Ok(()) } -} /* [impl std::fmt::Debug for Palette] */ +} /* [impl fmt::Debug for Palette] */ fn fd_of_path(path: &std::path::Path) -> Option { @@ -539,17 +536,17 @@ fn get_console_fd(path: Option<&str>) -> Option match path { Some(path) => { let path = std::path::Path::new(path); - match fd_of_path(&path) { + match fd_of_path(path) { Some(fd) => Some(fd), None => panic!("cannot open {:?} as a tty", path), } }, None => { let mut it = CONSOLE_PATHS.iter(); - while let Some(&path) = it.next() { + while let Some(path) = it.next() { vrb!("trying path: {:?}", path); let path = std::path::Path::new(path); - if let Some(fd) = fd_of_path(&path) { + if let Some(fd) = fd_of_path(path) { vrb!(" * Success!"); return Some(fd); } @@ -579,14 +576,13 @@ fn main() { let job = Job::from_argv(); vrb!("job parms: {:?}", job); - 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), - Scheme::Custom(None) => Palette::from_stdin(), - Scheme::Custom(Some(fname)) => Palette::from_file(&fname), - }; + 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), + Scheme::Custom(None) => Palette::from_stdin(), + Scheme::Custom(Some(ref fname)) => Palette::from_file(fname), + }; vrb!("Using palette:"); vrb!("{}", pal); let fd = get_console_fd(None).unwrap(); -- cgit v1.2.3