|
4 | 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
5 | 5 | */ |
6 | 6 |
|
| 7 | +use std::collections::HashSet; |
| 8 | + |
7 | 9 | use clap::builder::styling::{AnsiColor, Color, Style}; |
| 10 | +use itertools::Itertools; |
8 | 11 | use typedb_driver::{ |
9 | 12 | answer::{ConceptDocument, ConceptRow}, |
10 | 13 | concept::{Concept, Value}, |
11 | | - IID, |
| 14 | + Replica, ReplicaRole, ServerReplica, IID, |
12 | 15 | }; |
13 | 16 |
|
14 | 17 | const TABLE_INDENT: &'static str = " "; |
15 | 18 | const CONTENT_INDENT: &'static str = " "; |
16 | 19 | const TABLE_DASHES: usize = 7; |
17 | 20 |
|
18 | | -pub const ERROR_STYLE: Style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Red))).bold(); |
19 | | -pub const WARNING_STYLE: Style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Yellow))).bold(); |
20 | | -pub const ARGUMENT_STYLE: Style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Yellow))); |
| 21 | +pub const STYLE_RED: Style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Red))); |
| 22 | +pub const STYLE_GREEN: Style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Green))); |
| 23 | +pub const STYLE_ERROR: Style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Red))).bold(); |
| 24 | +pub const STYLE_WARNING: Style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Yellow))).bold(); |
| 25 | +pub const STYLE_ARGUMENT: Style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Yellow))); |
21 | 26 |
|
22 | 27 | #[macro_export] |
23 | 28 | macro_rules! format_error { |
24 | 29 | ($($arg:tt)*) => { |
25 | | - $crate::format_colored!($crate::printer::ERROR_STYLE, $($arg)*) |
| 30 | + $crate::format_colored!($crate::printer::STYLE_ERROR, $($arg)*) |
26 | 31 | }; |
27 | 32 | } |
28 | 33 |
|
29 | 34 | #[macro_export] |
30 | 35 | macro_rules! format_warning { |
31 | 36 | ($($arg:tt)*) => { |
32 | | - $crate::format_colored!($crate::printer::WARNING_STYLE, $($arg)*) |
| 37 | + $crate::format_colored!($crate::printer::STYLE_WARNING, $($arg)*) |
33 | 38 | }; |
34 | 39 | } |
35 | 40 |
|
36 | 41 | #[macro_export] |
37 | 42 | macro_rules! format_argument { |
38 | 43 | ($($arg:tt)*) => { |
39 | | - $crate::format_colored!($crate::printer::ARGUMENT_STYLE, $($arg)*) |
| 44 | + $crate::format_colored!($crate::printer::STYLE_ARGUMENT, $($arg)*) |
40 | 45 | }; |
41 | 46 | } |
42 | 47 |
|
@@ -67,6 +72,103 @@ fn println(string: &str) { |
67 | 72 | println!("{}", string) |
68 | 73 | } |
69 | 74 |
|
| 75 | +pub(crate) fn print_replicas_table(replicas: HashSet<ServerReplica>) { |
| 76 | + const COLUMN_NUM: usize = 5; |
| 77 | + #[derive(Debug)] |
| 78 | + struct Row { |
| 79 | + id: String, |
| 80 | + address: String, |
| 81 | + role: String, |
| 82 | + term: String, |
| 83 | + status: (String, Style), |
| 84 | + } |
| 85 | + |
| 86 | + let mut rows = Vec::new(); |
| 87 | + rows.push(Row { |
| 88 | + id: "id".to_string(), |
| 89 | + address: "address".to_string(), |
| 90 | + role: "role".to_string(), |
| 91 | + term: "term".to_string(), |
| 92 | + status: ("status".to_string(), Style::new()), |
| 93 | + }); |
| 94 | + |
| 95 | + for replica in replicas.into_iter().sorted_by_key(|replica| replica.id()) { |
| 96 | + let role = match replica.role() { |
| 97 | + Some(ReplicaRole::Primary) => "primary", |
| 98 | + Some(ReplicaRole::Candidate) => "candidate", |
| 99 | + Some(ReplicaRole::Secondary) => "secondary", |
| 100 | + None => "", |
| 101 | + } |
| 102 | + .to_string(); |
| 103 | + |
| 104 | + let term = replica.term().map(|t| t.to_string()).unwrap_or_default(); |
| 105 | + |
| 106 | + let status = match &replica { |
| 107 | + ServerReplica::Available(_) => ("available".to_string(), STYLE_GREEN), |
| 108 | + ServerReplica::Unavailable { .. } => ("unavailable".to_string(), STYLE_RED), |
| 109 | + }; |
| 110 | + |
| 111 | + rows.push(Row { |
| 112 | + id: replica.id().to_string(), |
| 113 | + address: replica.address().map(|address| address.to_string()).unwrap_or_default(), |
| 114 | + role, |
| 115 | + term, |
| 116 | + status, |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + // Compute max content length per column (without padding) |
| 121 | + let mut width_id = 0usize; |
| 122 | + let mut width_address = 0usize; |
| 123 | + let mut width_role = 0usize; |
| 124 | + let mut width_term = 0usize; |
| 125 | + let mut width_status = 0usize; |
| 126 | + |
| 127 | + for r in &rows { |
| 128 | + width_id = width_id.max(r.id.len()); |
| 129 | + width_address = width_address.max(r.address.len()); |
| 130 | + width_role = width_role.max(r.role.len()); |
| 131 | + width_term = width_term.max(r.term.len()); |
| 132 | + width_status = width_status.max(r.status.0.len()); |
| 133 | + } |
| 134 | + |
| 135 | + // Add 2 spaces per column (one at the beginning and one at the end) |
| 136 | + width_id += 2; |
| 137 | + width_address += 2; |
| 138 | + width_role += 2; |
| 139 | + width_term += 2; |
| 140 | + width_status += 2; |
| 141 | + |
| 142 | + fn print_cell(content: &str, width: usize, style: Option<Style>) { |
| 143 | + // One space on the left, one at the right, rest is extra right padding |
| 144 | + let content_len = content.len(); |
| 145 | + let base_len = content_len + 2; // left + right space |
| 146 | + let extra_padding = width.saturating_sub(base_len); |
| 147 | + let styled_content = format_colored!(style.unwrap_or_default(), "{content}"); |
| 148 | + print!(" {}{} ", styled_content, " ".repeat(extra_padding)); |
| 149 | + } |
| 150 | + |
| 151 | + const PIPES_NUM: usize = COLUMN_NUM - 1; |
| 152 | + let total_width = width_id + width_address + width_role + width_term + width_status + PIPES_NUM; |
| 153 | + |
| 154 | + for (row_index, row) in rows.iter().enumerate() { |
| 155 | + print_cell(&row.id, width_id, None); |
| 156 | + print!("|"); |
| 157 | + print_cell(&row.address, width_address, None); |
| 158 | + print!("|"); |
| 159 | + print_cell(&row.role, width_role, None); |
| 160 | + print!("|"); |
| 161 | + print_cell(&row.term, width_term, None); |
| 162 | + print!("|"); |
| 163 | + print_cell(&row.status.0, width_status, Some(row.status.1)); |
| 164 | + println!(); |
| 165 | + |
| 166 | + if row_index == 0 { |
| 167 | + println!("{}", "-".repeat(total_width)); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
70 | 172 | pub(crate) fn print_document(document: ConceptDocument) { |
71 | 173 | // Note: inefficient, but easy... |
72 | 174 | match serde_json::from_str::<serde_json::Value>(&document.into_json().to_string()) { |
|
0 commit comments