Skip to content

feat: Disable client console highlight by default #9013

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 4 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions datafusion-cli/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pub async fn exec_from_repl(
let mut rl = Editor::new()?;
rl.set_helper(Some(CliHelper::new(
&ctx.task_ctx().session_config().options().sql_parser.dialect,
print_options.color,
)));
rl.load_history(".history").ok();

Expand Down
15 changes: 10 additions & 5 deletions datafusion-cli/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,25 @@ use rustyline::Context;
use rustyline::Helper;
use rustyline::Result;

use crate::highlighter::SyntaxHighlighter;
use crate::highlighter::{NoSyntaxHighlighter, SyntaxHighlighter};

pub struct CliHelper {
completer: FilenameCompleter,
dialect: String,
highlighter: SyntaxHighlighter,
highlighter: Box<dyn Highlighter>,
}

impl CliHelper {
pub fn new(dialect: &str) -> Self {
pub fn new(dialect: &str, color: bool) -> Self {
let highlighter: Box<dyn Highlighter> = if !color {
Box::new(NoSyntaxHighlighter {})
} else {
Box::new(SyntaxHighlighter::new(dialect))
};
Self {
completer: FilenameCompleter::new(),
dialect: dialect.into(),
highlighter: SyntaxHighlighter::new(dialect),
highlighter,
}
}

Expand Down Expand Up @@ -102,7 +107,7 @@ impl CliHelper {

impl Default for CliHelper {
fn default() -> Self {
Self::new("generic")
Self::new("generic", false)
}
}

Expand Down
10 changes: 6 additions & 4 deletions datafusion-cli/src/highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,22 @@ use datafusion::sql::sqlparser::{
use rustyline::highlight::Highlighter;

/// The syntax highlighter.
#[derive(Debug)]
pub struct SyntaxHighlighter {
dialect: Box<dyn Dialect>,
}

impl SyntaxHighlighter {
pub fn new(dialect: &str) -> Self {
let dialect = match dialect_from_str(dialect) {
Some(dialect) => dialect,
None => Box::new(GenericDialect {}),
};
let dialect = dialect_from_str(dialect).unwrap_or(Box::new(GenericDialect {}));
Self { dialect }
}
}

pub struct NoSyntaxHighlighter {}

impl Highlighter for NoSyntaxHighlighter {}

impl Highlighter for SyntaxHighlighter {
fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> {
let mut out_line = String::new();
Expand Down
3 changes: 1 addition & 2 deletions datafusion-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ pub mod command;
pub mod exec;
pub mod functions;
pub mod helper;
pub mod highlighter;
pub mod object_storage;
pub mod print_format;
pub mod print_options;

mod highlighter;
18 changes: 11 additions & 7 deletions datafusion-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ struct Args {
default_value = "40"
)]
maxrows: MaxRows,

#[clap(long, help = "Enables console syntax highlighting")]
color: bool,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe call this syntax_highlighting?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another potential option might be to follow the duckdb lead and call it ascii output mode 🤔 (when there is no color)

andrewlamb@Andrews-MacBook-Pro:~/Software/influxdb_iox$ duckdb --help
Usage: duckdb [OPTIONS] FILENAME [SQL]
FILENAME is the name of an DuckDB database. A new database is created
if the file does not previously exist.
OPTIONS include:
   -append              append the database to the end of the file
   -ascii               set output mode to 'ascii'
   -bail                stop after hitting an error
   -batch               force batch I/O
   -box                 set output mode to 'box'
   -column              set output mode to 'column'
   -cmd COMMAND         run "COMMAND" before reading stdin
   -c COMMAND           run "COMMAND" and exit
   -csv                 set output mode to 'csv'
   -echo                print commands before execution
   -init FILENAME       read/process named file
   -[no]header          turn headers on or off
   -help                show this message
   -html                set output mode to HTML
   -interactive         force interactive I/O
   -json                set output mode to 'json'
   -line                set output mode to 'line'
   -list                set output mode to 'list'
   -markdown            set output mode to 'markdown'
   -newline SEP         set output row separator. Default: '\n'
   -nofollow            refuse to open symbolic links to database files
   -no-stdin            exit after processing options instead of reading stdin
   -nullvalue TEXT      set text string for NULL values. Default ''
   -quote               set output mode to 'quote'
   -readonly            open the database read-only
   -s COMMAND           run "COMMAND" and exit
   -separator SEP       set output column separator. Default: '|'
   -stats               print memory stats before each finalize
   -table               set output mode to 'table'
   -unsigned            allow loading of unsigned extensions
   -version             show DuckDB version

}

#[tokio::main]
Expand Down Expand Up @@ -169,28 +172,28 @@ async fn main_inner() -> Result<()> {
session_config = session_config.with_batch_size(batch_size);
};

let rn_config = RuntimeConfig::new();
let rn_config =
let rt_config = RuntimeConfig::new();
let rt_config =
// set memory pool size
if let Some(memory_limit) = args.memory_limit {
let memory_limit = extract_memory_pool_size(&memory_limit).unwrap();
// set memory pool type
if let Some(mem_pool_type) = args.mem_pool_type {
match mem_pool_type {
PoolType::Greedy => rn_config
PoolType::Greedy => rt_config
.with_memory_pool(Arc::new(GreedyMemoryPool::new(memory_limit))),
PoolType::Fair => rn_config
PoolType::Fair => rt_config
.with_memory_pool(Arc::new(FairSpillPool::new(memory_limit))),
}
} else {
rn_config
rt_config
.with_memory_pool(Arc::new(GreedyMemoryPool::new(memory_limit)))
}
} else {
rn_config
rt_config
};

let runtime_env = create_runtime_env(rn_config.clone())?;
let runtime_env = create_runtime_env(rt_config.clone())?;

let mut ctx =
SessionContext::new_with_config_rt(session_config.clone(), Arc::new(runtime_env));
Expand All @@ -207,6 +210,7 @@ async fn main_inner() -> Result<()> {
format: args.format,
quiet: args.quiet,
maxrows: args.maxrows,
color: args.color,
};

let commands = args.command;
Expand Down
1 change: 1 addition & 0 deletions datafusion-cli/src/print_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub struct PrintOptions {
pub format: PrintFormat,
pub quiet: bool,
pub maxrows: MaxRows,
pub color: bool,
}

fn get_timing_info_str(
Expand Down
3 changes: 3 additions & 0 deletions docs/source/user-guide/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ OPTIONS:
-c, --command <COMMAND>...
Execute the given command string(s), then exit

--color
Enables console syntax highlighting

-f, --file <FILE>...
Execute commands from file(s), then exit

Expand Down