From 3d28ae0ced245411bfee03d2a9ae1c83b2dfaf2b Mon Sep 17 00:00:00 2001 From: sundyli <543950155@qq.com> Date: Wed, 12 Jul 2023 17:34:17 -0700 Subject: [PATCH] feat(cli): better new line mode (#157) --- Cargo.toml | 6 ++--- bindings/nodejs/npm/darwin-arm64/package.json | 2 +- bindings/nodejs/npm/darwin-x64/package.json | 2 +- .../nodejs/npm/linux-x64-gnu/package.json | 2 +- .../nodejs/npm/win32-x64-msvc/package.json | 2 +- bindings/nodejs/package.json | 2 +- cli/src/display.rs | 22 ++++++++++++++++-- cli/src/session.rs | 23 +++++++++++++++++-- 8 files changed, 49 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a6b642adc..84ec52941 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ members = [ ] [workspace.package] -version = "0.4.3" +version = "0.4.4" edition = "2021" license = "Apache-2.0" authors = ["Databend Authors "] @@ -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" } diff --git a/bindings/nodejs/npm/darwin-arm64/package.json b/bindings/nodejs/npm/darwin-arm64/package.json index 313b19171..457148559 100644 --- a/bindings/nodejs/npm/darwin-arm64/package.json +++ b/bindings/nodejs/npm/darwin-arm64/package.json @@ -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" ], diff --git a/bindings/nodejs/npm/darwin-x64/package.json b/bindings/nodejs/npm/darwin-x64/package.json index 5fd524ac4..e0904e6a0 100644 --- a/bindings/nodejs/npm/darwin-x64/package.json +++ b/bindings/nodejs/npm/darwin-x64/package.json @@ -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" ], diff --git a/bindings/nodejs/npm/linux-x64-gnu/package.json b/bindings/nodejs/npm/linux-x64-gnu/package.json index 0327baba6..bd9c9a742 100644 --- a/bindings/nodejs/npm/linux-x64-gnu/package.json +++ b/bindings/nodejs/npm/linux-x64-gnu/package.json @@ -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" ], diff --git a/bindings/nodejs/npm/win32-x64-msvc/package.json b/bindings/nodejs/npm/win32-x64-msvc/package.json index b30a40ccc..8478f3dbd 100644 --- a/bindings/nodejs/npm/win32-x64-msvc/package.json +++ b/bindings/nodejs/npm/win32-x64-msvc/package.json @@ -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" ], diff --git a/bindings/nodejs/package.json b/bindings/nodejs/package.json index 07ee1107d..676cf8200 100644 --- a/bindings/nodejs/package.json +++ b/bindings/nodejs/package.json @@ -1,7 +1,7 @@ { "name": "databend-driver", "author": "Databend Authors ", - "version": "0.4.3", + "version": "0.4.4", "license": "Apache-2.0", "main": "index.js", "types": "index.d.ts", diff --git a/cli/src/display.rs b/cli/src/display.rs index ec76bd4d5..8f5a79151 100644 --- a/cli/src/display.rs +++ b/cli/src/display.rs @@ -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, @@ -55,6 +57,7 @@ impl<'a> FormatDisplay<'a> { pub fn new( settings: &'a Settings, query: &'a str, + replace_newline: bool, start: Instant, schema: SchemaRef, data: RowProgressIterator, @@ -62,6 +65,7 @@ impl<'a> FormatDisplay<'a> { Self { settings, query, + replace_newline, schema, data, rows: 0, @@ -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 @@ -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, @@ -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); @@ -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); } @@ -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); } diff --git a/cli/src/session.rs b/cli/src/session.rs index 62f69f2fd..1409cd203 100644 --- a/cli/src/session.rs +++ b/cli/src/session.rs @@ -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) } @@ -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, + } +}