From d8faf4a20465806f51834bd053bdb16385cf9bec Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Wed, 10 Nov 2021 19:49:14 +0100 Subject: clean up struct Palette --- src/vtcol.rs | 86 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/src/vtcol.rs b/src/vtcol.rs index 90aca21..e1b4d6a 100644 --- a/src/vtcol.rs +++ b/src/vtcol.rs @@ -244,7 +244,7 @@ impl<'a> Job fn dump_scheme(colors: &[&str; PALETTE_SIZE]) { - let pal: Palette = Palette::new(colors); + let pal: Palette = Palette::from(colors); pal.dump(); } @@ -264,9 +264,9 @@ impl<'a> Job fn set_scheme(scheme: Scheme) -> Result<()> { let pal: Palette = match scheme { - Scheme::Default => Palette::new(&DEFAULT_COLORS), - Scheme::SolarizedDark => Palette::new(&SOLARIZED_COLORS_DARK), - Scheme::SolarizedLight => Palette::new(&SOLARIZED_COLORS_LIGHT), + Scheme::Default => Palette::from(&DEFAULT_COLORS), + Scheme::SolarizedDark => Palette::from(&SOLARIZED_COLORS_DARK), + Scheme::SolarizedLight => Palette::from(&SOLARIZED_COLORS_LIGHT), Scheme::Custom(None) => Palette::from_stdin(), Scheme::Custom(Some(ref fname)) => Palette::from_file(fname), }; @@ -330,29 +330,6 @@ static DUMMY_COLORS: RawPalette = [ "000000", "ffffff", ]; -pub struct Palette([u8; PALETTE_BYTES]); - -impl Palette -{ - fn dump(&self) - { - let mut buf: [u8; 3] = [0u8, 0u8, 0u8]; - for (i, col) in self.0.iter().enumerate() { - let idx: usize = i % 3; - buf[idx] = *col; - if idx == 2 { - println!( - "{:>15} => 0x{:02.X}{:02.X}{:02.X}", - Color::of_value((i / 3) as u8).to_string(), - buf[0], - buf[1], - buf[2] - ); - } - } - } -} /* [impl Palette] */ - fn nibble_of_char(chr: u8) -> u8 { match chr { @@ -378,9 +355,17 @@ fn rgb_of_hex_triplet(def: &str) -> (u8, u8, u8) (r, g, b) } +pub struct Palette([u8; PALETTE_BYTES]); + impl Palette { - pub fn new(colors: &[&str; PALETTE_SIZE]) -> Palette + /** Construct an all-zero ``Palette``. */ + pub fn new() -> Self { Self([0u8; PALETTE_BYTES]) } + + /* [Palette::new] */ + + /** Obtain a ``Palette`` from a ``RawPalette``. */ + pub fn from(colors: &RawPalette) -> Self { let mut idx: usize = 0; let mut pal: [u8; PALETTE_BYTES] = [0; PALETTE_BYTES]; @@ -394,16 +379,14 @@ impl Palette idx += 3; } - Palette (pal) + Self(pal) } - /* [Palette::new] */ - - pub fn dummy() -> Palette { Palette::new(&DUMMY_COLORS) } + /* [Palette::from] */ - /* [Palette::dummy] */ + pub fn dummy() -> Self { Self::from(&DUMMY_COLORS) } - pub fn from_buffered_reader(reader: &mut dyn std::io::BufRead) -> Palette + pub fn from_buffered_reader(reader: &mut dyn std::io::BufRead) -> Self { let mut pal_idx: usize = 0; let mut pal: [u8; PALETTE_BYTES] = [0; PALETTE_BYTES]; @@ -442,30 +425,47 @@ impl Palette line.truncate(0); } - Palette (pal) + Self(pal) } - /* [Palette::from_buffered_reader] */ + fn dump(&self) + { + let mut buf: [u8; 3] = [0u8, 0u8, 0u8]; + for (i, col) in self.0.iter().enumerate() { + let idx: usize = i % 3; + buf[idx] = *col; + if idx == 2 { + println!( + "{:>15} => 0x{:02.X}{:02.X}{:02.X}", + Color::of_value((i / 3) as u8).to_string(), + buf[0], + buf[1], + buf[2] + ); + } + } + } - pub fn from_file(fname: &Path) -> Palette + pub fn from_file(fname: &Path) -> Self { /* Check if file exists */ let file = match std::fs::File::open(&fname) { - Err(e) => - panic!("failed to open {} as file ({})", fname.display(), e), + Err(e) => { + panic!("failed to open {} as file ({})", fname.display(), e) + }, Ok(f) => f, }; let mut reader = std::io::BufReader::new(file); /* Parse scheme file */ - Palette::from_buffered_reader(&mut reader) + Self::from_buffered_reader(&mut reader) } /* [Palette::from_file] */ - pub fn from_stdin() -> Palette + pub fn from_stdin() -> Self { vrb!("Go ahead, type your color scheme …"); vrb!("vtcol>"); @@ -473,7 +473,7 @@ impl Palette /* Parse scheme file */ - Palette::from_buffered_reader(&mut reader) + Self::from_buffered_reader(&mut reader) } /* [Palette::from_stdin] */ } /* [impl Palette] */ @@ -601,7 +601,7 @@ fn ioctl_pio_cmap(fd: Fd, pal: &Palette) -> Result<()> { if unsafe { ioctl(fd, PIO_CMAP, std::mem::transmute(pal)) } < 0 { Err(anyhow!( - "PIO_CMAP, ioctl failed to insert new palette: {}", + "ioctl(PIO_CMAP) failed to insert new palette: {}", Error::last_os_error() )) } else { -- cgit v1.2.3