use rustyline::{
Completer, Helper, Highlighter, Hinter,
completion::Completer,
highlight::Highlighter,
hint::Hinter,
validate::{ValidationContext, ValidationResult, Validator},
};
use crate::style::*;
const COMMAND_NAMES: &[&str] = &[
"/add",
"/agent",
"/apikey",
"/audit",
"/autoaccept",
"/casual",
"/clear",
"/command",
"/compact",
"/context",
"/contextlimit",
"/drop",
"/dropall",
"/exit",
"/files",
"/help",
"/init",
"/mode",
"/model",
"/plan",
"/quit",
"/refresh",
"/rename",
"/retries",
"/research",
"/session",
"/sessions",
"/settings",
"/showthink",
"/skill",
"/skills",
"/think",
"/timeout",
"/unload",
"/use",
];
fn subcommand_completions(cmd: &str) -> Vec<&'static str> {
match cmd {
"/command" => vec![
"add",
"deny",
"help",
"list",
"rm",
"reset",
"resetdeny",
"undeny",
],
"/session" => vec!["delete"],
"/mode" => vec!["agent", "casual", "planning", "research"],
"/settings" => vec!["all"],
"/autoaccept" => vec!["off", "on"],
"/apikey" => vec!["clear"],
"/showthink" => vec!["off", "on"],
"/think" => vec!["high", "low", "medium", "off"],
_ => vec![],
}
}
#[derive(Completer, Helper, Highlighter, Hinter)]
pub struct CommandHelper {
#[rustyline(Completer)]
completer: CommandCompleter,
#[rustyline(Hinter)]
hinter: CommandHinter,
#[rustyline(Highlighter)]
highlighter: CommandHighlighter,
}
impl Validator for CommandHelper {
fn validate(&self, ctx: &mut ValidationContext<'_>) -> rustyline::Result<ValidationResult> {
let input = ctx.input();
let trimmed = input.trim_end();
if trimmed.ends_with('\\') {
return Ok(ValidationResult::Incomplete);
}
let fence_count = input.matches("```").count();
if fence_count % 2 == 1 {
return Ok(ValidationResult::Incomplete);
}
let backtick_count = input.matches('`').count();
if backtick_count % 2 == 1 {
return Ok(ValidationResult::Incomplete);
}
Ok(ValidationResult::Valid(None))
}
}
impl Default for CommandHelper {
fn default() -> Self {
Self {
completer: CommandCompleter,
hinter: CommandHinter,
highlighter: CommandHighlighter,
}
}
}
impl CommandHelper {
pub fn new() -> Self {
Self::default()
}
}
pub struct CommandCompleter;
impl Completer for CommandCompleter {
type Candidate = String;
fn complete(
&self,
line: &str,
pos: usize,
_ctx: &rustyline::Context<'_>,
) -> rustyline::Result<(usize, Vec<Self::Candidate>)> {
if !line.starts_with('/') || pos == 0 {
return Ok((0, vec![]));
}
let prefix = &line[..pos];
if let Some(space_pos) = prefix.find(' ') {
let cmd = &prefix[..space_pos].to_lowercase();
let sub_prefix = prefix[space_pos + 1..].trim_start().to_lowercase();
let subs = subcommand_completions(cmd);
if !subs.is_empty() {
let matches: Vec<String> = subs
.iter()
.filter(|s| s.starts_with(&sub_prefix))
.take(5)
.map(|s| format!("{} {}", cmd, s))
.collect();
if matches.is_empty() {
return Ok((0, vec![]));
}
return Ok((0, matches));
}
}
let cmd_prefix = prefix.to_lowercase();
let matches: Vec<String> = COMMAND_NAMES
.iter()
.filter(|name| name.starts_with(&cmd_prefix))
.take(3)
.map(|s| s.to_string())
.collect();
if matches.is_empty() {
return Ok((0, vec![]));
}
Ok((0, matches))
}
}
pub struct CommandHinter;
impl Hinter for CommandHinter {
type Hint = String;
fn hint(&self, line: &str, pos: usize, _ctx: &rustyline::Context<'_>) -> Option<Self::Hint> {
if !line.starts_with('/') || pos == 0 || line.len() != pos {
return None;
}
if let Some(space_pos) = line.find(' ') {
let cmd = &line[..space_pos].to_lowercase();
let sub_prefix = line[space_pos + 1..].trim_start().to_lowercase();
let subs = subcommand_completions(cmd);
if !subs.is_empty() {
let matches: Vec<&&str> = subs
.iter()
.filter(|s| s.starts_with(&sub_prefix))
.take(5)
.collect();
if matches.is_empty() {
return None;
}
if matches.len() == 1 && sub_prefix.is_empty() {
return Some(format!(" {}", matches[0]));
}
if matches.len() == 1 && sub_prefix == *matches[0] {
return None;
}
let suggestions: Vec<&str> = matches.iter().map(|s| **s).collect();
return Some(format!(" ({})", suggestions.join(" | ")));
}
}
let prefix = line.to_lowercase();
let matches: Vec<&str> = COMMAND_NAMES
.iter()
.filter(|name| name.starts_with(&prefix))
.take(3)
.copied()
.collect();
if matches.is_empty() {
return None;
}
if matches.len() == 1 {
let hint = matches[0][pos..].to_string();
if !hint.is_empty() {
return Some(hint);
}
}
let suggestions = matches.join(" ");
Some(format!(" ({})", suggestions))
}
}
pub struct CommandHighlighter;
impl Highlighter for CommandHighlighter {
fn highlight<'l>(&self, line: &'l str, _pos: usize) -> std::borrow::Cow<'l, str> {
if line.starts_with('/') {
std::borrow::Cow::Owned(format!("{}{}{}", TITLE_COLOR, line, RESET))
} else {
std::borrow::Cow::Borrowed(line)
}
}
fn highlight_hint<'l>(&self, hint: &'l str) -> std::borrow::Cow<'l, str> {
std::borrow::Cow::Owned(format!("{}{}{}", GRAY, hint, RESET))
}
fn highlight_candidate<'l>(
&self,
candidate: &'l str,
_completion: rustyline::CompletionType,
) -> std::borrow::Cow<'l, str> {
std::borrow::Cow::Owned(format!("{}{}{}", TITLE_COLOR, candidate, RESET))
}
}