Skip to content

Commit b4009e9

Browse files
committed
Rewrite UTXOs display with comfy_table
1 parent 6ef701f commit b4009e9

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

crates/cli-client/src/cli/swap.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,28 @@ use simplicityhl::elements::pset::serialize::Serialize;
2626
use simplicityhl::simplicity::hex::DisplayHex;
2727
use simplicityhl_core::{LIQUID_TESTNET_BITCOIN_ASSET, LIQUID_TESTNET_GENESIS};
2828

29-
pub(crate) struct LocalSwapData {
29+
pub struct LocalSwapData {
3030
pub(crate) swap_args: SwapWithChangeArguments,
3131
pub(crate) taproot_pubkey_gen: contracts::sdk::taproot_pubkey_gen::TaprootPubkeyGen,
3232
pub(crate) metadata: ContractMetadata,
3333
pub(crate) current_outpoint: simplicityhl::elements::OutPoint,
3434
pub(crate) current_value: u64,
3535
}
3636

37-
pub(crate) struct LocalCancellableSwap {
37+
pub struct LocalCancellableSwap {
3838
pub(crate) swap_args: SwapWithChangeArguments,
3939
pub(crate) taproot_pubkey_gen: contracts::sdk::taproot_pubkey_gen::TaprootPubkeyGen,
4040
pub(crate) metadata: ContractMetadata,
4141
}
4242

43-
pub(crate) struct LocalWithdrawableSwap {
43+
pub struct LocalWithdrawableSwap {
4444
pub(crate) swap_args: SwapWithChangeArguments,
4545
pub(crate) taproot_pubkey_gen: contracts::sdk::taproot_pubkey_gen::TaprootPubkeyGen,
4646
pub(crate) metadata: ContractMetadata,
4747
pub(crate) settlement_amount: u64,
4848
}
4949

50-
pub(crate) struct ActiveSwapDisplay {
50+
pub struct ActiveSwapDisplay {
5151
pub(crate) index: usize,
5252
pub(crate) offering: String,
5353
pub(crate) price: String,
@@ -56,15 +56,15 @@ pub(crate) struct ActiveSwapDisplay {
5656
pub(crate) seller: String,
5757
}
5858

59-
pub(crate) struct CancellableSwapDisplay {
59+
pub struct CancellableSwapDisplay {
6060
pub(crate) index: usize,
6161
pub(crate) collateral: String,
6262
pub(crate) asset: String,
6363
pub(crate) expired: String,
6464
pub(crate) contract: String,
6565
}
6666

67-
pub(crate) struct WithdrawableSwapDisplay {
67+
pub struct WithdrawableSwapDisplay {
6868
pub(crate) index: usize,
6969
pub(crate) settlement: String,
7070
pub(crate) asset: String,

crates/cli-client/src/cli/tables.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ impl TableData for WithdrawableSwapDisplay {
136136
}
137137
}
138138

139+
pub struct UtxoDisplay {
140+
pub outpoint: String,
141+
pub asset: String,
142+
pub value: String,
143+
}
144+
145+
impl TableData for UtxoDisplay {
146+
fn get_header() -> Vec<String> {
147+
vec!["Outpoint", "Asset", "Value"]
148+
.into_iter()
149+
.map(String::from)
150+
.collect()
151+
}
152+
fn to_row(&self) -> Vec<String> {
153+
vec![self.outpoint.clone(), self.asset.clone(), self.value.clone()]
154+
}
155+
}
156+
139157
fn render_table<T: TableData>(items: &[T], empty_msg: &str) {
140158
if items.is_empty() {
141159
println!(" ({empty_msg})");
@@ -188,3 +206,7 @@ pub fn display_cancellable_swaps_table(cancellable_swaps: &[CancellableSwapDispl
188206
pub fn display_withdrawable_swaps_table(withdrawable_swaps: &[WithdrawableSwapDisplay]) {
189207
render_table(withdrawable_swaps, "No withdrawable swaps found");
190208
}
209+
210+
pub fn display_utxo_table(utxos: &[UtxoDisplay]) {
211+
render_table(utxos, "No UTXOs found");
212+
}

crates/cli-client/src/cli/wallet.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::cli::tables::{UtxoDisplay, display_utxo_table};
12
use crate::cli::{Cli, WalletCommand};
23
use crate::config::Config;
34
use crate::error::Error;
@@ -63,24 +64,25 @@ impl Cli {
6364
let results = wallet.store().query_utxos(&[filter]).await?;
6465

6566
if let Some(coin_store::UtxoQueryResult::Found(entries, _)) = results.into_iter().next() {
66-
for entry in &entries {
67-
let (Some(asset), Some(value)) = (entry.asset(), entry.value()) else {
68-
println!(
69-
"{:<76} | {:<64} | {:<25}",
70-
entry.outpoint(),
71-
"Confidential",
72-
"Confidential"
73-
);
74-
75-
continue;
76-
};
77-
78-
println!("{:<76} | {:<64} | {:<25}", entry.outpoint(), asset, value);
79-
}
80-
67+
let displays: Vec<UtxoDisplay> = entries
68+
.iter()
69+
.map(|entry| {
70+
let (asset, value) = match (entry.asset(), entry.value()) {
71+
(Some(a), Some(v)) => (a.to_string(), v.to_string()),
72+
_ => ("Confidential".to_string(), "Confidential".to_string()),
73+
};
74+
UtxoDisplay {
75+
outpoint: entry.outpoint().to_string(),
76+
asset,
77+
value,
78+
}
79+
})
80+
.collect();
81+
82+
display_utxo_table(&displays);
8183
println!("Total: {} UTXOs", entries.len());
8284
} else {
83-
println!("No UTXOs found");
85+
display_utxo_table(&[]);
8486
}
8587
Ok(())
8688
}

0 commit comments

Comments
 (0)