Skip to content

Commit

Permalink
feat: Disable client console highlight by default (#9013)
Browse files Browse the repository at this point in the history
* console highlight disabled by default
  • Loading branch information
comphead committed Jan 27, 2024
1 parent 9c4affe commit ff7dfc3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 18 deletions.
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,
}

#[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

0 comments on commit ff7dfc3

Please sign in to comment.