1use comfy_table::{
5 modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL, Attribute, Cell, Color, ContentArrangement,
6 Table,
7};
8use linera_base::{crypto::Signer, identifiers::ChainId};
9pub use linera_client::wallet::*;
10
11pub async fn pretty_print(
12 wallet: &Wallet,
13 signer: &impl Signer,
14 chain_ids: impl IntoIterator<Item = ChainId>,
15) {
16 let mut table = Table::new();
17 table
18 .load_preset(UTF8_FULL)
19 .apply_modifier(UTF8_ROUND_CORNERS)
20 .set_content_arrangement(ContentArrangement::Dynamic)
21 .set_header(vec![
22 Cell::new("Chain ID").add_attribute(Attribute::Bold),
23 Cell::new("Latest Block").add_attribute(Attribute::Bold),
24 ]);
25 for chain_id in chain_ids {
26 let Some(user_chain) = wallet.chains.get(&chain_id) else {
27 panic!("Chain {} not found.", chain_id);
28 };
29 update_table_with_chain(
30 &mut table,
31 chain_id,
32 user_chain,
33 Some(chain_id) == wallet.default,
34 signer,
35 )
36 .await;
37 }
38 println!("{}", table);
39}
40
41async fn update_table_with_chain(
42 table: &mut Table,
43 chain_id: ChainId,
44 user_chain: &UserChain,
45 is_default_chain: bool,
46 signer: &impl Signer,
47) {
48 let chain_id_cell = if is_default_chain {
49 Cell::new(format!("{}", chain_id)).fg(Color::Green)
50 } else {
51 Cell::new(format!("{}", chain_id))
52 };
53 let account_owner = user_chain.owner;
54 let account_pub_key = match account_owner {
55 Some(owner) => signer
56 .get_public_key(&owner)
57 .await
58 .map(Some)
59 .expect("We should get a public key for owned chain."),
60 None => None,
61 };
62 table.add_row(vec![
63 chain_id_cell,
64 Cell::new(format!(
65 r#"Public Key: {}
66AccountOwner: {}
67Block Hash: {}
68Timestamp: {}
69Next Block Height: {}"#,
70 account_pub_key
71 .map(|kp| kp.to_string())
72 .unwrap_or_else(|| "-".to_string()),
73 account_owner
74 .as_ref()
75 .map(|o| o.to_string())
76 .unwrap_or_else(|| "-".to_string()),
77 user_chain
78 .block_hash
79 .map(|bh| bh.to_string())
80 .unwrap_or_else(|| "-".to_string()),
81 user_chain.timestamp,
82 user_chain.next_block_height
83 )),
84 ]);
85}