|
1 | | -use crate::cli::interactive::{TokenDisplay, SwapDisplay}; |
| 1 | +use crate::cli::interactive::{SwapDisplay, TokenDisplay}; |
2 | 2 | use crate::cli::positions::{CollateralDisplay, UserTokenDisplay}; |
3 | 3 | use comfy_table::presets::UTF8_FULL; |
4 | 4 | use comfy_table::{Attribute, Cell, Table}; |
5 | 5 |
|
| 6 | +trait TableData { |
| 7 | + fn get_header() -> Vec<String>; |
| 8 | + fn to_row(&self) -> Vec<String>; |
| 9 | +} |
6 | 10 |
|
7 | | -pub fn display_token_table(tokens: &[TokenDisplay]) { |
8 | | - if tokens.is_empty() { |
9 | | - println!(" (No tokens found)"); |
10 | | - return; |
| 11 | +impl TableData for TokenDisplay { |
| 12 | + fn get_header() -> Vec<String> { |
| 13 | + vec!["#", "Collateral/Token", "Strike/Token", "Expires", "Contract"] |
| 14 | + .into_iter() |
| 15 | + .map(String::from) |
| 16 | + .collect() |
11 | 17 | } |
12 | | - |
13 | | - let mut table = Table::new(); |
14 | | - |
15 | | - table.load_preset(UTF8_FULL); |
16 | | - |
17 | | - table.set_header(vec![ |
18 | | - Cell::new("#").add_attribute(Attribute::Bold), |
19 | | - Cell::new("Collateral/Token").add_attribute(Attribute::Bold), |
20 | | - Cell::new("Strike/Token").add_attribute(Attribute::Bold), |
21 | | - Cell::new("Expires").add_attribute(Attribute::Bold), |
22 | | - Cell::new("Contract").add_attribute(Attribute::Bold), |
23 | | - ]); |
24 | | - |
25 | | - for token in tokens { |
26 | | - table.add_row(vec![ |
27 | | - token.index.to_string(), |
28 | | - token.collateral.clone(), |
29 | | - token.settlement.clone(), |
30 | | - token.expires.clone(), |
31 | | - token.status.clone(), |
32 | | - ]); |
| 18 | + fn to_row(&self) -> Vec<String> { |
| 19 | + vec![ |
| 20 | + self.index.to_string(), |
| 21 | + self.collateral.clone(), |
| 22 | + self.settlement.clone(), |
| 23 | + self.expires.clone(), |
| 24 | + self.status.clone(), |
| 25 | + ] |
33 | 26 | } |
| 27 | +} |
34 | 28 |
|
35 | | - let table_string = table.to_string(); |
36 | | - for line in table_string.lines() { |
37 | | - println!(" {}", line); |
| 29 | +impl TableData for SwapDisplay { |
| 30 | + fn get_header() -> Vec<String> { |
| 31 | + vec!["#", "Price", "Wants", "Expires", "Seller"] |
| 32 | + .into_iter() |
| 33 | + .map(String::from) |
| 34 | + .collect() |
| 35 | + } |
| 36 | + fn to_row(&self) -> Vec<String> { |
| 37 | + vec![ |
| 38 | + self.index.to_string(), |
| 39 | + self.offering.clone(), |
| 40 | + self.wants.clone(), |
| 41 | + self.expires.clone(), |
| 42 | + self.seller.clone(), |
| 43 | + ] |
38 | 44 | } |
39 | 45 | } |
40 | 46 |
|
41 | | -pub fn display_swap_table(swaps: &[SwapDisplay]) { |
42 | | - if swaps.is_empty() { |
43 | | - println!(" (No swaps found)"); |
44 | | - return; |
| 47 | +impl TableData for CollateralDisplay { |
| 48 | + fn get_header() -> Vec<String> { |
| 49 | + vec!["#", "Locked Assets", "Settlement", "Expires", "Contract"] |
| 50 | + .into_iter() |
| 51 | + .map(String::from) |
| 52 | + .collect() |
45 | 53 | } |
46 | | - |
47 | | - let mut table = Table::new(); |
48 | | - |
49 | | - table.load_preset(UTF8_FULL); |
50 | | - |
51 | | - table.set_header(vec![ |
52 | | - Cell::new("#").add_attribute(Attribute::Bold), |
53 | | - Cell::new("Price").add_attribute(Attribute::Bold), |
54 | | - Cell::new("Wants").add_attribute(Attribute::Bold), |
55 | | - Cell::new("Expires").add_attribute(Attribute::Bold), |
56 | | - Cell::new("Seller").add_attribute(Attribute::Bold), |
57 | | - ]); |
58 | | - |
59 | | - for swap in swaps { |
60 | | - table.add_row(vec![ |
61 | | - swap.index.to_string(), |
62 | | - swap.offering.clone(), |
63 | | - swap.wants.clone(), |
64 | | - swap.expires.clone(), |
65 | | - swap.seller.clone(), |
66 | | - ]); |
| 54 | + fn to_row(&self) -> Vec<String> { |
| 55 | + vec![ |
| 56 | + self.index.to_string(), |
| 57 | + self.collateral.clone(), |
| 58 | + self.settlement.clone(), |
| 59 | + self.expires.clone(), |
| 60 | + self.contract.clone(), |
| 61 | + ] |
67 | 62 | } |
| 63 | +} |
68 | 64 |
|
69 | | - let table_string = table.to_string(); |
70 | | - for line in table_string.lines() { |
71 | | - println!(" {}", line); |
| 65 | +impl TableData for UserTokenDisplay { |
| 66 | + fn get_header() -> Vec<String> { |
| 67 | + vec!["#", "Type", "Amount", "Strike/Token", "Expires", "Contract"] |
| 68 | + .into_iter() |
| 69 | + .map(String::from) |
| 70 | + .collect() |
| 71 | + } |
| 72 | + fn to_row(&self) -> Vec<String> { |
| 73 | + vec![ |
| 74 | + self.index.to_string(), |
| 75 | + self.token_type.clone(), |
| 76 | + self.amount.clone(), |
| 77 | + self.strike.clone(), |
| 78 | + self.expires.clone(), |
| 79 | + self.contract.clone(), |
| 80 | + ] |
72 | 81 | } |
73 | 82 | } |
74 | 83 |
|
75 | | - |
76 | | -pub fn display_collateral_table(displays: &[CollateralDisplay]) { |
77 | | - if displays.is_empty() { |
78 | | - println!(" (No locked assets found)"); |
| 84 | +fn render_table<T: TableData>(items: &[T], empty_msg: &str) { |
| 85 | + if items.is_empty() { |
| 86 | + println!(" ({empty_msg})"); |
79 | 87 | return; |
80 | 88 | } |
81 | 89 |
|
82 | 90 | let mut table = Table::new(); |
83 | 91 |
|
84 | 92 | table.load_preset(UTF8_FULL); |
85 | 93 |
|
86 | | - table.set_header(vec![ |
87 | | - Cell::new("#").add_attribute(Attribute::Bold), |
88 | | - Cell::new("Locked Assets").add_attribute(Attribute::Bold), |
89 | | - Cell::new("Settlement").add_attribute(Attribute::Bold), |
90 | | - Cell::new("Expires").add_attribute(Attribute::Bold), |
91 | | - Cell::new("Contract").add_attribute(Attribute::Bold), |
92 | | - ]); |
| 94 | + let header_cells: Vec<Cell> = T::get_header() |
| 95 | + .into_iter() |
| 96 | + .map(|h| Cell::new(h).add_attribute(Attribute::Bold)) |
| 97 | + .collect(); |
| 98 | + table.set_header(header_cells); |
93 | 99 |
|
94 | | - for display in displays { |
95 | | - table.add_row(vec![ |
96 | | - display.index.to_string(), |
97 | | - display.collateral.clone(), |
98 | | - display.settlement.clone(), |
99 | | - display.expires.clone(), |
100 | | - display.contract.clone(), |
101 | | - ]); |
| 100 | + for item in items { |
| 101 | + table.add_row(item.to_row()); |
102 | 102 | } |
103 | 103 |
|
104 | | - let table_string = table.to_string(); |
105 | | - for line in table_string.lines() { |
106 | | - println!(" {}", line); |
| 104 | + for line in table.to_string().lines() { |
| 105 | + println!(" {line}"); |
107 | 106 | } |
108 | 107 | } |
109 | 108 |
|
110 | | -pub fn display_user_token_table(displays: &[UserTokenDisplay]) { |
111 | | - if displays.is_empty() { |
112 | | - println!(" (No option/grantor tokens found)"); |
113 | | - return; |
114 | | - } |
115 | | - |
116 | | - let mut table = Table::new(); |
117 | | - |
118 | | - table.load_preset(UTF8_FULL); |
| 109 | +pub fn display_token_table(tokens: &[TokenDisplay]) { |
| 110 | + render_table(tokens, "No tokens found"); |
| 111 | +} |
119 | 112 |
|
120 | | - table.set_header(vec![ |
121 | | - Cell::new("#").add_attribute(Attribute::Bold), |
122 | | - Cell::new("Type").add_attribute(Attribute::Bold), |
123 | | - Cell::new("Amount").add_attribute(Attribute::Bold), |
124 | | - Cell::new("Strike/Token").add_attribute(Attribute::Bold), |
125 | | - Cell::new("Expires").add_attribute(Attribute::Bold), |
126 | | - Cell::new("Contract").add_attribute(Attribute::Bold), |
127 | | - ]); |
| 113 | +pub fn display_swap_table(swaps: &[SwapDisplay]) { |
| 114 | + render_table(swaps, "No swaps found"); |
| 115 | +} |
128 | 116 |
|
129 | | - for display in displays { |
130 | | - table.add_row(vec![ |
131 | | - display.index.to_string(), |
132 | | - display.token_type.clone(), |
133 | | - display.amount.to_string(), |
134 | | - display.strike.clone(), |
135 | | - display.expires.clone(), |
136 | | - display.contract.clone(), |
137 | | - ]); |
138 | | - } |
| 117 | +pub fn display_collateral_table(displays: &[CollateralDisplay]) { |
| 118 | + render_table(displays, "No locked assets found"); |
| 119 | +} |
139 | 120 |
|
140 | | - let table_string = table.to_string(); |
141 | | - for line in table_string.lines() { |
142 | | - println!(" {}", line); |
143 | | - } |
| 121 | +pub fn display_user_token_table(displays: &[UserTokenDisplay]) { |
| 122 | + render_table(displays, "No option/grantor tokens found"); |
144 | 123 | } |
0 commit comments