Skip to content

Commit f471aaf

Browse files
authored
feat(datafusion-cli): enhance CLI helper with default hint (#20310)
# Pull Request Description ## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> When the CLI prompt is empty, users have no indication of how to get help or exit. Showing a subtle gray hint (e.g. `\? for help, \q to quit`) on an empty line improves discoverability without changing any existing behavior. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> - Implement `Hinter::hint` on `CliHelper` so that when the current line is empty, a default hint is shown (e.g. `\? for help, \q to quit`) in gray. - Add a `DEFAULT_HINT_SUGGESTION` constant and use it for the hint text. - Expose the highlighter’s `Color` type and its methods as `pub(crate)` and add `Color::gray()` for styling the hint. ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> No new tests were added; the change is a small UI hint. Manual testing: start the CLI and confirm the hint appears on an empty line and disappears when typing. Happy to add tests if the project expects them for this behavior. ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> Yes. When the input line is empty, users now see a gray hint: `\? for help, \q to quit`. No API or CLI flag changes. Documentation could optionally mention this hint in the CLI docs if the project documents REPL UX. <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 7d217b1 commit f471aaf

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

datafusion-cli/src/helper.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
use std::borrow::Cow;
2222

23-
use crate::highlighter::{NoSyntaxHighlighter, SyntaxHighlighter};
23+
use crate::highlighter::{Color, NoSyntaxHighlighter, SyntaxHighlighter};
2424

2525
use datafusion::sql::parser::{DFParser, Statement};
2626
use datafusion::sql::sqlparser::dialect::dialect_from_str;
@@ -33,6 +33,9 @@ use rustyline::hint::Hinter;
3333
use rustyline::validate::{ValidationContext, ValidationResult, Validator};
3434
use rustyline::{Context, Helper, Result};
3535

36+
/// Default suggestion shown when the input line is empty.
37+
const DEFAULT_HINT_SUGGESTION: &str = " \\? for help, \\q to quit";
38+
3639
pub struct CliHelper {
3740
completer: FilenameCompleter,
3841
dialect: Dialect,
@@ -114,6 +117,15 @@ impl Highlighter for CliHelper {
114117

115118
impl Hinter for CliHelper {
116119
type Hint = String;
120+
121+
fn hint(&self, line: &str, _pos: usize, _ctx: &Context<'_>) -> Option<String> {
122+
if line.trim().is_empty() {
123+
let suggestion = Color::gray(DEFAULT_HINT_SUGGESTION);
124+
Some(suggestion)
125+
} else {
126+
None
127+
}
128+
}
117129
}
118130

119131
/// returns true if the current position is after the open quote for

datafusion-cli/src/highlighter.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,20 @@ impl Highlighter for SyntaxHighlighter {
8080
}
8181

8282
/// Convenient utility to return strings with [ANSI color](https://gist.github.com/JBlond/2fea43a3049b38287e5e9cefc87b2124).
83-
struct Color {}
83+
pub(crate) struct Color {}
8484

8585
impl Color {
86-
fn green(s: impl Display) -> String {
86+
pub(crate) fn green(s: impl Display) -> String {
8787
format!("\x1b[92m{s}\x1b[0m")
8888
}
8989

90-
fn red(s: impl Display) -> String {
90+
pub(crate) fn red(s: impl Display) -> String {
9191
format!("\x1b[91m{s}\x1b[0m")
9292
}
93+
94+
pub(crate) fn gray(s: impl Display) -> String {
95+
format!("\x1b[90m{s}\x1b[0m")
96+
}
9397
}
9498

9599
#[cfg(test)]

0 commit comments

Comments
 (0)