Skip to content

Commit d767425

Browse files
b41shsundy-li
andauthored
feat: Query result display escaped newline (#187)
Co-authored-by: sundyli <543950155@qq.com>
1 parent acd937a commit d767425

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ prompt = ":) "
115115
| `output_format` | The output format to use. |
116116
| `time` | Whether to show the time elapsed when executing queries. |
117117
| `multi_line` | Whether to allow multi-line input. |
118+
| `replace_newline` | whether replace '\n' with '\\\n'. |
118119

119120

120121
## Control commands in REPL

bindings/nodejs/src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,7 @@ impl Row {
220220
#[napi]
221221
pub fn values(&self) -> Vec<Value> {
222222
// FIXME: do not clone
223-
self.0
224-
.values()
225-
.to_owned()
226-
.into_iter()
227-
.map(|v| Value(v))
228-
.collect()
223+
self.0.values().iter().map(|v| Value(v.clone())).collect()
229224
}
230225
}
231226

cli/src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub struct SettingsConfig {
3737
pub progress_color: Option<String>,
3838
pub show_progress: Option<bool>,
3939
pub show_stats: Option<bool>,
40+
pub replace_newline: Option<bool>,
4041
}
4142

4243
#[derive(Clone, Debug)]
@@ -67,6 +68,8 @@ pub struct Settings {
6768

6869
/// Multi line mode, default is true.
6970
pub multi_line: bool,
71+
/// whether replace '\n' with '\\n', default true.
72+
pub replace_newline: bool,
7073
}
7174

7275
#[derive(ValueEnum, Clone, Debug, PartialEq, Deserialize)]
@@ -94,6 +97,9 @@ impl Settings {
9497
if let Some(show_stats) = cfg.show_stats {
9598
self.show_stats = show_stats;
9699
}
100+
if let Some(replace_newline) = cfg.replace_newline {
101+
self.replace_newline = replace_newline;
102+
}
97103
}
98104

99105
pub fn inject_ctrl_cmd(&mut self, cmd_name: &str, cmd_value: &str) -> Result<()> {
@@ -117,6 +123,7 @@ impl Settings {
117123
"max_display_rows" => self.max_display_rows = cmd_value.parse()?,
118124
"max_width" => self.max_width = cmd_value.parse()?,
119125
"max_col_width" => self.max_col_width = cmd_value.parse()?,
126+
"replace_newline" => self.replace_newline = cmd_value.parse()?,
120127
_ => return Err(anyhow!("Unknown command: {}", cmd_name)),
121128
}
122129
Ok(())
@@ -169,6 +176,7 @@ impl Default for Settings {
169176
show_stats: false,
170177
time: false,
171178
multi_line: true,
179+
replace_newline: true,
172180
}
173181
}
174182
}

cli/src/display.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ pub struct FormatDisplay<'a> {
4444
settings: &'a Settings,
4545
query: &'a str,
4646
kind: QueryKind,
47-
// whether replace '\n' with '\\n', only disable in explain/show create stmts
47+
// whether replace '\n' with '\\n',
48+
// disable in explain/show create stmts or user config setting false
4849
replace_newline: bool,
4950
schema: SchemaRef,
5051
data: RowProgressIterator,
@@ -67,10 +68,10 @@ impl<'a> FormatDisplay<'a> {
6768
Self {
6869
settings,
6970
query,
71+
kind: QueryKind::from(query),
7072
replace_newline,
7173
schema,
7274
data,
73-
kind: QueryKind::from(query),
7475
rows: 0,
7576
progress: None,
7677
start,
@@ -397,7 +398,7 @@ fn create_table(
397398
schema: SchemaRef,
398399
results: &[Row],
399400
replace_newline: bool,
400-
mut max_rows: usize,
401+
max_rows: usize,
401402
mut max_width: usize,
402403
max_col_width: usize,
403404
) -> Result<Table> {
@@ -417,15 +418,12 @@ fn create_table(
417418
}
418419
}
419420

420-
if !replace_newline {
421-
max_width = usize::MAX;
422-
max_rows = usize::MAX;
423-
}
424-
425421
let row_count: usize = results.len();
426422
let mut rows_to_render = row_count.min(max_rows);
427-
428-
if row_count <= max_rows + 3 {
423+
if !replace_newline {
424+
max_width = usize::MAX;
425+
rows_to_render = row_count;
426+
} else if row_count <= max_rows + 3 {
429427
// hiding rows adds 3 extra rows
430428
// so hiding rows makes no sense if we are only slightly over the limit
431429
// if we are 1 row over the limit hiding rows will actually increase the number of lines we display!

cli/src/helper.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ impl KeyWordCompleter {
192192
}),
193193
);
194194

195-
(pos - hint.len(), results)
195+
if pos >= hint.len() {
196+
(pos - hint.len(), results)
197+
} else {
198+
(0, results)
199+
}
196200
}
197201
}

cli/src/session.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,11 @@ impl Session {
317317
Ok(false)
318318
}
319319
_ => {
320-
let replace_newline = replace_newline_in_box_display(query);
320+
let replace_newline = !if self.settings.replace_newline {
321+
false
322+
} else {
323+
replace_newline_in_box_display(query)
324+
};
321325
let (schema, data) = self.conn.query_iter_ext(query).await?;
322326
let mut displayer = FormatDisplay::new(
323327
&self.settings,

0 commit comments

Comments
 (0)