Skip to content

Commit

Permalink
Lazily calculate value when using StyledTable::add_kv_row_if and pred…
Browse files Browse the repository at this point in the history
…icate is true

We use StyledTable::add_kv_row_if to check if a value exists. We should only access the
value if the predicate passes. That's why this command changes the value parameter into
a closure that is evaluated if the predicate is true.

This fixes #1631.
  • Loading branch information
tillrohrmann committed Jun 14, 2024
1 parent 3271b79 commit 49163e5
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cli/src/commands/invocations/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async fn describe(env: &CliEnv, opts: &Describe) -> Result<()> {
table.add_kv_row_if(
|| inv.state_modified_at.is_some(),
"Modified at:",
format!("{}", &inv.state_modified_at.unwrap()),
|| format!("{}", &inv.state_modified_at.unwrap()),
);

c_title!("📜", "Invocation Information");
Expand Down
2 changes: 1 addition & 1 deletion cli/src/ui/deployments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub fn add_deployment_to_kv_table(deployment: &Deployment, table: &mut Table) {
table.add_kv_row_if(
|| assume_role_arn.is_some(),
"Deployment Assume Role ARN:",
assume_role_arn.as_ref().unwrap(),
|| assume_role_arn.as_ref().unwrap(),
);

table.add_kv_row("Endpoint:", arn);
Expand Down
4 changes: 2 additions & 2 deletions crates/cli-util/src/ui/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ pub trait StyledTable {
fn new_styled() -> Self;
fn set_styled_header<T: ToString>(&mut self, headers: Vec<T>) -> &mut Self;
fn add_kv_row<V: Into<comfy_table::Cell>>(&mut self, key: &str, value: V) -> &mut Self;
fn add_kv_row_if<P: Fn() -> bool, V: Display>(
fn add_kv_row_if<P: Fn() -> bool, V: Fn() -> D, D: Display>(
&mut self,
predicate: P,
key: &str,
value: V,
) -> &mut Self {
if predicate() {
self.add_kv_row(key, value)
self.add_kv_row(key, value())
} else {
self
}
Expand Down

0 comments on commit 49163e5

Please sign in to comment.