1use crate::output::MultiFormatDisplay;
4use comfy_table::{presets::UTF8_FULL, Cell, Color, ContentArrangement, Table};
5use serde::Serialize;
6
7#[derive(Debug, Serialize)]
8pub struct PeerInfo {
9 pub id: String,
10 pub address: String,
11 pub latency_ms: f64,
12 pub state: String,
13 pub last_seen: String,
14}
15
16#[derive(Debug, Serialize)]
17pub struct PeerList {
18 pub peers: Vec<PeerInfo>,
19 pub total: usize,
20}
21
22impl MultiFormatDisplay for PeerList {
23 fn to_table(&self) -> Table {
24 let mut table = Table::new();
25 table
26 .load_preset(UTF8_FULL)
27 .set_content_arrangement(ContentArrangement::Dynamic)
28 .set_header(vec![
29 Cell::new("PEER ID").fg(Color::Cyan),
30 Cell::new("ADDRESS").fg(Color::Cyan),
31 Cell::new("LATENCY").fg(Color::Cyan),
32 Cell::new("STATE").fg(Color::Cyan),
33 Cell::new("LAST SEEN").fg(Color::Cyan),
34 ]);
35
36 for peer in &self.peers {
37 let state_color = match peer.state.as_str() {
38 "Connected" => Color::Green,
39 "Suspicious" => Color::Yellow,
40 "Failed" => Color::Red,
41 _ => Color::White,
42 };
43
44 table.add_row(vec![
45 Cell::new(&peer.id[..8]),
46 Cell::new(&peer.address),
47 Cell::new(format!("{:.1}ms", peer.latency_ms)),
48 Cell::new(&peer.state).fg(state_color),
49 Cell::new(&peer.last_seen),
50 ]);
51 }
52
53 table
54 }
55
56 fn to_quiet(&self) -> String {
57 self.peers
58 .iter()
59 .map(|p| p.id.clone())
60 .collect::<Vec<_>>()
61 .join("\n")
62 }
63}