Skip to content

Commit fb56fd9

Browse files
authored
fix(cli): Add table formatting and fix wrapping issues
* Add table formatting and fix wrapping issues * switch to tabled crate for table rendering * remove unused parameters in table function calls
1 parent 29aca6d commit fb56fd9

File tree

11 files changed

+281
-82
lines changed

11 files changed

+281
-82
lines changed

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: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,15 @@ 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"]);
76-
for ct in &body.connection_types {
77-
table.add_row([&ct.name, &ct.label]);
74+
if body.connection_types.is_empty() {
75+
use crossterm::style::Stylize;
76+
eprintln!("{}", "No connection types found.".dark_grey());
77+
} else {
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);
7882
}
79-
println!("{table}");
8083
}
8184
_ => unreachable!(),
8285
}
@@ -312,12 +315,15 @@ pub fn list(workspace_id: &str, format: &str) {
312315
print!("{}", serde_yaml::to_string(&body.connections).unwrap());
313316
}
314317
"table" => {
315-
let mut table = crate::util::make_table();
316-
table.set_header(["ID", "NAME", "SOURCE TYPE"]);
317-
for c in &body.connections {
318-
table.add_row([&c.id, &c.name, &c.source_type]);
318+
if body.connections.is_empty() {
319+
use crossterm::style::Stylize;
320+
eprintln!("{}", "No connections found.".dark_grey());
321+
} else {
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);
319326
}
320-
println!("{table}");
321327
}
322328
_ => unreachable!(),
323329
}

src/datasets.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -475,17 +475,18 @@ 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"]);
480-
table.column_mut(1).unwrap().set_constraint(
481-
comfy_table::ColumnConstraint::UpperBoundary(comfy_table::Width::Fixed(30))
482-
);
483-
for d in &body.datasets {
484-
let created_at = d.created_at.split('.').next().unwrap_or(&d.created_at).replace('T', " ");
485-
let full_name = format!("datasets.main.{}", d.table_name);
486-
table.add_row([&d.id, &d.label, &full_name, &created_at]);
478+
if body.datasets.is_empty() {
479+
use crossterm::style::Stylize;
480+
eprintln!("{}", "No datasets found.".dark_grey());
481+
} else {
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);
487489
}
488-
println!("{table}");
489490
if body.has_more {
490491
let next = offset.unwrap_or(0) + body.count as u32;
491492
use crossterm::style::Stylize;
@@ -547,8 +548,8 @@ pub fn get(dataset_id: &str, workspace_id: &str, format: &str) {
547548
"json" => println!("{}", serde_json::to_string_pretty(&d).unwrap()),
548549
"yaml" => print!("{}", serde_yaml::to_string(&d).unwrap()),
549550
"table" => {
550-
let created_at = d.created_at.split('.').next().unwrap_or(&d.created_at).replace('T', " ");
551-
let updated_at = d.updated_at.split('.').next().unwrap_or(&d.updated_at).replace('T', " ");
551+
let created_at = crate::util::format_date(&d.created_at);
552+
let updated_at = crate::util::format_date(&d.updated_at);
552553
println!("id: {}", d.id);
553554
println!("label: {}", d.label);
554555
println!("full_name: datasets.main.{}", d.table_name);
@@ -557,12 +558,10 @@ pub fn get(dataset_id: &str, workspace_id: &str, format: &str) {
557558
println!("updated_at: {updated_at}");
558559
if !d.columns.is_empty() {
559560
println!();
560-
let mut table = crate::util::make_table();
561-
table.set_header(["COLUMN", "DATA TYPE", "NULLABLE"]);
562-
for col in &d.columns {
563-
table.add_row([&col.name, &col.data_type, &col.nullable.to_string()]);
564-
}
565-
println!("{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);
566565
}
567566
}
568567
_ => 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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +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);
116-
for row in &result.rows {
117-
let cells: Vec<String> = row.iter().map(value_to_string).collect();
118-
table.add_row(cells);
119-
}
120-
println!("{table}");
114+
crate::table::print_json(&result.columns, &result.rows);
121115
use crossterm::style::Stylize;
122116
let id_part = result.result_id.as_deref().map(|id| format!(" [result-id: {id}]")).unwrap_or_default();
123117
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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +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);
109-
for row in &result.rows {
110-
let cells: Vec<String> = row.iter().map(value_to_string).collect();
111-
table.add_row(cells);
112-
}
113-
println!("{table}");
107+
crate::table::print_json(&result.columns, &result.rows);
114108
use crossterm::style::Stylize;
115109
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());
116110
}

0 commit comments

Comments
 (0)