mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-18 17:14:20 +01:00
customize completion candidates for some args
This commit is contained in:
@@ -467,7 +467,8 @@ impl<'a> Limbo<'a> {
|
|||||||
}
|
}
|
||||||
match CommandParser::try_parse_from(args) {
|
match CommandParser::try_parse_from(args) {
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
let _ = self.write_fmt(format_args!("{err}"));
|
// Let clap print with Styled Colors instead
|
||||||
|
let _ = err.print();
|
||||||
}
|
}
|
||||||
Ok(cmd) => match cmd.command {
|
Ok(cmd) => match cmd.command {
|
||||||
Command::Exit(args) => {
|
Command::Exit(args) => {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use clap::{Args, ValueEnum};
|
use clap::{Args, ValueEnum};
|
||||||
|
use clap_complete::{ArgValueCompleter, CompletionCandidate, PathCompleter};
|
||||||
|
|
||||||
use crate::input::OutputMode;
|
use crate::{input::OutputMode, opcodes_dictionary::OPCODE_DESCRIPTIONS};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct ExitArgs {
|
pub struct ExitArgs {
|
||||||
@@ -12,13 +13,17 @@ pub struct ExitArgs {
|
|||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct OpenArgs {
|
pub struct OpenArgs {
|
||||||
/// Path to open database
|
/// Path to open database
|
||||||
|
#[arg(add = ArgValueCompleter::new(PathCompleter::file()))]
|
||||||
pub path: String,
|
pub path: String,
|
||||||
|
// TODO see how to have this completed with the output of List Vfs function
|
||||||
|
// Currently not possible to pass arbitrary
|
||||||
/// Name of VFS
|
/// Name of VFS
|
||||||
pub vfs_name: Option<String>,
|
pub vfs_name: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct SchemaArgs {
|
pub struct SchemaArgs {
|
||||||
|
// TODO depends on PRAGMA table_list for completions
|
||||||
/// Table name to visualize schema
|
/// Table name to visualize schema
|
||||||
pub table_name: Option<String>,
|
pub table_name: Option<String>,
|
||||||
}
|
}
|
||||||
@@ -26,6 +31,7 @@ pub struct SchemaArgs {
|
|||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct SetOutputArgs {
|
pub struct SetOutputArgs {
|
||||||
/// File path to send output to
|
/// File path to send output to
|
||||||
|
#[arg(add = ArgValueCompleter::new(PathCompleter::file()))]
|
||||||
pub path: Option<String>,
|
pub path: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,15 +41,40 @@ pub struct OutputModeArgs {
|
|||||||
pub mode: OutputMode,
|
pub mode: OutputMode,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn opcodes_completer(current: &std::ffi::OsStr) -> Vec<CompletionCandidate> {
|
||||||
|
let mut completions = vec![];
|
||||||
|
|
||||||
|
let Some(current) = current.to_str() else {
|
||||||
|
return completions;
|
||||||
|
};
|
||||||
|
|
||||||
|
let current = current.to_lowercase();
|
||||||
|
|
||||||
|
let opcodes = &OPCODE_DESCRIPTIONS;
|
||||||
|
|
||||||
|
for op in opcodes {
|
||||||
|
// TODO if someone know how to do prefix_match with case insensitve in Rust
|
||||||
|
// without converting the String to lowercase first, please fix this.
|
||||||
|
let op_name = op.name.to_ascii_lowercase();
|
||||||
|
if op_name.starts_with(¤t) {
|
||||||
|
completions.push(CompletionCandidate::new(op.name).help(Some(op.description.into())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
completions
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct OpcodesArgs {
|
pub struct OpcodesArgs {
|
||||||
/// Opcode to display description
|
/// Opcode to display description
|
||||||
|
#[arg(add = ArgValueCompleter::new(opcodes_completer))]
|
||||||
pub opcode: Option<String>,
|
pub opcode: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct CwdArgs {
|
pub struct CwdArgs {
|
||||||
/// Target directory
|
/// Target directory
|
||||||
|
#[arg(add = ArgValueCompleter::new(PathCompleter::dir()))]
|
||||||
pub directory: String,
|
pub directory: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,11 +103,6 @@ pub struct TablesArgs {
|
|||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct LoadExtensionArgs {
|
pub struct LoadExtensionArgs {
|
||||||
/// Path to extension file
|
/// Path to extension file
|
||||||
pub path: String,
|
#[arg(add = ArgValueCompleter::new(PathCompleter::file()))]
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Args)]
|
|
||||||
pub struct ListVfsArgs {
|
|
||||||
/// Path to extension file
|
|
||||||
pub path: String,
|
pub path: String,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,9 +35,6 @@ pub enum Command {
|
|||||||
/// Open a database file
|
/// Open a database file
|
||||||
#[command(display_name = ".open")]
|
#[command(display_name = ".open")]
|
||||||
Open(OpenArgs),
|
Open(OpenArgs),
|
||||||
/// Print this message or the help of the given subcommand(s)
|
|
||||||
// #[command(display_name = ".help")]
|
|
||||||
// Help,
|
|
||||||
/// Display schema for a table
|
/// Display schema for a table
|
||||||
#[command(display_name = ".schema")]
|
#[command(display_name = ".schema")]
|
||||||
Schema(SchemaArgs),
|
Schema(SchemaArgs),
|
||||||
|
|||||||
@@ -107,6 +107,7 @@ impl<C: Parser + Send + Sync + 'static> SqlCompleter<C> {
|
|||||||
mut line: &str,
|
mut line: &str,
|
||||||
mut pos: usize,
|
mut pos: usize,
|
||||||
) -> rustyline::Result<(usize, Vec<Pair>)> {
|
) -> rustyline::Result<(usize, Vec<Pair>)> {
|
||||||
|
// TODO maybe check to see if the line is empty and then just output the command names
|
||||||
line = &line[1..];
|
line = &line[1..];
|
||||||
pos = pos - 1;
|
pos = pos - 1;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user