melib/gpgme: impl Display for gpgme::Key
Implement display to show more info on the user interface other than just the fingerprint. Will show the fingerprint, the primary user id and a list of properties/flags: - revoked status - expired status - disabled status - invalid status - can encrypt status - can sign status - whether it's a secret or a public key Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>
This commit is contained in:
parent
60c90d7549
commit
6b3636013c
2 changed files with 63 additions and 2 deletions
|
@ -736,7 +736,7 @@ To: {}
|
|||
.gpg_state
|
||||
.sign_keys
|
||||
.iter()
|
||||
.map(|k| k.fingerprint())
|
||||
.map(|k| k.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
|
||||
|
@ -786,7 +786,7 @@ To: {}
|
|||
.gpg_state
|
||||
.encrypt_keys
|
||||
.iter()
|
||||
.map(|k| k.fingerprint())
|
||||
.map(|k| k.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
|
||||
|
|
|
@ -128,6 +128,67 @@ impl Key {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Key {
|
||||
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(fmt, "{} ", self.fingerprint())?;
|
||||
if let Some(uid) = self.primary_uid() {
|
||||
write!(fmt, "{}", uid)?;
|
||||
} else {
|
||||
write!(fmt, "(missing primary uid)")?;
|
||||
}
|
||||
// Write some properties as a list inside square brackets
|
||||
write!(fmt, " [")?;
|
||||
{
|
||||
let revoked = self.revoked();
|
||||
let expired = self.expired();
|
||||
let disabled = self.disabled();
|
||||
let invalid = self.invalid();
|
||||
let can_encrypt = self.can_encrypt();
|
||||
let can_sign = self.can_sign();
|
||||
let secret = self.secret();
|
||||
let mut empty = true;
|
||||
macro_rules! write_property {
|
||||
($cond:ident, $lit:literal, $else:literal$(,)?) => {{
|
||||
if !empty {
|
||||
write!(fmt, ",")?;
|
||||
}
|
||||
if $cond {
|
||||
write!(fmt, $lit)?;
|
||||
} else {
|
||||
write!(fmt, $else)?;
|
||||
}
|
||||
empty = false;
|
||||
}};
|
||||
($cond:ident, $lit:literal$(,)?) => {{
|
||||
if $cond {
|
||||
if !empty {
|
||||
write!(fmt, ",")?;
|
||||
}
|
||||
write!(fmt, $lit)?;
|
||||
empty = false;
|
||||
}
|
||||
}};
|
||||
}
|
||||
macro_rules! write_properties {
|
||||
($(($cond:ident, $lit:literal $(, $else:literal)?)),*$(,)?) => {{
|
||||
$(write_property!($cond, $lit $(, $else)*);)*
|
||||
}};
|
||||
}
|
||||
write_properties! {
|
||||
(revoked, "revoked"),
|
||||
(expired, "expired"),
|
||||
(disabled, "disabled"),
|
||||
(invalid, "invalid"),
|
||||
(can_encrypt, "can encrypt"),
|
||||
(can_sign, "can sign"),
|
||||
(secret, "secret", "public"),
|
||||
}
|
||||
_ = empty;
|
||||
}
|
||||
write!(fmt, "]")
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Key {
|
||||
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
fmt.debug_struct(crate::identify!(Key))
|
||||
|
|
Loading…
Add table
Reference in a new issue