1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use comfy_table::{
    modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL, Attribute, Cell, Color, ContentArrangement,
    Table,
};
use linera_base::identifiers::{ChainId, Owner};
pub use linera_client::wallet::*;

pub fn pretty_print(wallet: &Wallet, chain_ids: impl IntoIterator<Item = ChainId>) {
    let mut table = Table::new();
    table
        .load_preset(UTF8_FULL)
        .apply_modifier(UTF8_ROUND_CORNERS)
        .set_content_arrangement(ContentArrangement::Dynamic)
        .set_header(vec![
            Cell::new("Chain ID").add_attribute(Attribute::Bold),
            Cell::new("Latest Block").add_attribute(Attribute::Bold),
        ]);
    for chain_id in chain_ids {
        let Some(user_chain) = wallet.chains.get(&chain_id) else {
            panic!("Chain {} not found.", chain_id);
        };
        update_table_with_chain(
            &mut table,
            chain_id,
            user_chain,
            Some(chain_id) == wallet.default,
        );
    }
    println!("{}", table);
}

fn update_table_with_chain(
    table: &mut Table,
    chain_id: ChainId,
    user_chain: &UserChain,
    is_default_chain: bool,
) {
    let chain_id_cell = if is_default_chain {
        Cell::new(format!("{}", chain_id)).fg(Color::Green)
    } else {
        Cell::new(format!("{}", chain_id))
    };
    table.add_row(vec![
        chain_id_cell,
        Cell::new(format!(
            r#"Public Key:         {}
Owner:              {}
Block Hash:         {}
Timestamp:          {}
Next Block Height:  {}"#,
            user_chain
                .key_pair
                .as_ref()
                .map(|kp| kp.public().to_string())
                .unwrap_or_else(|| "-".to_string()),
            user_chain
                .key_pair
                .as_ref()
                .map(|kp| Owner::from(kp.public()))
                .map(|o| o.to_string())
                .unwrap_or_else(|| "-".to_string()),
            user_chain
                .block_hash
                .map(|bh| bh.to_string())
                .unwrap_or_else(|| "-".to_string()),
            user_chain.timestamp,
            user_chain.next_block_height
        )),
    ]);
}