fish.rs
This file
Path
crates/core/flags/complete/fish.rs
75 lines · 2.6 KiB · Rust
Last changed by
style: apply rustfmtAndrew Gallantbefore adoptionbb8172f
History of this fileTake it
The walk stopped after 200 commits: lines older than 5f5da48 point at it.
globset: support nested alternatesLuke Sandbergbefore adoption5f5da48
lines 1–4why, one step back1/*!2Provides completions for ripgrep's CLI for the fish shell.3*/4style: apply rustfmtAndrew Gallantbefore adoptionbb8172f
line 5why, one step back5use crate::flags::{CompletionType, defs::FLAGS};globset: support nested alternatesLuke Sandbergbefore adoption5f5da48
lines 6–7why, one step back67const TEMPLATE: &'static str = "complete -c rg !SHORT! -l !LONG! -d '!DOC!'";style: apply rustfmtAndrew Gallantbefore adoptionbb8172f
line 8why, one step back8const TEMPLATE_NEGATED: &'static str = "complete -c rg -l !NEGATED! -n '__rg_contains_opt !LONG! !SHORT!' -d '!DOC!'\n";globset: support nested alternatesLuke Sandbergbefore adoption5f5da48
lines 9–75why, one step back910/// Generate completions for Fish.11///12/// Reference: <https://fishshell.com/docs/current/completions.html>13pub(crate) fn generate() -> String {14 let mut out = String::new();15 out.push_str(include_str!("prelude.fish"));16 out.push('\n');17 for flag in FLAGS.iter() {18 let short = match flag.name_short() {19 None => "".to_string(),20 Some(byte) => format!("-s {}", char::from(byte)),21 };22 let long = flag.name_long();23 let doc = flag.doc_short().replace("'", "\\'");24 let mut completion = TEMPLATE25 .replace("!SHORT!", &short)26 .replace("!LONG!", &long)27 .replace("!DOC!", &doc);2829 match flag.completion_type() {30 CompletionType::Filename => {31 completion.push_str(" -r -F");32 }33 CompletionType::Executable => {34 completion.push_str(" -r -f -a '(__fish_complete_command)'");35 }36 CompletionType::Filetype => {37 completion.push_str(38 " -r -f -a '(rg --type-list | string replace : \\t)'",39 );40 }41 CompletionType::Encoding => {42 completion.push_str(" -r -f -a '");43 completion.push_str(super::ENCODINGS);44 completion.push_str("'");45 }46 CompletionType::Other if !flag.doc_choices().is_empty() => {47 completion.push_str(" -r -f -a '");48 completion.push_str(&flag.doc_choices().join(" "));49 completion.push_str("'");50 }51 CompletionType::Other if !flag.is_switch() => {52 completion.push_str(" -r -f");53 }54 CompletionType::Other => (),55 }5657 completion.push('\n');58 out.push_str(&completion);5960 if let Some(negated) = flag.name_negated() {61 let short = match flag.name_short() {62 None => "".to_string(),63 Some(byte) => char::from(byte).to_string(),64 };65 out.push_str(66 &TEMPLATE_NEGATED67 .replace("!NEGATED!", &negated)68 .replace("!SHORT!", &short)69 .replace("!LONG!", &long)70 .replace("!DOC!", &doc),71 );72 }73 }74 out75}