Skip to content

Commit

Permalink
Merge pull request #87 from rsteube/xonsh
Browse files Browse the repository at this point in the history
added xonsh
  • Loading branch information
rsteube authored Sep 19, 2020
2 parents aea9c71 + 0f37d43 commit 3242959
Show file tree
Hide file tree
Showing 9 changed files with 633 additions and 6 deletions.
19 changes: 19 additions & 0 deletions Dockerfile.xonsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM golang

RUN apt-get update \
&& apt-get install -y python3-pip

RUN pip3 install --no-cache-dir --disable-pip-version-check xonsh \
&& ln -s $(which xonsh) /usr/bin/xonsh


RUN mkdir -p ~/.config/xonsh \
&& echo "\n\
\$PROMPT='carapace '\n\
exec(\$(example _carapace xonsh))" \
> ~/.config/xonsh/rc.xsh

RUN ln -s /carapace/example/example /usr/local/bin/example

ENTRYPOINT [ "xonsh" ]

19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ Completion script generator for [cobra] with support for:
- [Bash](https://www.gnu.org/software/bash/manual/html_node/A-Programmable-Completion-Example.html)
- [Elvish](https://elv.sh/ref/edit.html#editcompletionarg-completer)
- [Fish](https://fishshell.com/docs/current/#writing-your-own-completions)
- [Oil](http://www.oilshell.org/blog/2018/10/10.html) *broken* ([#86](https://github.com/rsteube/carapace/issues/86))
- [Powershell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/register-argumentcompleter)
- [Xonsh](https://xon.sh/tutorial_completers.html#writing-a-new-completer) *experimental*
- [Zsh](https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org)


Expand Down Expand Up @@ -121,6 +123,12 @@ Since callbacks are simply invocations of the program they can be tested directl
#[CompletionResult]::new('ERR', 'ERR', [CompletionResultType]::ParameterValue, ' ')
#[CompletionResult]::new('flag --required must be set to valid: invalid', 'flag --required must be set to valid: invalid', [CompletionResultType]::ParameterValue, ' ')

./example _carapace xonsh '_example__condition#1' example condition --required invalid
#{
# RichCompletion('_', display='_', description='flag --required must be set to valid: invalid', prefix_len=0),
# RichCompletion('ERR', display='ERR', description='flag --required must be set to valid: invalid', prefix_len=0),
#}

./example _carapace zsh '_example__condition#1' example condition --required invalid
# _message -r 'flag --required must be set to valid: invalid'
```
Expand Down Expand Up @@ -159,6 +167,7 @@ Additional information can be found at:
- Elvish: [using-and-writing-completions-in-elvish](https://zzamboni.org/post/using-and-writing-completions-in-elvish/) and [argument-completer](https://elv.sh/ref/edit.html#argument-completer)
- Fish: [fish-shell/share/functions](https://github.com/fish-shell/fish-shell/tree/master/share/functions) and [writing your own completions](https://fishshell.com/docs/current/#writing-your-own-completions)
- Powershell: [Dynamic Tab Completion](https://adamtheautomator.com/powershell-parameters-argumentcompleter/) and [Register-ArgumentCompleter](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/register-argumentcompleter)
- Xonsh: [Programmable Tab-Completion](https://xon.sh/tutorial_completers.html) and [RichCompletion(str)](https://github.com/xonsh/xonsh/blob/master/xonsh/completers/tools.py)
- Zsh: [zsh-completions-howto](https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org#functions-for-performing-complex-completions-of-single-words) and [Completion-System](http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-System).

## Standalone Mode
Expand Down Expand Up @@ -188,11 +197,19 @@ example _carapace elvish > example.elv
set PATH $PATH (pwd)
example _carapace fish | source

# oil
PATH=$PATH:$(pwd)
source <(example _carapace oil)

# powershell
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
$env:PATH += ":$pwd"
example _carapace powershell | out-string | Invoke-Expression

# xonsh
$PATH.append($(pwd))
exec($(example _carapace xonsh))

# zsh
PATH=$PATH:$(pwd)
source <(example _carapace zsh)
Expand All @@ -203,7 +220,7 @@ example <TAB>
or use [docker-compose](https://docs.docker.com/compose/):
```sh
docker-compose run --rm build
docker-compose run --rm [bash|elvish|fish|powershell|zsh]
docker-compose run --rm [bash|elvish|fish|oil|powershell|xonsh|zsh]

example <TAB>
```
Expand Down
17 changes: 16 additions & 1 deletion action.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/rsteube/carapace/elvish"
"github.com/rsteube/carapace/fish"
"github.com/rsteube/carapace/powershell"
"github.com/rsteube/carapace/xonsh"
"github.com/rsteube/carapace/zsh"
"github.com/spf13/cobra"
)
Expand All @@ -18,8 +19,9 @@ type Action struct {
Bash string
Elvish string
Fish string
Zsh string
Powershell string
Xonsh string
Zsh string
Callback CompletionCallback
}
type ActionMap map[string]Action
Expand All @@ -40,6 +42,9 @@ func (a Action) finalize(cmd *cobra.Command, uid string) Action {
if a.Powershell == "" {
a.Powershell = powershell.Callback(cmd.Root().Name(), uid)
}
if a.Xonsh == "" {
a.Xonsh = xonsh.Callback(cmd.Root().Name(), uid)
}
if a.Zsh == "" {
a.Zsh = zsh.Callback(uid)
}
Expand All @@ -57,6 +62,8 @@ func (a Action) Value(shell string) string {
return a.Elvish
case "powershell":
return a.Powershell
case "xonsh":
return a.Xonsh
case "zsh":
return a.Zsh
default:
Expand Down Expand Up @@ -92,6 +99,7 @@ func ActionExecute(command string) Action {
Elvish: elvish.ActionExecute(command),
Fish: fish.ActionExecute(command),
Powershell: powershell.ActionExecute(command),
Xonsh: xonsh.ActionExecute(command),
Zsh: zsh.ActionExecute(command),
}
}
Expand All @@ -107,6 +115,7 @@ func ActionDirectories() Action {
Elvish: elvish.ActionDirectories(),
Fish: fish.ActionDirectories(),
Powershell: powershell.ActionDirectories(),
Xonsh: xonsh.ActionDirectories(),
Zsh: zsh.ActionDirectories(),
}
}
Expand All @@ -117,6 +126,7 @@ func ActionFiles(suffix string) Action {
Elvish: elvish.ActionFiles(suffix),
Fish: fish.ActionFiles(suffix),
Powershell: powershell.ActionFiles(suffix),
Xonsh: xonsh.ActionFiles(suffix),
Zsh: zsh.ActionFiles("*" + suffix),
}
}
Expand All @@ -128,6 +138,7 @@ func ActionNetInterfaces() Action {
Elvish: elvish.ActionNetInterfaces(),
Fish: fish.ActionNetInterfaces(),
Powershell: powershell.ActionNetInterfaces(),
Xonsh: xonsh.ActionNetInterfaces(),
Zsh: zsh.ActionNetInterfaces(),
}
}
Expand Down Expand Up @@ -235,6 +246,7 @@ func ActionValues(values ...string) Action {
Elvish: elvish.ActionValues(values...),
Fish: fish.ActionValues(values...),
Powershell: powershell.ActionValues(values...),
Xonsh: xonsh.ActionValues(values...),
Zsh: zsh.ActionValues(values...),
}
}
Expand All @@ -246,6 +258,7 @@ func ActionValuesDescribed(values ...string) Action {
Elvish: elvish.ActionValuesDescribed(values...),
Fish: fish.ActionValuesDescribed(values...),
Powershell: powershell.ActionValuesDescribed(values...),
Xonsh: xonsh.ActionValuesDescribed(values...),
Zsh: zsh.ActionValuesDescribed(values...),
}
}
Expand All @@ -257,6 +270,7 @@ func ActionMessage(msg string) Action {
Elvish: elvish.ActionMessage(msg),
Fish: fish.ActionMessage(msg),
Powershell: powershell.ActionMessage(msg),
Xonsh: xonsh.ActionMessage(msg),
Zsh: zsh.ActionMessage(msg),
}
}
Expand All @@ -267,6 +281,7 @@ func ActionPrefixValues(prefix string, values ...string) Action {
Elvish: elvish.ActionPrefixValues(prefix, values...),
Fish: fish.ActionPrefixValues(prefix, values...),
Powershell: powershell.ActionPrefixValues(prefix, values...),
Xonsh: xonsh.ActionPrefixValues(prefix, values...),
Zsh: zsh.ActionPrefixValues(prefix, values...),
})
}
Expand Down
15 changes: 12 additions & 3 deletions carapace.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/rsteube/carapace/fish"
"github.com/rsteube/carapace/powershell"
"github.com/rsteube/carapace/uid"
"github.com/rsteube/carapace/xonsh"
"github.com/rsteube/carapace/zsh"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -79,6 +80,10 @@ func (c Carapace) Zsh() string {
return c.Snippet("zsh")
}

func (c Carapace) Xonsh() string {
return c.Snippet("xonsh")
}

func (c Carapace) Standalone() {
// TODO probably needs to be done for each subcommand
if c.cmd.Root().Flag("help") != nil {
Expand All @@ -101,14 +106,16 @@ func (c Carapace) Snippet(shell string) string {
snippet = elvish.Snippet
case "fish":
snippet = fish.Snippet
case "osh":
case "oil":
snippet = bash.Snippet
case "powershell":
snippet = powershell.Snippet
case "xonsh":
snippet = xonsh.Snippet
case "zsh":
snippet = zsh.Snippet
default:
return fmt.Sprintf("expected 'bash', 'elvish', 'fish', 'osh', 'powershell' or 'zsh' [was: %v]", shell)
return fmt.Sprintf("expected 'bash', 'elvish', 'fish', 'oil', 'powershell', 'xonsh' or 'zsh' [was: %v]", shell)
}
return snippet(c.cmd.Root(), completions.actions.Shell(shell))
}
Expand Down Expand Up @@ -226,9 +233,11 @@ func determineShell() string {
case "fish":
return "fish"
case "osh":
return "osh"
return "oil"
case "pwsh":
return "powershell"
case "xonsh":
return "xonsh"
case "zsh":
return "zsh"
default:
Expand Down
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ services:
dockerfile: Dockerfile.powershell
volumes:
- '.:/carapace/'

xonsh:
build:
context: .
dockerfile: Dockerfile.xonsh
volumes:
- '.:/carapace/'

zsh:
build:
Expand Down
2 changes: 1 addition & 1 deletion example/cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func init() {
"users": carapace.ActionUsers(),
"values": carapace.ActionValues("values", "example"),
"values_described": carapace.ActionValuesDescribed("values", "valueDescription", "example", "exampleDescription"),
"custom": carapace.Action{Zsh: "_most_recent_file 2"},
"custom": carapace.Action{Zsh: "_most_recent_file 2", Xonsh: "{}"},
"kill": carapace.ActionKillSignals(),
"optarg": carapace.ActionValues("blue", "red", "green", "yellow"),
})
Expand Down
Loading

0 comments on commit 3242959

Please sign in to comment.