summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Gesang <phg@phi-gamma.net>2021-11-12 00:01:57 +0100
committerPhilipp Gesang <phg@phi-gamma.net>2021-11-12 00:02:02 +0100
commita9958bf09f75c2916736ebdf106cf0cde33ebdf2 (patch)
treec1efe6a54b0e128b7e7ff1ffc2ef80236d33bd40
parentb9464579c3c69b998a7e725e29f58189d587f839 (diff)
downloadvtcol-a9958bf09f75c2916736ebdf106cf0cde33ebdf2.tar.gz
bring “dump” out put in line with scheme format
This revamps the output in a way that allows feeding the result back into vtcol: $ vtcol dump solarized |vtcol set -
-rw-r--r--src/vtcol.rs60
1 files 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<io::Stdout>) -> 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