From a9958bf09f75c2916736ebdf106cf0cde33ebdf2 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Fri, 12 Nov 2021 00:01:57 +0100 Subject: =?UTF-8?q?bring=20=E2=80=9Cdump=E2=80=9D=20out=20put=20in=20line?= =?UTF-8?q?=20with=20scheme=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This revamps the output in a way that allows feeding the result back into vtcol: $ vtcol dump solarized |vtcol set - --- src/vtcol.rs | 60 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/src/vtcol.rs b/src/vtcol.rs index 3b8f59a..3acd1d3 100644 --- a/src/vtcol.rs +++ b/src/vtcol.rs @@ -1,7 +1,7 @@ use anyhow::{anyhow, Result}; use std::{convert::TryFrom, fmt, - io::Error, + io::{self, BufWriter, Error, Write}, path::{Path, PathBuf}, sync::atomic::{AtomicBool, Ordering}}; @@ -276,7 +276,7 @@ impl<'a> Job .help("name of the scheme") .required(true) .value_name("NAME") - .takes_value(true) + .takes_value(true), ), ) .subcommand( @@ -394,31 +394,25 @@ impl<'a> Job } } - fn dump(scm: Scheme) + fn dump(scm: Scheme) -> Result<()> { vrb!("Dumping color scheme {}", scm); + let mut out = BufWriter::new(io::stdout()); + match scm { Scheme::Builtin(Builtin { palette, .. }) => - Self::dump_scheme(palette), - Scheme::Custom(None) => Self::dump_palette(Palette::from_stdin()), + Palette::from(palette).dump(&mut out), + Scheme::Custom(None) => Palette::from_stdin().dump(&mut out), Scheme::Custom(Some(fname)) => - Self::dump_palette(Palette::from_file(&fname)), - Scheme::Palette(pal) => Self::dump_palette(pal), + Palette::from_file(&fname).dump(&mut out), + Scheme::Palette(pal) => pal.dump(&mut out), } } - fn dump_scheme(colors: &[&str; PALETTE_SIZE]) - { - let pal: Palette = Palette::from(colors); - pal.dump(); - } - - fn dump_palette(pal: Palette) { pal.dump() } - fn run(self) -> Result<()> { match self { - Self::Dump(scm) => Self::dump(scm), + Self::Dump(scm) => Self::dump(scm)?, Self::List => Self::list_schemes(), Self::Set(scm) => Self::set_scheme(scm)?, Self::Get => Self::get_scheme()?, @@ -609,22 +603,40 @@ impl Palette Self(pal) } - fn dump(&self) + /** Print palette in a text format that can be re-read back in. + Basically we print one hex rgb code per line prefixed with color + indices and the canonical names on the right: + + 00 #002B36 black + 01 #DC322F red + 02 #859900 green + 03 #B58900 yellow + 04 #268BD2 blue + … + */ + fn dump(&self, out: &mut BufWriter) -> Result<()> { 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 { - let col = Color::try_from((i / 3) as u8) - .map(|c| format!("{}", c)) - .unwrap_or_else(|_| "??".into()); - println!( - "{:>15} => 0x{:02.X}{:02.X}{:02.X}", - col, buf[0], buf[1], buf[2] - ); + let col = Color::try_from((i / 3) as u8)?; + out.write_all( + format!( + "{:02} #{:02.X}{:02.X}{:02.X} {}\n", + i / 3, + buf[0], + buf[1], + buf[2], + col, + ) + .as_bytes(), + )?; } } + + Ok(()) } pub fn from_file(fname: &Path) -> Self -- cgit v1.2.3