Skip to content

Commit

Permalink
[Hexa] support multi line edit
Browse files Browse the repository at this point in the history
  • Loading branch information
suibianwanwank committed May 1, 2024
1 parent c760ce1 commit 8dd0b6e
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use rustyline::highlight::Highlighter;
use rustyline::highlight::MatchingBracketHighlighter;
use rustyline::hint::Hinter;
use rustyline::history::SearchDirection;
use rustyline::validate::MatchingBracketValidator;
use rustyline::validate::{ValidationContext, ValidationResult};
use rustyline::CompletionType::List;
use rustyline::validate::Validator;
use rustyline::{
Completer, CompletionType, Config, Context, EditMode, Editor, Helper, Hinter, Validator,
Completer, CompletionType, Config, Context, EditMode, Editor, Helper, Hinter,
};
use snafu::{ResultExt, Snafu};
use std::borrow::Cow;
Expand Down Expand Up @@ -186,18 +187,26 @@ impl Hinter for SqlCliHinter {
}
}

#[derive(Helper, Completer, Validator, Hinter)]
#[derive(Helper, Completer, Hinter)]
struct SqlCliHelper {
#[rustyline(Completer)]
completer: HintCompleter,
highlighter: MatchingBracketHighlighter,
#[rustyline(Validator)]
validator: MatchingBracketValidator,
// #[rustyline(Validator)]
// validator: MatchingBracketValidator,
#[rustyline(Hinter)]
hinter: SqlCliHinter,
colored_prompt: String,
}


impl Validator for SqlCliHelper {
fn validate(&self, ctx: &mut ValidationContext<'_>) -> rustyline::Result<ValidationResult> {
let input = ctx.input().trim_end();
self.validate_input(input)
}
}

struct HintCompleter {}

impl Completer for HintCompleter {
Expand Down Expand Up @@ -234,7 +243,18 @@ impl SqlCliHelper {
highlighter: MatchingBracketHighlighter::new(),
colored_prompt: PROMPT.bright_green().to_string(),
hinter: SqlCliHinter {},
validator: MatchingBracketValidator::new(),
// validator: MatchingBracketValidator::new(),
}
}

fn validate_input(&self, input: &str) -> rustyline::Result<ValidationResult> {
if let Some(_sql) = input.strip_suffix(';') {
Ok(ValidationResult::Valid(None))
} else if input.starts_with('\\') {
// command
Ok(ValidationResult::Valid(None))
} else {
Ok(ValidationResult::Incomplete)
}
}
}
Expand Down

0 comments on commit 8dd0b6e

Please sign in to comment.