Skip to content
Myr

Add new BashCompSubdirsInDir annotation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This first `cd` to a specified directory, then lists the subdirectories therein with `_filedir -d`. This can be used by e.g. `hugo --theme=[Tab][Tab]`, which would give a list of subdirectories under the `themes` directory.

authored by Anthony Fok · committed by Anthony Fok · parent 385fc87 · Browse files · 2 files+24 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+24 added−0 removed

  • other:+24 added−0 removed

2 files+24 added−0 removed

modified:bash_completions_test.go+7 added
54 unmodified lines
55 var flagvalExt string56 c.Flags().StringVar(&flagvalExt, "filename-ext", "", "Enter a filename (extension limited)")57 c.MarkFlagFilename("filename-ext")5859 // subdirectories in a given directory60 var flagvalTheme string61 c.Flags().StringVar(&flagvalTheme, "theme", "", "theme to use (located in /themes/THEMENAME/)")62 c.Flags().SetAnnotation("theme", BashCompSubdirsInDir, []string{"themes"})6364 out := new(bytes.Buffer)65 c.GenBashCompletion(out)66 str := out.String()67
11 unmodified lines
79 // check for filename extension flags80 check(t, str, `flags_completion+=("_filedir")`)81 // check for filename extension flags82 check(t, str, `flags_completion+=("__handle_filename_extension_flag json|yaml|yml")`)83 // check for subdirs_in_dir flags84 check(t, str, `flags_completion+=("__handle_subdirs_in_dir_flag themes")`)8586 checkOmit(t, str, cmdDeprecated.Name())87}
modified:bash_completions.go+17 added
11 unmodified lines
1213const (14 BashCompFilenameExt = "cobra_annotation_bash_completion_filename_extentions"15 BashCompOneRequiredFlag = "cobra_annotation_bash_completion_one_required_flag"16 BashCompSubdirsInDir = "cobra_annotation_bash_completion_subdirs_in_dir"17)1819func preamble(out *bytes.Buffer) {20 fmt.Fprintf(out, `#!/bin/bash
79 unmodified lines
100 local ext="$1"101 _filedir "@(${ext})"102}103104__handle_subdirs_in_dir_flag()105{106 local dir="$1"107 pushd "${dir}" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1108}109110__handle_flag()111{112 __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"113
118 unmodified lines
232 } else {233 ext := "_filedir"234 fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)235 }236 case BashCompSubdirsInDir:237 fmt.Fprintf(out, " flags_with_completion+=(%q)\n", name)238239 if len(value) == 1 {240 ext := "__handle_subdirs_in_dir_flag " + value[0]241 fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)242 } else {243 ext := "_filedir -d"244 fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)245 }246 }247 }248}249
138 unmodified lines