mirror of
				https://github.com/BurntSushi/ripgrep.git
				synced 2025-10-30 23:17:47 +02:00 
			
		
		
		
	
		
			
	
	
		
			48 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
		
		
			
		
	
	
			48 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
|   | /*!
 | ||
|  | Provides completions for ripgrep's CLI for the fish shell.
 | ||
|  | */
 | ||
|  | 
 | ||
|  | use crate::flags::defs::FLAGS;
 | ||
|  | 
 | ||
|  | const TEMPLATE: &'static str =
 | ||
|  |     "complete -c rg -n '__fish_use_subcommand' !SHORT! !LONG! !DOC!\n";
 | ||
|  | const TEMPLATE_CHOICES: &'static str =
 | ||
|  |     "complete -c rg -n '__fish_use_subcommand' !SHORT! !LONG! !DOC! -r -f -a '!CHOICES!'\n";
 | ||
|  | 
 | ||
|  | /// Generate completions for Fish.
 | ||
|  | ///
 | ||
|  | /// Note that these completions are based on what was produced for ripgrep <=13
 | ||
|  | /// using Clap 2.x. Improvements on this are welcome.
 | ||
|  | pub(crate) fn generate() -> String {
 | ||
|  |     let mut out = String::new();
 | ||
|  |     for flag in FLAGS.iter() {
 | ||
|  |         let short = match flag.name_short() {
 | ||
|  |             None => "".to_string(),
 | ||
|  |             Some(byte) => format!("-s {}", char::from(byte)),
 | ||
|  |         };
 | ||
|  |         let long = format!("-l '{}'", flag.name_long().replace("'", "\\'"));
 | ||
|  |         let doc = format!("-d '{}'", flag.doc_short().replace("'", "\\'"));
 | ||
|  |         let template = if flag.doc_choices().is_empty() {
 | ||
|  |             TEMPLATE.to_string()
 | ||
|  |         } else {
 | ||
|  |             TEMPLATE_CHOICES
 | ||
|  |                 .replace("!CHOICES!", &flag.doc_choices().join(" "))
 | ||
|  |         };
 | ||
|  |         out.push_str(
 | ||
|  |             &template
 | ||
|  |                 .replace("!SHORT!", &short)
 | ||
|  |                 .replace("!LONG!", &long)
 | ||
|  |                 .replace("!DOC!", &doc),
 | ||
|  |         );
 | ||
|  |         if let Some(negated) = flag.name_negated() {
 | ||
|  |             out.push_str(
 | ||
|  |                 &template
 | ||
|  |                     .replace("!SHORT!", "")
 | ||
|  |                     .replace("!LONG!", &negated)
 | ||
|  |                     .replace("!DOC!", &doc),
 | ||
|  |             );
 | ||
|  |         }
 | ||
|  |     }
 | ||
|  |     out
 | ||
|  | }
 |