Skip to content

Commit

Permalink
Merge pull request #9 from xb-bx/main
Browse files Browse the repository at this point in the history
More completion scripts
  • Loading branch information
thinkbeforecoding authored Feb 9, 2024
2 parents be8683f + 1ca4b8a commit 3e61bab
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ Usage: --text <text>
Arguments:
--text, -t <text> The text to display
```
## Tab Completion
### Powershell

To enable completion in powershell, execute the following line:
```
Expand All @@ -92,6 +94,14 @@ hello completion powershell | out-string | invoke-expression
This line can be added to the $Profile.CurrentUserAllHost file to enable completion for every new session.

Now, type `hello` followed by a space, and press tab or Ctrl+Space. The arguments are suggested.
### Bash
```bash
hello completion bash >> ~/.bashrc
```
### Fish
```fish
hello completion fish > ~/.config/fish/completions/hello.fish
```

## Arg<'t>

Expand Down
24 changes: 22 additions & 2 deletions src/Fargo/Fargo.fs
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ module Run =
printDescription usages
printOptions usages.Options

type Shell = Powershell
type Shell = Powershell | Fish | Bash

let printCompletion appName shell =
match shell with
Expand All @@ -671,6 +671,26 @@ Register-ArgumentCompleter -Native -CommandName %s -ScriptBlock {
}
}
""" appName appName
| Fish ->
printfn """
function __%s_completion
set -l count (commandline -pC)
set -l cmd (commandline -opc)
%s complete --position (math $count - (string length $cmd[1])) - 1 "$cmd[2..]"
end
complete -f -c %s -a '(__%s_completion)'
""" appName appName appName appName
| Bash ->
printfn """
__%s_completion()
{
local words=${COMP_WORDS[@]:1}
local first_len=${#COMP_WORDS[0]}
local point=$(( $COMP_POINT - $first_len - 1 ))
COMPREPLY=( $(compgen -W "$(%s complete --position $point ${words[*]})") )
}
complete -F __%s_completion %s
""" appName appName appName appName


type TopCmd = CompleteCmd | CompletionCmd | RunCmd
Expand All @@ -692,7 +712,7 @@ Register-ArgumentCompleter -Native -CommandName %s -ScriptBlock {
|> defaultValue Int32.MaxValue

let pShell =
let shells = Map.ofList [ "powershell", Powershell ]
let shells = Map.ofList [ "powershell", Powershell; "fish", Fish; "bash", Bash ]
argc "shell" "the shell for which to emit the script" (Completer.choices (shells |> Map.toList |> List.map fst) )
|> reqArg
|> parse (fun shell ->
Expand Down
2 changes: 1 addition & 1 deletion src/Fargo/Fargo.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ module Run =
val printOptions: Usage list -> unit
val printHelp: Usages -> unit

type Shell = Powershell
type Shell = Powershell | Fish | Bash

val printCompletion: appName:string -> shell:Shell -> unit

Expand Down

0 comments on commit 3e61bab

Please sign in to comment.