Skip to content

Apache DataFusion SQL Query Engine

feat(datafusion-cli): enhance CLI helper with default hint (#20310)

dario currerif471aaf27539
diff --git a/datafusion-cli/src/helper.rs b/datafusion-cli/src/helper.rs
index df7afc1404..c53272ee19 100644
--- a/datafusion-cli/src/helper.rs
+++ b/datafusion-cli/src/helper.rs
@@ -20,7 +20,7 @@
use std::borrow::Cow;
-use crate::highlighter::{NoSyntaxHighlighter, SyntaxHighlighter};
+use crate::highlighter::{Color, NoSyntaxHighlighter, SyntaxHighlighter};
use datafusion::sql::parser::{DFParser, Statement};
use datafusion::sql::sqlparser::dialect::dialect_from_str;
@@ -33,6 +33,9 @@ use rustyline::hint::Hinter;
use rustyline::validate::{ValidationContext, ValidationResult, Validator};
use rustyline::{Context, Helper, Result};
+/// Default suggestion shown when the input line is empty.
+const DEFAULT_HINT_SUGGESTION: &str = " \\? for help, \\q to quit";
+
pub struct CliHelper {
completer: FilenameCompleter,
dialect: Dialect,
@@ -114,6 +117,15 @@ impl Highlighter for CliHelper {
impl Hinter for CliHelper {
type Hint = String;
+
+ fn hint(&self, line: &str, _pos: usize, _ctx: &Context<'_>) -> Option<String> {
+ if line.trim().is_empty() {
+ let suggestion = Color::gray(DEFAULT_HINT_SUGGESTION);
+ Some(suggestion)
+ } else {
+ None
+ }
+ }
}
/// returns true if the current position is after the open quote for
diff --git a/datafusion-cli/src/highlighter.rs b/datafusion-cli/src/highlighter.rs
index 912a13916a..0a2a2e6c14 100644
--- a/datafusion-cli/src/highlighter.rs
+++ b/datafusion-cli/src/highlighter.rs
@@ -80,16 +80,20 @@ impl Highlighter for SyntaxHighlighter {
}
/// Convenient utility to return strings with [ANSI color](https://gist.github.com/JBlond/2fea43a3049b38287e5e9cefc87b2124).
-struct Color {}
+pub(crate) struct Color {}
impl Color {
- fn green(s: impl Display) -> String {
+ pub(crate) fn green(s: impl Display) -> String {
format!("\x1b[92m{s}\x1b[0m")
}
- fn red(s: impl Display) -> String {
+ pub(crate) fn red(s: impl Display) -> String {
format!("\x1b[91m{s}\x1b[0m")
}
+
+ pub(crate) fn gray(s: impl Display) -> String {
+ format!("\x1b[90m{s}\x1b[0m")
+ }
}
#[cfg(test)]