From 3deac98d406576fa7ffc5ae032e06c6550796d5c Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Sat, 8 Feb 2025 11:25:36 +0200 Subject: [PATCH] cli: Make pretty mode pretty like DuckDB MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DuckDB is pretty, I want to be pretty! ``` limbo> CREATE TABLE t(x); INSERT INTO t VALUES (1), (2), (3); limbo> .mode pretty limbo> SELECT * FROM t; ┌───┐ │ x │ ├───┤ │ 1 │ ├───┤ │ 2 │ ├───┤ │ 3 │ └───┘ ``` --- cli/app.rs | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/cli/app.rs b/cli/app.rs index 729a8cf6e..7fa7b70f0 100644 --- a/cli/app.rs +++ b/cli/app.rs @@ -2,6 +2,7 @@ use crate::{ import::{ImportFile, IMPORT_HELP}, opcodes_dictionary::OPCODE_DESCRIPTIONS, }; +use cli_table::format::{Border, HorizontalLine, Separator, VerticalLine}; use cli_table::{Cell, Style, Table}; use limbo_core::{Database, LimboError, Statement, StepResult, Value}; @@ -717,11 +718,7 @@ impl Limbo { } } } - if let Ok(table) = table_rows.table().display() { - let _ = self.write_fmt(format_args!("{}", table)); - } else { - let _ = self.writeln("Error displaying table."); - } + self.print_table(table_rows); } }, Ok(None) => {} @@ -737,6 +734,40 @@ impl Limbo { Ok(()) } + fn print_table(&mut self, table_rows: Vec>) { + if table_rows.is_empty() { + return; + } + + let horizontal_line = HorizontalLine::new('┌', '┐', '┬', '─'); + let horizontal_line_mid = HorizontalLine::new('├', '┤', '┼', '─'); + let horizontal_line_bottom = HorizontalLine::new('└', '┘', '┴', '─'); + let vertical_line = VerticalLine::new('│'); + + let border = Border::builder() + .top(horizontal_line) + .bottom(horizontal_line_bottom) + .left(vertical_line.clone()) + .right(vertical_line.clone()) + .build(); + + let separator = Separator::builder() + .column(Some(vertical_line)) + .row(Some(horizontal_line_mid)) + .build(); + + if let Ok(table) = table_rows + .table() + .border(border) + .separator(separator) + .display() + { + let _ = self.write_fmt(format_args!("{}", table)); + } else { + let _ = self.writeln("Error displaying table."); + } + } + fn display_schema(&mut self, table: Option<&str>) -> anyhow::Result<()> { let sql = match table { Some(table_name) => format!(