From 6086134d416f036c8e98cbe78bd322922e170f3a Mon Sep 17 00:00:00 2001 From: yama Date: Sun, 3 Nov 2024 21:56:06 +0900 Subject: [PATCH] feat: include package revision hash in `ya pack --list` (#1884) Co-authored-by: sxyazi --- yazi-cli/src/package/parser.rs | 13 ++++++++----- yazi-shared/src/event/cmd.rs | 18 +++++++----------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/yazi-cli/src/package/parser.rs b/yazi-cli/src/package/parser.rs index 49a22a403..7322ebf9d 100644 --- a/yazi-cli/src/package/parser.rs +++ b/yazi-cli/src/package/parser.rs @@ -46,8 +46,8 @@ impl Package { let deps = deps.as_array_mut().context("`deps` must be an array")?; for dep in deps.iter_mut() { let dep = dep.as_inline_table_mut().context("Dependency must be an inline table")?; - let use_ = dep.get("use").and_then(|d| d.as_str()).context("Missing `use` field")?; - let rev = dep.get("rev").and_then(|d| d.as_str()); + let use_ = dep.get("use").and_then(Value::as_str).context("Missing `use` field")?; + let rev = dep.get("rev").and_then(Value::as_str); let mut package = Package::new(use_, rev); if upgrade { @@ -81,8 +81,11 @@ impl Package { println!("{section}s:"); for dep in deps { - if let Some(Value::String(use_)) = dep.as_inline_table().and_then(|t| t.get("use")) { - println!("\t{}", use_.value()); + let Some(dep) = dep.as_inline_table() else { continue }; + match (dep.get("use").and_then(Value::as_str), dep.get("rev").and_then(Value::as_str)) { + (Some(use_), None) => println!("\t{use_}"), + (Some(use_), Some(rev)) => println!("\t{use_} ({rev})"), + _ => {} } } Ok(()) @@ -119,7 +122,7 @@ impl Package { fn same(v: &Value, name: &str) -> bool { v.as_inline_table() .and_then(|t| t.get("use")) - .and_then(|v| v.as_str()) + .and_then(Value::as_str) .is_some_and(|s| Package::new(s, None).name() == Some(name)) } diff --git a/yazi-shared/src/event/cmd.rs b/yazi-shared/src/event/cmd.rs index 66701b7bf..251adb417 100644 --- a/yazi-shared/src/event/cmd.rs +++ b/yazi-shared/src/event/cmd.rs @@ -57,20 +57,16 @@ impl Cmd { pub fn get(&self, name: &str) -> Option<&Data> { self.args.get(name) } #[inline] - pub fn str(&self, name: &str) -> Option<&str> { self.args.get(name).and_then(Data::as_str) } + pub fn str(&self, name: &str) -> Option<&str> { self.get(name).and_then(Data::as_str) } #[inline] - pub fn bool(&self, name: &str) -> bool { - self.args.get(name).and_then(Data::as_bool).unwrap_or(false) - } + pub fn bool(&self, name: &str) -> bool { self.maybe_bool(name).unwrap_or(false) } #[inline] - pub fn maybe_bool(&self, name: &str) -> Option { - self.args.get(name).and_then(Data::as_bool) - } + pub fn maybe_bool(&self, name: &str) -> Option { self.get(name).and_then(Data::as_bool) } #[inline] - pub fn first(&self) -> Option<&Data> { self.args.get("0") } + pub fn first(&self) -> Option<&Data> { self.get("0") } // --- Take #[inline] @@ -78,15 +74,15 @@ impl Cmd { #[inline] pub fn take_str(&mut self, name: &str) -> Option { - if let Some(Data::String(s)) = self.args.remove(name) { Some(s) } else { None } + if let Some(Data::String(s)) = self.take(name) { Some(s) } else { None } } #[inline] - pub fn take_first(&mut self) -> Option { self.args.remove("0") } + pub fn take_first(&mut self) -> Option { self.take("0") } #[inline] pub fn take_first_str(&mut self) -> Option { - if let Some(Data::String(s)) = self.args.remove("0") { Some(s) } else { None } + if let Some(Data::String(s)) = self.take_first() { Some(s) } else { None } } #[inline]