Skip to content
Myr

bash.rs

This file

Path

crates/core/flags/complete/bash.rs

107 lines · 2.7 KiB · Rust

Last changed by

Not known.

History of this file

Take it

1/*!2Provides completions for ripgrep's CLI for the bash shell.3*/45use crate::flags::defs::FLAGS;67const TEMPLATE_FULL: &'static str = "8_rg() {9  local i cur prev opts cmds10  COMPREPLY=()11  cur=\"${COMP_WORDS[COMP_CWORD]}\"12  prev=\"${COMP_WORDS[COMP_CWORD-1]}\"13  cmd=\"\"14  opts=\"\"1516  for i in ${COMP_WORDS[@]}; do17    case \"${i}\" in18      rg)19        cmd=\"rg\"20        ;;21      *)22        ;;23    esac24  done2526  case \"${cmd}\" in27    rg)28      opts=\"!OPTS!\"29      if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then30        COMPREPLY=($(compgen -W \"${opts}\" -- \"${cur}\"))31        return 032      fi33      case \"${prev}\" in34!CASES!35      esac36      COMPREPLY=($(compgen -W \"${opts}\" -- \"${cur}\"))37      return 038      ;;39  esac40}4142complete -F _rg -o bashdefault -o default rg43";4445const TEMPLATE_CASE: &'static str = "46        !FLAG!)47          COMPREPLY=($(compgen -f \"${cur}\"))48          return 049          ;;50";5152const TEMPLATE_CASE_CHOICES: &'static str = "53        !FLAG!)54          COMPREPLY=($(compgen -W \"!CHOICES!\" -- \"${cur}\"))55          return 056          ;;57";5859/// Generate completions for Bash.60///61/// Note that these completions are based on what was produced for ripgrep <=1362/// using Clap 2.x. Improvements on this are welcome.63pub(crate) fn generate() -> String {64    let mut opts = String::new();65    for flag in FLAGS.iter() {66        opts.push_str("--");67        opts.push_str(flag.name_long());68        opts.push(' ');69        if let Some(short) = flag.name_short() {70            opts.push('-');71            opts.push(char::from(short));72            opts.push(' ');73        }74        if let Some(name) = flag.name_negated() {75            opts.push_str("--");76            opts.push_str(name);77            opts.push(' ');78        }79    }80    opts.push_str("<PATTERN> <PATH>...");8182    let mut cases = String::new();83    for flag in FLAGS.iter() {84        let template = if !flag.doc_choices().is_empty() {85            let choices = flag.doc_choices().join(" ");86            TEMPLATE_CASE_CHOICES.trim_end().replace("!CHOICES!", &choices)87        } else {88            TEMPLATE_CASE.trim_end().to_string()89        };90        let name = format!("--{}", flag.name_long());91        cases.push_str(&template.replace("!FLAG!", &name));92        if let Some(short) = flag.name_short() {93            let name = format!("-{}", char::from(short));94            cases.push_str(&template.replace("!FLAG!", &name));95        }96        if let Some(negated) = flag.name_negated() {97            let name = format!("--{negated}");98            cases.push_str(&template.replace("!FLAG!", &name));99        }100    }101102    TEMPLATE_FULL103        .replace("!OPTS!", &opts)104        .replace("!CASES!", &cases)105        .trim_start()106        .to_string()107}