|
1 | 1 | // Copyright (c) Zefchain Labs, Inc. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | | -use comfy_table::{ |
5 | | - modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL, Attribute, Cell, Color, ContentArrangement, |
6 | | - Table, |
| 4 | +use linera_base::{ |
| 5 | + data_types::{ChainDescription, ChainOrigin}, |
| 6 | + identifiers::ChainId, |
7 | 7 | }; |
8 | | -use linera_base::identifiers::ChainId; |
9 | 8 | pub use linera_client::wallet::*; |
10 | 9 |
|
11 | 10 | pub fn pretty_print(wallet: &Wallet, chain_ids: impl IntoIterator<Item = ChainId>) { |
12 | | - let mut table = Table::new(); |
13 | | - table |
14 | | - .load_preset(UTF8_FULL) |
15 | | - .apply_modifier(UTF8_ROUND_CORNERS) |
16 | | - .set_content_arrangement(ContentArrangement::Dynamic) |
17 | | - .set_header(vec![ |
18 | | - Cell::new("Chain ID").add_attribute(Attribute::Bold), |
19 | | - Cell::new("Latest Block").add_attribute(Attribute::Bold), |
20 | | - ]); |
21 | | - for chain_id in chain_ids { |
| 11 | + let chain_ids: Vec<_> = chain_ids.into_iter().collect(); |
| 12 | + let total_chains = chain_ids.len(); |
| 13 | + |
| 14 | + if total_chains == 0 { |
| 15 | + println!("No chains in wallet."); |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + let plural_s = if total_chains == 1 { "" } else { "s" }; |
| 20 | + println!("\n\x1b[1mWALLET ({total_chains} chain{plural_s} in total)\x1b[0m",); |
| 21 | + |
| 22 | + let mut chains = chain_ids |
| 23 | + .into_iter() |
| 24 | + .map(|chain_id| ChainDetails::new(chain_id, wallet)) |
| 25 | + .collect::<Vec<_>>(); |
| 26 | + // Print first the default, then the admin chain, then other root chains, and finally the |
| 27 | + // child chains. |
| 28 | + chains.sort_unstable_by_key(|chain| { |
| 29 | + let root_id = chain |
| 30 | + .origin |
| 31 | + .and_then(|origin| origin.root()) |
| 32 | + .unwrap_or(u32::MAX); |
| 33 | + let chain_id = chain.user_chain.chain_id; |
| 34 | + (!chain.is_default, !chain.is_admin, root_id, chain_id) |
| 35 | + }); |
| 36 | + for chain in chains { |
| 37 | + println!(); |
| 38 | + chain.print_paragraph(); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +struct ChainDetails<'a> { |
| 43 | + is_default: bool, |
| 44 | + is_admin: bool, |
| 45 | + origin: Option<ChainOrigin>, |
| 46 | + user_chain: &'a UserChain, |
| 47 | +} |
| 48 | + |
| 49 | +impl<'a> ChainDetails<'a> { |
| 50 | + fn new(chain_id: ChainId, wallet: &'a Wallet) -> Self { |
22 | 51 | let Some(user_chain) = wallet.chains.get(&chain_id) else { |
23 | 52 | panic!("Chain {} not found.", chain_id); |
24 | 53 | }; |
25 | | - update_table_with_chain( |
26 | | - &mut table, |
27 | | - chain_id, |
| 54 | + ChainDetails { |
| 55 | + is_default: Some(chain_id) == wallet.default, |
| 56 | + is_admin: chain_id == wallet.genesis_admin_chain(), |
| 57 | + origin: wallet |
| 58 | + .genesis_config() |
| 59 | + .chains |
| 60 | + .iter() |
| 61 | + .find(|description| description.id() == chain_id) |
| 62 | + .map(ChainDescription::origin), |
28 | 63 | user_chain, |
29 | | - Some(chain_id) == wallet.default, |
30 | | - ); |
| 64 | + } |
31 | 65 | } |
32 | | - println!("{}", table); |
33 | | -} |
34 | 66 |
|
35 | | -fn update_table_with_chain( |
36 | | - table: &mut Table, |
37 | | - chain_id: ChainId, |
38 | | - user_chain: &UserChain, |
39 | | - is_default_chain: bool, |
40 | | -) { |
41 | | - let chain_id_cell = if is_default_chain { |
42 | | - Cell::new(format!("{}", chain_id)).fg(Color::Green) |
43 | | - } else { |
44 | | - Cell::new(format!("{}", chain_id)) |
45 | | - }; |
46 | | - let account_owner = user_chain.owner; |
47 | | - table.add_row(vec![ |
48 | | - chain_id_cell, |
49 | | - Cell::new(format!( |
50 | | - r#"AccountOwner: {} |
51 | | -Block Hash: {} |
52 | | -Timestamp: {} |
53 | | -Next Block Height: {}"#, |
54 | | - account_owner |
55 | | - .as_ref() |
56 | | - .map_or_else(|| "-".to_string(), |o| o.to_string()), |
57 | | - user_chain |
58 | | - .block_hash |
59 | | - .map_or_else(|| "-".to_string(), |bh| bh.to_string()), |
60 | | - user_chain.timestamp, |
61 | | - user_chain.next_block_height |
62 | | - )), |
63 | | - ]); |
| 67 | + fn print_paragraph(&self) { |
| 68 | + let title = if self.is_admin { |
| 69 | + "Admin Chain".to_string() |
| 70 | + } else { |
| 71 | + match self.origin { |
| 72 | + Some(ChainOrigin::Root(i)) => format!("Root Chain {i}"), |
| 73 | + _ => "Child Chain".to_string(), |
| 74 | + } |
| 75 | + }; |
| 76 | + let default_marker = if self.is_default { " [DEFAULT]" } else { "" }; |
| 77 | + |
| 78 | + // Print chain header in bold |
| 79 | + println!("\x1b[1m{}{}\x1b[0m", title, default_marker); |
| 80 | + println!(" Chain ID: {}", self.user_chain.chain_id); |
| 81 | + if let Some(owner) = &self.user_chain.owner { |
| 82 | + println!(" Owner: {owner}"); |
| 83 | + } else { |
| 84 | + println!(" Owner: No owner key"); |
| 85 | + } |
| 86 | + println!(" Timestamp: {}", self.user_chain.timestamp); |
| 87 | + println!(" Blocks: {}", self.user_chain.next_block_height); |
| 88 | + if let Some(hash) = self.user_chain.block_hash { |
| 89 | + println!(" Latest Block: {}", hash); |
| 90 | + } |
| 91 | + if self.user_chain.pending_proposal.is_some() { |
| 92 | + println!(" Status: ⚠ Pending proposal"); |
| 93 | + } |
| 94 | + } |
64 | 95 | } |
0 commit comments