Skip to content

Commit

Permalink
feat(cli): better new line mode (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
sundy-li authored Jul 13, 2023
1 parent 2b4f6f6 commit 3d28ae0
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ members = [
]

[workspace.package]
version = "0.4.3"
version = "0.4.4"
edition = "2021"
license = "Apache-2.0"
authors = ["Databend Authors <opensource@datafuselabs.com>"]
Expand All @@ -18,5 +18,5 @@ keywords = ["databend", "database"]
repository = "https://github.com/datafuselabs/bendsql"

[workspace.dependencies]
databend-client = { path = "core", version = "0.4.3" }
databend-driver = { path = "driver", version = "0.4.3" }
databend-client = { path = "core", version = "0.4.4" }
databend-driver = { path = "driver", version = "0.4.4" }
2 changes: 1 addition & 1 deletion bindings/nodejs/npm/darwin-arm64/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@databend-driver/lib-darwin-arm64",
"repository": "https://github.com/datafuselabs/bendsql.git",
"version": "0.4.3",
"version": "0.4.4",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/npm/darwin-x64/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@databend-driver/lib-darwin-x64",
"repository": "https://github.com/datafuselabs/bendsql.git",
"version": "0.4.3",
"version": "0.4.4",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/npm/linux-x64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@databend-driver/lib-linux-x64-gnu",
"repository": "https://github.com/datafuselabs/bendsql.git",
"version": "0.4.3",
"version": "0.4.4",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/npm/win32-x64-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@databend-driver/lib-win32-x64-msvc",
"repository": "https://github.com/datafuselabs/bendsql.git",
"version": "0.4.3",
"version": "0.4.4",
"os": [
"win32"
],
Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "databend-driver",
"author": "Databend Authors <opensource@datafuselabs.com>",
"version": "0.4.3",
"version": "0.4.4",
"license": "Apache-2.0",
"main": "index.js",
"types": "index.d.ts",
Expand Down
22 changes: 20 additions & 2 deletions cli/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub trait ChunkDisplay {
pub struct FormatDisplay<'a> {
settings: &'a Settings,
query: &'a str,
// whether replace '\n' with '\\n', only disable in explain/show create stmts
replace_newline: bool,
schema: SchemaRef,
data: RowProgressIterator,

Expand All @@ -55,13 +57,15 @@ impl<'a> FormatDisplay<'a> {
pub fn new(
settings: &'a Settings,
query: &'a str,
replace_newline: bool,
start: Instant,
schema: SchemaRef,
data: RowProgressIterator,
) -> Self {
Self {
settings,
query,
replace_newline,
schema,
data,
rows: 0,
Expand Down Expand Up @@ -114,6 +118,7 @@ impl<'a> FormatDisplay<'a> {
create_table(
self.schema.clone(),
&rows,
self.replace_newline,
self.settings.max_display_rows,
self.settings.max_width,
self.settings.max_col_width
Expand Down Expand Up @@ -380,6 +385,7 @@ fn compute_render_widths(
fn create_table(
schema: SchemaRef,
results: &[Row],
replace_newline: bool,
max_rows: usize,
mut max_width: usize,
max_col_width: usize,
Expand All @@ -400,6 +406,10 @@ fn create_table(
}
}

if !replace_newline {
max_width = usize::MAX;
}

let row_count: usize = results.len();
let mut rows_to_render = row_count.min(max_rows);

Expand All @@ -424,7 +434,11 @@ fn create_table(
let values = row.values();
let mut v = vec![];
for value in values {
v.push(value.to_string());
if replace_newline {
v.push(value.to_string().replace('\n', "\\n"));
} else {
v.push(value.to_string());
}
}
res_vec.push(v);
}
Expand All @@ -434,7 +448,11 @@ fn create_table(
let values = row.values();
let mut v = vec![];
for value in values {
v.push(value.to_string());
if replace_newline {
v.push(value.to_string().replace('\n', "\\n"));
} else {
v.push(value.to_string());
}
}
res_vec.push(v);
}
Expand Down
23 changes: 21 additions & 2 deletions cli/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,16 @@ impl Session {
Ok(false)
}
QueryKind::Query | QueryKind::Explain => {
let replace_newline = replace_newline_in_box_display(query);
let (schema, data) = self.conn.query_iter_ext(query).await?;
let mut displayer =
FormatDisplay::new(&self.settings, query, start, Arc::new(schema), data);
let mut displayer = FormatDisplay::new(
&self.settings,
query,
replace_newline,
start,
Arc::new(schema),
data,
);
displayer.display().await?;
Ok(false)
}
Expand Down Expand Up @@ -408,3 +415,15 @@ impl From<&str> for QueryKind {
}
}
}

fn replace_newline_in_box_display(query: &str) -> bool {
let mut tz = Tokenizer::new(query);
match tz.next() {
Some(Ok(t)) => match t.kind {
TokenKind::EXPLAIN => false,
TokenKind::SHOW => !matches!(tz.next(), Some(Ok(t)) if t.kind == TokenKind::CREATE),
_ => true,
},
_ => true,
}
}

0 comments on commit 3d28ae0

Please sign in to comment.