feat: optimize info outputs

This commit is contained in:
2023-10-15 22:56:24 +08:00
parent f281cc15a2
commit ab2e850435
2 changed files with 17 additions and 2 deletions

View File

@@ -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))
));
})
}

View File

@@ -2,6 +2,10 @@ use crate::config::{TinyEncryptConfig, TinyEncryptConfigEnvelop};
use crate::spec::TinyEncryptEnvelop;
pub fn format_envelop(envelop: &TinyEncryptEnvelop, config: &Option<TinyEncryptConfig>) -> String {
format_envelop_with_type_with(envelop, config, None)
}
pub fn format_envelop_with_type_with(envelop: &TinyEncryptEnvelop, config: &Option<TinyEncryptConfig>, type_width: Option<usize>) -> 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<TinyEncryptC
let desc = envelop_desc.as_ref()
.map(|desc| format!(", Desc: {}", desc))
.unwrap_or_else(|| "".to_string());
format!("{}{}{}", envelop.r#type.get_upper_name(), envelop_kid, desc)
format!("{}{}{}", with_with(&envelop.r#type.get_upper_name(), type_width), envelop_kid, desc)
}
fn get_envelop_desc(envelop: &TinyEncryptEnvelop, config_envelop: &Option<&TinyEncryptConfigEnvelop>) -> Option<String> {
@@ -22,3 +26,14 @@ fn get_envelop_desc(envelop: &TinyEncryptEnvelop, config_envelop: &Option<&TinyE
}
None
}
fn with_with(s: &str, width: Option<usize>) -> String {
match width {
None => s.to_string(),
Some(w) => if s.len() < w {
format!("{}{}", s, " ".repeat(w - s.len()))
} else {
s.to_string()
}
}
}