From c5305231d900cca8189a9995284258a841042f50 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Wed, 24 Nov 2021 21:08:17 +0100 Subject: =?UTF-8?q?add=20base64=20input=20for=20=E2=80=98set=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib.rs | 31 +++++++++++++++++++++++++++++++ src/vtcol.rs | 14 ++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 9914f77..2dee9ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -571,9 +571,40 @@ impl Palette Self::from_buffered_reader(&mut reader) } + fn from_bytes(b: &[u8]) -> io::Result + { + if b.len() != PALETTE_SIZE * 3 { + return Err(io::Error::new( + io::ErrorKind::Other, + format!( + "expected {} B of data, got {}", + PALETTE_SIZE * 3, + b.len() + ), + )); + } + + let mut res = Self::new(); + res.0.copy_from_slice(&b); + + Ok(res) + } + /* [Palette::from_stdin] */ pub fn base64(&self) -> String { base64::encode(&self.0) } + + pub fn from_base64(b64: &str) -> io::Result + { + base64::decode(b64.as_bytes()) + .map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!("failed to decode input as base64: {}", e), + ) + }) + .and_then(|b| Self::from_bytes(&b)) + } } /* [impl Palette] */ impl fmt::Display for Palette diff --git a/src/vtcol.rs b/src/vtcol.rs index 0a198e6..8161241 100644 --- a/src/vtcol.rs +++ b/src/vtcol.rs @@ -76,6 +76,15 @@ impl<'a> Job .value_name("PATH") .help("apply scheme from file") .takes_value(true), + ) + .arg( + Arg::with_name("base64") + .short("b") + .long("base64") + .help("base64 encoded binary input") + .value_name("DATA") + .required(false) + .takes_value(true), ), ) .subcommand( @@ -174,6 +183,11 @@ impl<'a> Job }, ("list", _) => Ok(Self::List), ("set", Some(subm)) => { + if let Some(b64) = subm.value_of("base64") { + let scheme = + Palette::from_base64(&b64).map(Scheme::from)?; + return Ok(Self::Set(scheme)); + } let scheme = match subm.value_of("scheme") { Some("-") => Self::read_scheme_from_stdin(), Some(name) => { -- cgit v1.2.3