Skip to content

Commit d322a9c

Browse files
zahorodnyiKyrylR
authored andcommitted
Fix code duplication in table display functions
1 parent 41d5d79 commit d322a9c

File tree

5 files changed

+96
-118
lines changed

5 files changed

+96
-118
lines changed

crates/cli-client/src/cli/browse.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use crate::cli::Cli;
22
use crate::cli::interactive::{
3-
SwapDisplay, TokenDisplay, format_relative_time, format_settlement_asset,
4-
truncate_with_ellipsis,
3+
SwapDisplay, TokenDisplay, format_relative_time, format_settlement_asset, truncate_with_ellipsis,
54
};
5+
use crate::cli::tables::{display_swap_table, display_token_table};
66
use crate::config::Config;
77
use crate::error::Error;
8-
use crate::cli::tables::{display_swap_table, display_token_table};
98

109
use options_relay::{OptionCreatedEvent, SwapCreatedEvent};
1110
use simplicityhl::elements::AssetId;

crates/cli-client/src/cli/interactive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::error::Error;
21
use crate::cli::tables::display_token_table;
2+
use crate::error::Error;
33

44
use std::io::{self, Write};
55
use std::time::{SystemTime, UNIX_EPOCH};

crates/cli-client/src/cli/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ mod option;
55
mod positions;
66
mod swap;
77
mod sync;
8+
mod tables;
89
mod tx;
910
mod wallet;
10-
mod tables;
1111

1212
use crate::error::Error;
1313

crates/cli-client/src/cli/positions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use crate::cli::Cli;
22
use crate::cli::interactive::{
3-
EnrichedTokenEntry, GRANTOR_TOKEN_TAG, OPTION_TOKEN_TAG, TokenDisplay,
4-
format_asset_value_with_tag, format_asset_with_tag, format_relative_time, format_settlement_asset, format_time_ago,
3+
EnrichedTokenEntry, GRANTOR_TOKEN_TAG, OPTION_TOKEN_TAG, TokenDisplay, format_asset_value_with_tag,
4+
format_asset_with_tag, format_relative_time, format_settlement_asset, format_time_ago,
55
get_grantor_tokens_from_wallet, get_option_tokens_from_wallet, truncate_with_ellipsis,
66
};
7+
use crate::cli::tables::{display_collateral_table, display_token_table, display_user_token_table};
78
use crate::config::Config;
89
use crate::error::Error;
910
use crate::metadata::ContractMetadata;
10-
use crate::cli::tables::{display_token_table, display_user_token_table, display_collateral_table};
1111

1212
use coin_store::{Store, UtxoEntry, UtxoFilter, UtxoQueryResult, UtxoStore};
1313
use contracts::options::{OPTION_SOURCE, OptionsArguments, get_options_address};
Lines changed: 89 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,144 +1,123 @@
1-
use crate::cli::interactive::{TokenDisplay, SwapDisplay};
1+
use crate::cli::interactive::{SwapDisplay, TokenDisplay};
22
use crate::cli::positions::{CollateralDisplay, UserTokenDisplay};
33
use comfy_table::presets::UTF8_FULL;
44
use comfy_table::{Attribute, Cell, Table};
55

6+
trait TableData {
7+
fn get_header() -> Vec<String>;
8+
fn to_row(&self) -> Vec<String>;
9+
}
610

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()
1117
}
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+
]
3326
}
27+
}
3428

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+
]
3844
}
3945
}
4046

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()
4553
}
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+
]
6762
}
63+
}
6864

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+
]
7281
}
7382
}
7483

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})");
7987
return;
8088
}
8189

8290
let mut table = Table::new();
8391

8492
table.load_preset(UTF8_FULL);
8593

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);
9399

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());
102102
}
103103

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}");
107106
}
108107
}
109108

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+
}
119112

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+
}
128116

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+
}
139120

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");
144123
}

0 commit comments

Comments
 (0)