From ab2e85043597b006846d466e114243de6bd13909 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 15 Oct 2023 22:56:24 +0800 Subject: [PATCH] feat: optimize info outputs --- src/cmd_info.rs | 2 +- src/util_envelop.rs | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/cmd_info.rs b/src/cmd_info.rs index 13cd256..1f40eda 100644 --- a/src/cmd_info.rs +++ b/src/cmd_info.rs @@ -75,7 +75,7 @@ pub fn info_single(path: &PathBuf, cmd_info: &CmdInfo) -> XResult<()> { envelops.iter().enumerate().for_each(|(i, envelop)| { infos.push(format!("{}: {}", header(&format!("Envelop #{}", i + 1)), - util_envelop::format_envelop(envelop, &config) + util_envelop::format_envelop_with_type_with(envelop, &config, Some(10)) )); }) } diff --git a/src/util_envelop.rs b/src/util_envelop.rs index 01bc98b..ae0e19a 100644 --- a/src/util_envelop.rs +++ b/src/util_envelop.rs @@ -2,6 +2,10 @@ use crate::config::{TinyEncryptConfig, TinyEncryptConfigEnvelop}; use crate::spec::TinyEncryptEnvelop; pub fn format_envelop(envelop: &TinyEncryptEnvelop, config: &Option) -> String { + format_envelop_with_type_with(envelop, config, None) +} + +pub fn format_envelop_with_type_with(envelop: &TinyEncryptEnvelop, config: &Option, type_width: Option) -> String { let config_envelop = config.as_ref().and_then(|c| c.find_by_kid(&envelop.kid)); let envelop_kid = config_envelop.and_then(|e| e.sid.as_ref()) .map(|sid| format!(", Sid: {}", sid)) @@ -10,7 +14,7 @@ pub fn format_envelop(envelop: &TinyEncryptEnvelop, config: &Option) -> Option { @@ -22,3 +26,14 @@ fn get_envelop_desc(envelop: &TinyEncryptEnvelop, config_envelop: &Option<&TinyE } None } + +fn with_with(s: &str, width: Option) -> String { + match width { + None => s.to_string(), + Some(w) => if s.len() < w { + format!("{}{}", s, " ".repeat(w - s.len())) + } else { + s.to_string() + } + } +}