diff options
Diffstat (limited to 'mingling_macros/tmpls/comps')
| -rw-r--r-- | mingling_macros/tmpls/comps/bash.sh | 67 | ||||
| -rw-r--r-- | mingling_macros/tmpls/comps/fish.fish | 120 | ||||
| -rw-r--r-- | mingling_macros/tmpls/comps/pwsh.ps1 | 136 | ||||
| -rw-r--r-- | mingling_macros/tmpls/comps/zsh.zsh | 71 |
4 files changed, 394 insertions, 0 deletions
diff --git a/mingling_macros/tmpls/comps/bash.sh b/mingling_macros/tmpls/comps/bash.sh new file mode 100644 index 0000000..edec28d --- /dev/null +++ b/mingling_macros/tmpls/comps/bash.sh @@ -0,0 +1,67 @@ +#!/usr/bin/env bash +_<<<bin_name>>>_bash_completion() { + local line="${COMP_LINE:0:COMP_POINT}" + local cur="${line##* }" + local prev="" + local word_index=1 + + local before="${line:0:$(( ${#line} - ${#cur} ))}" + local -a before_words + if [[ -n "$before" ]]; then + read -ra before_words <<< "$before" + word_index=$(( ${#before_words[@]} + 1 )) + if [[ $word_index -gt 1 ]]; then + prev="${before_words[${#before_words[@]}-1]}" + fi + fi + + local args=() + args+=(-f "${COMP_LINE//-/^}") + args+=(-C "$COMP_POINT") + args+=(-w "${cur//-/^}") + args+=(-p "${prev//-/^}") + args+=(-c "${COMP_WORDS[0]//-/^}") + args+=(-i "$word_index") + args+=(-F "bash") + + for word in "${COMP_WORDS[@]}"; do + args+=(-a "${word//-/^}") + done + + local suggestions + if suggestions=$(<<<bin_name>>> __comp "${args[@]}" 2>/dev/null); then + if [ $? -eq 0 ]; then + if [ "$suggestions" = "_file_" ]; then + compopt -o default + COMPREPLY=() + return + fi + + if [ -n "$suggestions" ]; then + local -a all_suggestions filtered + mapfile -t all_suggestions < <(printf '%s\n' "$suggestions") + + for suggestion in "${all_suggestions[@]}"; do + [ -z "$cur" ] || [[ "$suggestion" == "$cur"* ]] && filtered+=("$suggestion") + done + + if [ ${#filtered[@]} -gt 0 ]; then + COMPREPLY=("${filtered[@]}") + if [[ "$cur" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then + local colon_prefix="${cur%"${cur##*:}"}" + local -a ltrimmed=() + for suggestion in "${COMPREPLY[@]}"; do + ltrimmed+=("${suggestion#"$colon_prefix"}") + done + COMPREPLY=("${ltrimmed[@]}") + fi + fi + return + fi + fi + fi + + COMPREPLY=() +} + +complete -F _<<<bin_name>>>_bash_completion <<<bin_name>>> diff --git a/mingling_macros/tmpls/comps/fish.fish b/mingling_macros/tmpls/comps/fish.fish new file mode 100644 index 0000000..64b4ed3 --- /dev/null +++ b/mingling_macros/tmpls/comps/fish.fish @@ -0,0 +1,120 @@ +#!/usr/bin/env fish +function __<<<bin_name>>>_fish_complete + set -l cmdline (commandline -opc) + set -l buffer (commandline -b) + set -l cursor (commandline -C) + set -l current_token (commandline -ct) + + set -l current_word "" + set -l previous_word "" + set -l word_index 0 + set -l char_count 0 + + set -l found false + if test -n "$current_token" + for i in (seq (count $cmdline)) + if test "$cmdline[$i]" = "$current_token" + set word_index $i + set current_word $current_token + if test $i -gt 1 + set previous_word $cmdline[(math $i - 1)] + end + set found true + break + end + end + end + + if not $found + for i in (seq (count $cmdline)) + set word $cmdline[$i] + if test $i -gt 1 + set char_count (math $char_count + 1) + end + set char_count (math $char_count + (string length -- "$word")) + + if test $cursor -le $char_count + set word_index $i + set current_word $word + if test $i -gt 1 + set previous_word $cmdline[(math $i - 1)] + end + break + end + end + end + + if test $word_index -eq 0 -a (count $cmdline) -gt 0 + set word_index (count $cmdline) + if test -n "$current_token" -a "$current_token" != "$cmdline[-1]" + set current_word $current_token + else + set current_word "" + end + set previous_word $cmdline[-1] + end + + if test $word_index -gt (count $cmdline) + set word_index (count $cmdline) + end + + set -l buffer_replaced (string replace -a "-" "^" -- "$buffer") + set -l current_word_replaced (string replace -a "-" "^" -- "$current_word") + set -l previous_word_replaced (string replace -a "-" "^" -- "$previous_word") + + set -l args + set -a args -f "$buffer_replaced" -C "$cursor" -w "$current_word_replaced" -p "$previous_word_replaced" + + if test (count $cmdline) -gt 0 + set -a args -c "$cmdline[1]" + else + set -a args -c "" + end + + set -a args -i "$word_index" + + if test (count $cmdline) -gt 0 + set -l all_words_replaced + for word in $cmdline + set -a all_words_replaced (string replace -a "-" "^" -- "$word") + end + + if test -n "$current_token" -a "$current_word" = "$current_token" + set -l found_in_cmdline false + for word in $cmdline + if test "$word" = "$current_token" + set found_in_cmdline true + break + end + end + if not $found_in_cmdline -a $word_index -eq (math (count $cmdline) + 1) + set -a all_words_replaced (string replace -a "-" "^" -- "$current_token") + end + end + + set -a args -a $all_words_replaced + else + set -a args -a "" + end + + set -a args -F "fish" + + set -l output + if not <<<bin_name>>> __comp $args 2>/dev/null | read -z output + return + end + + set -l trimmed_output (string trim -- "$output") + if test "$trimmed_output" = "_file_" + __fish_complete_path "$current_word" + return 0 + else if test -n "$trimmed_output" + string split -n \n -- "$output" | while read -l line + test -n "$line" && echo "$line" + end + return 0 + end + return 1 +end + +complete -c <<<bin_name>>> -a '(__<<<bin_name>>>_fish_complete)' -f diff --git a/mingling_macros/tmpls/comps/pwsh.ps1 b/mingling_macros/tmpls/comps/pwsh.ps1 new file mode 100644 index 0000000..d72a027 --- /dev/null +++ b/mingling_macros/tmpls/comps/pwsh.ps1 @@ -0,0 +1,136 @@ +# PowerShell completion script for <<<bin_name>>> +Register-ArgumentCompleter -Native -CommandName '<<<bin_name>>>' -ScriptBlock { + param($wordToComplete, $commandAst, $cursorPosition) + + $line = $commandAst.ToString() + + $elements = @() + if ($commandAst.CommandElements.Count -gt 0) { + $elements = $commandAst.CommandElements | ForEach-Object { $_.Value } + } + + $commandName = if ($elements.Count -gt 0) { $elements[0] } else { "" } + + $currentWord = $wordToComplete + $previousWord = "" + $wordIndex = 0 + + $found = $false + for ($i = 0; $i -lt $elements.Count; $i++) { + if ($elements[$i] -eq $currentWord) { + $wordIndex = $i + 1 + if ($i -gt 0) { + $previousWord = $elements[$i - 1] + } + $found = $true + break + } + } + + if (-not $found) { + $wordIndex = $elements.Count + 1 + if ($elements.Count -gt 0) { + $previousWord = $elements[-1] + } + } + + $args = @( + "-C", $cursorPosition.ToString() + "-i", $wordIndex.ToString() + "-F", "Powershell" + ) + + if ($line) { + $args += "-f" + $args += ($line -replace '-', '^') + } + if ($currentWord) { + $args += "-w" + $args += ($currentWord -replace '-', '^') + } + if ($previousWord) { + $args += "-p" + $args += ($previousWord -replace '-', '^') + } + if ($commandName) { + $args += "-c" + $args += ($commandName -replace '-', '^') + } + + foreach ($element in $elements) { + if ($element) { + $args += "-a" + $args += ($element -replace '-', '^') + } + } + + $originalEncoding = [Console]::OutputEncoding + $originalPSEncoding = $OutputEncoding + [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 + $OutputEncoding = [System.Text.Encoding]::UTF8 + + $output = & <<<bin_name>>> __comp $args 2>&1 + + [Console]::OutputEncoding = $originalEncoding + $OutputEncoding = $originalPSEncoding + + $output = $output -replace "`r`n", "`n" -replace "`r", "`n" + + if (-not $output) { + return @() + } + + $lines = $output -split "`n" + + if ($lines.Count -eq 0) { + return @() + } + + $firstLine = $lines[0].Trim() + + if ($firstLine -eq "_file_") { + if ($lines.Count -gt 1) { + $fileSuggestions = $lines[1..($lines.Count-1)] + } else { + $fileSuggestions = @() + } + + $completionResults = @() + $fileSuggestions | ForEach-Object { + $path = $_ + $isDirectory = $path.EndsWith([System.IO.Path]::DirectorySeparatorChar) -or $path.EndsWith('/') + $completionType = if ($isDirectory) { 'ProviderContainer' } else { 'ProviderItem' } + $completionResults += [System.Management.Automation.CompletionResult]::new($path, $path, $completionType, $path) + } + + return $completionResults + } else { + $completionResults = @() + + foreach ($line in $lines) { + $trimmedLine = $line.Trim() + + if ($trimmedLine -match '^([^$]+)\$\((.+)\)$') { + $text = $matches[1] + $description = $matches[2] + $completionResults += [System.Management.Automation.CompletionResult]::new( + $text, + $text, + 'ParameterValue', + $description + ) + } else { + $text = $trimmedLine + $resultType = if ($text.StartsWith('-')) { 'ParameterName' } else { 'ParameterValue' } + $completionResults += [System.Management.Automation.CompletionResult]::new( + $text, + $text, + $resultType, + $text + ) + } + } + + return $completionResults + } +} diff --git a/mingling_macros/tmpls/comps/zsh.zsh b/mingling_macros/tmpls/comps/zsh.zsh new file mode 100644 index 0000000..7cf5f7b --- /dev/null +++ b/mingling_macros/tmpls/comps/zsh.zsh @@ -0,0 +1,71 @@ +#!/usr/bin/env zsh +_<<<bin_name>>>_completion() { + local -a args + local suggestions + + local buffer="$BUFFER" + local cursor="$CURSOR" + local current_word="${words[$CURRENT]}" + local previous_word="" + local command_name="${words[1]}" + local word_index="$CURRENT" + + if [[ $CURRENT -gt 1 ]]; then + previous_word="${words[$((CURRENT-1))]}" + fi + + args=( + -f "${buffer//-/^}" + -C "$cursor" + -w "${current_word//-/^}" + -p "${previous_word//-/^}" + -c "$command_name" + -i "$word_index" + -a "${(@)words//-/^}" + -F "zsh" + ) + + suggestions=$(<<<bin_name>>> __comp "${args[@]}" 2>/dev/null) + + if [[ $? -eq 0 ]] && [[ -n "$suggestions" ]]; then + local -a completions + completions=(${(f)suggestions}) + + if [[ "${completions[1]}" == "_file_" ]]; then + shift completions + _files + else + local -a parsed_completions + for item in "${completions[@]}"; do + if [[ "$item" =~ '^([^$]+)\$\((.+)\)$' ]]; then + parsed_completions+=("${match[1]//:/\\:}:${match[2]}") + else + parsed_completions+=("${item//:/\\:}") + fi + done + + if (( $+functions[_describe] )); then + _describe '<<<bin_name>>> commands' parsed_completions + else + local -a simple_completions + for item in "${completions[@]}"; do + if [[ "$item" =~ '^([^$]+)\$\((.+)\)$' ]]; then + simple_completions+=("${match[1]}") + else + simple_completions+=("$item") + fi + done + compadd -a simple_completions + fi + fi + fi +} + +if (( $+functions[compdef] )); then + compdef _<<<bin_name>>>_completion <<<bin_name>>> + if [[ $? -ne 0 ]]; then + compctl -K _<<<bin_name>>>_completion <<<bin_name>>> + fi +else + compctl -K _<<<bin_name>>>_completion <<<bin_name>>> +fi |
