-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move dynamic auto-complete to rust (#113)
* move dynamic auto-complete to rust * fix scoop
- Loading branch information
1 parent
722bca3
commit cbe6f1d
Showing
6 changed files
with
86 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,60 @@ | ||
use std::{fs::File, io::BufWriter, path::PathBuf}; | ||
use std::{ | ||
fs::File, | ||
io::{self, Cursor, Write}, | ||
path::PathBuf, | ||
}; | ||
|
||
use clap::Command; | ||
use clap_complete::{generate, Generator, Shell}; | ||
|
||
use crate::cli::command; | ||
|
||
const AUTO_COMPLETE_FUNCTIONS_ZSH: &str = include_str!("autocomplete_functions/zshell.zsh"); | ||
|
||
pub fn auto_complete(shell: Shell, out: PathBuf) { | ||
let mut cmd = command::get(); | ||
eprintln!("Generating completion file for {shell}..."); | ||
let file = File::create(out).unwrap(); | ||
let mut buf_writer = BufWriter::new(file); | ||
completions_to_file(shell, &mut cmd, &mut buf_writer); | ||
let mut buffer: Vec<u8> = Vec::new(); | ||
let mut cursor = io::Cursor::new(&mut buffer); | ||
|
||
generate_autocomplete_buf(shell, &mut cmd, &mut cursor); | ||
let auto_complete_script = String::from_utf8(buffer).unwrap(); | ||
let auto_complete_script = make_dynamic_completion_script(shell, &auto_complete_script); | ||
let mut output_file = File::create(out).unwrap(); | ||
output_file | ||
.write_all(auto_complete_script.as_bytes()) | ||
.unwrap(); | ||
} | ||
|
||
fn generate_autocomplete_buf<G: Generator>( | ||
gen: G, | ||
cmd: &mut Command, | ||
buf: &mut Cursor<&mut Vec<u8>>, | ||
) { | ||
generate(gen, cmd, cmd.get_name().to_string(), buf); | ||
} | ||
|
||
fn make_dynamic_completion_script(shell: Shell, auto_complete_static: &str) -> String { | ||
match shell { | ||
Shell::Bash => todo!(), | ||
Shell::Elvish => unimplemented!(), | ||
Shell::Fish => unimplemented!(), | ||
Shell::PowerShell => todo!(), | ||
Shell::Zsh => handle_zsh(auto_complete_static), | ||
_ => todo!(), | ||
} | ||
} | ||
|
||
fn completions_to_file<G: Generator>(gen: G, cmd: &mut Command, file: &mut BufWriter<File>) { | ||
generate(gen, cmd, cmd.get_name().to_string(), file); | ||
fn handle_zsh(auto_complete_static: &str) -> String { | ||
let mut auto_complete_static = auto_complete_static | ||
.replace( | ||
"datastore_name -- The name as defined in Satori data portal:", | ||
"datastore_name -- The name as defined in Satori data portal:_datastores", | ||
) | ||
.replace( | ||
"database -- Database name:", | ||
"database -- Database name:_databases", | ||
); | ||
auto_complete_static.push_str(AUTO_COMPLETE_FUNCTIONS_ZSH); | ||
auto_complete_static | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
|
||
_datastores() { | ||
local datastore_names=() | ||
|
||
# Read keys line by line and populate the array | ||
while IFS= read -r line; do | ||
datastore_names+=("$line") | ||
done < <(satori list --datastores) | ||
|
||
_values datastore_name $datastore_names | ||
} | ||
|
||
|
||
_databases() { | ||
local datastore_name="$words[CURRENT-1]" # Get the selected datastore_name | ||
datastore_name=${datastore_name//\\/} | ||
local databases=() | ||
while IFS= read -r line; do | ||
databases+=("$line") | ||
done < <(satori list --databases $datastore_name) | ||
if [[ ! -z "$databases" ]]; then | ||
_values database $databases | ||
fi | ||
} |