Skip to content

Commit 7e35848

Browse files
committed
switch to tabled crate for table rendering
1 parent a7f4bf3 commit 7e35848

11 files changed

Lines changed: 249 additions & 160 deletions

File tree

Cargo.lock

Lines changed: 129 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ open = "5"
2626
rand = "0.8"
2727
sha2 = "0.10"
2828
tiny_http = "0.12"
29-
comfy-table = "7"
29+
tabled = { version = "0.20", features = ["ansi"] }
3030
inquire = "0.9.4"
3131
indicatif = "0.17"
3232
nix = { version = "0.29", features = ["fs"] }

src/connections.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,14 @@ pub fn types_list(workspace_id: &str, format: &str) {
7171
"json" => println!("{}", serde_json::to_string_pretty(&body.connection_types).unwrap()),
7272
"yaml" => print!("{}", serde_yaml::to_string(&body.connection_types).unwrap()),
7373
"table" => {
74-
let mut table = crate::util::make_table();
75-
table.set_header(["NAME", "LABEL"].map(crate::util::hcell));
76-
crate::util::no_wrap(&mut table);
7774
if body.connection_types.is_empty() {
7875
use crossterm::style::Stylize;
7976
eprintln!("{}", "No connection types found.".dark_grey());
8077
} else {
81-
for ct in &body.connection_types {
82-
table.add_row([&ct.name, &ct.label]);
83-
}
84-
crate::util::print_table(&table);
78+
let rows: Vec<Vec<String>> = body.connection_types.iter()
79+
.map(|ct| vec![ct.name.clone(), ct.label.clone()])
80+
.collect();
81+
crate::table::print(&["NAME", "LABEL"], &rows);
8582
}
8683
}
8784
_ => unreachable!(),
@@ -318,17 +315,14 @@ pub fn list(workspace_id: &str, format: &str) {
318315
print!("{}", serde_yaml::to_string(&body.connections).unwrap());
319316
}
320317
"table" => {
321-
let mut table = crate::util::make_table();
322-
table.set_header(["ID", "NAME", "SOURCE TYPE"].map(crate::util::hcell));
323-
crate::util::no_wrap(&mut table);
324318
if body.connections.is_empty() {
325319
use crossterm::style::Stylize;
326320
eprintln!("{}", "No connections found.".dark_grey());
327321
} else {
328-
for c in &body.connections {
329-
table.add_row([&c.id, &c.name, &c.source_type]);
330-
}
331-
crate::util::print_table(&table);
322+
let rows: Vec<Vec<String>> = body.connections.iter()
323+
.map(|c| vec![c.id.clone(), c.name.clone(), c.source_type.clone()])
324+
.collect();
325+
crate::table::print(&["ID", "NAME", "SOURCE TYPE"], &rows);
332326
}
333327
}
334328
_ => unreachable!(),

src/datasets.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -475,19 +475,17 @@ pub fn list(workspace_id: &str, limit: Option<u32>, offset: Option<u32>, format:
475475
"json" => println!("{}", serde_json::to_string_pretty(&body.datasets).unwrap()),
476476
"yaml" => print!("{}", serde_yaml::to_string(&body.datasets).unwrap()),
477477
"table" => {
478-
let mut table = crate::util::make_table();
479-
table.set_header(["ID", "LABEL", "FULL NAME", "CREATED AT"].map(crate::util::hcell));
480-
crate::util::no_wrap(&mut table);
481478
if body.datasets.is_empty() {
482479
use crossterm::style::Stylize;
483480
eprintln!("{}", "No datasets found.".dark_grey());
484481
} else {
485-
for d in &body.datasets {
486-
let created_at = crate::util::format_date(&d.created_at);
487-
let full_name = format!("datasets.main.{}", d.table_name);
488-
table.add_row([&d.id, &d.label, &full_name, &created_at]);
489-
}
490-
crate::util::print_table(&table);
482+
let rows: Vec<Vec<String>> = body.datasets.iter().map(|d| vec![
483+
d.id.clone(),
484+
d.label.clone(),
485+
format!("datasets.main.{}", d.table_name),
486+
crate::util::format_date(&d.created_at),
487+
]).collect();
488+
crate::table::print(&["ID", "LABEL", "FULL NAME", "CREATED AT"], &rows);
491489
}
492490
if body.has_more {
493491
let next = offset.unwrap_or(0) + body.count as u32;
@@ -560,13 +558,10 @@ pub fn get(dataset_id: &str, workspace_id: &str, format: &str) {
560558
println!("updated_at: {updated_at}");
561559
if !d.columns.is_empty() {
562560
println!();
563-
let mut table = crate::util::make_table();
564-
table.set_header(["COLUMN", "DATA TYPE", "NULLABLE"].map(crate::util::hcell));
565-
crate::util::no_wrap(&mut table);
566-
for col in &d.columns {
567-
table.add_row([&col.name, &col.data_type, &col.nullable.to_string()]);
568-
}
569-
crate::util::print_table(&table);
561+
let rows: Vec<Vec<String>> = d.columns.iter().map(|col| vec![
562+
col.name.clone(), col.data_type.clone(), col.nullable.to_string(),
563+
]).collect();
564+
crate::table::print(&["COLUMN", "DATA TYPE", "NULLABLE"], &rows);
570565
}
571566
}
572567
_ => unreachable!(),

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod init;
88
mod query;
99
mod results;
1010
mod skill;
11+
mod table;
1112
mod tables;
1213
mod util;
1314
mod workspace;

src/query.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,7 @@ pub fn execute(sql: &str, workspace_id: &str, connection: Option<&str>, format:
111111
}
112112
}
113113
"table" => {
114-
let mut table = crate::util::make_table();
115-
table.set_header(result.columns.iter().map(|c| crate::util::hcell(c)));
116-
crate::util::no_wrap(&mut table);
117-
for row in &result.rows {
118-
let cells: Vec<comfy_table::Cell> = row.iter().map(crate::util::json_cell).collect();
119-
table.add_row(cells);
120-
}
121-
crate::util::print_table(&table);
114+
crate::table::print_json(&result.columns, &result.rows);
122115
use crossterm::style::Stylize;
123116
let id_part = result.result_id.as_deref().map(|id| format!(" [result-id: {id}]")).unwrap_or_default();
124117
eprintln!("{}", format!("\n{} row{} ({} ms){}", result.row_count, if result.row_count == 1 { "" } else { "s" }, result.execution_time_ms, id_part).dark_grey());

src/results.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,7 @@ pub fn get(result_id: &str, workspace_id: &str, format: &str) {
104104
}
105105
}
106106
"table" => {
107-
let mut table = crate::util::make_table();
108-
table.set_header(result.columns.iter().map(|c| crate::util::hcell(c)));
109-
crate::util::no_wrap(&mut table);
110-
for row in &result.rows {
111-
let cells: Vec<comfy_table::Cell> = row.iter().map(crate::util::json_cell).collect();
112-
table.add_row(cells);
113-
}
114-
crate::util::print_table(&table);
107+
crate::table::print_json(&result.columns, &result.rows);
115108
use crossterm::style::Stylize;
116109
eprintln!("{}", format!("\n{} row{} ({} ms) [result-id: {}]", result.row_count, if result.row_count == 1 { "" } else { "s" }, result.execution_time_ms, result.result_id).dark_grey());
117110
}

0 commit comments

Comments
 (0)