Skip to content

Commit

Permalink
merged fish/zsh completion into single project
Browse files Browse the repository at this point in the history
- temporary removal of tests/readme
  • Loading branch information
rsteube committed Mar 17, 2020
1 parent 158adff commit 378fc35
Show file tree
Hide file tree
Showing 20 changed files with 685 additions and 790 deletions.
132 changes: 0 additions & 132 deletions README.md

This file was deleted.

113 changes: 62 additions & 51 deletions action.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package zsh
package carapace

import (
"fmt"
"strings"
"github.com/rsteube/carapace/fish"
"github.com/rsteube/carapace/zsh"
)

// Action indicates how to complete the corresponding argument
// https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org
// http://zsh.sourceforge.net/Doc/Release/Completion-System.html
type Action struct {
Value string
Fish string
Zsh string
Callback CompletionCallback
}
type ActionMap map[string]Action
Expand All @@ -18,7 +15,9 @@ type CompletionCallback func(args []string) Action
// finalize replaces value if a callback function is set
func (a Action) finalize(uid string) Action {
if a.Callback != nil {
a.Value = ActionExecute(fmt.Sprintf(`${os_args[1]} _zsh_completion '%v' ${${os_args:1:gs/\"/\\\"}:gs/\'/\\\"}`, uid)).Value
// TODO only set to callback if no value is set (one shell might not need the callback)
a.Fish = fish.Callback(uid)
a.Zsh = zsh.Callback(uid)
}
return a
}
Expand All @@ -30,91 +29,103 @@ func ActionCallback(callback CompletionCallback) Action {

// ActionExecute uses command substitution to invoke a command and evalues it's result as Action
func ActionExecute(command string) Action {
return Action{Value: fmt.Sprintf(` eval \$(%v)`, command)} // {EVAL-STRING} action did not handle space escaping ('\ ') as expected (separate arguments), this one works
return Action{
Fish: fish.ActionExecute(command),
Zsh: zsh.ActionExecute(command),
}
}

// ActionBool completes true/false
func ActionBool() Action {
return ActionValues("true", "false")
return Action{
Fish: fish.ActionBool(),
Zsh: zsh.ActionBool(),
}
}

// ActionPathFiles completes filepaths
// [http://zsh.sourceforge.net/Doc/Release/Completion-System.html#index-_005fpath_005ffiles]
func ActionPathFiles(pattern string) Action { // TODO support additional options
if pattern == "" {
return Action{Value: "_path_files"}
} else {
return Action{Value: fmt.Sprintf("_path_files -g '%v'", pattern)}
}
func ActionPathFiles(suffix string) Action {
return Action{
Fish: fish.ActionPathFiles(suffix),
Zsh: zsh.ActionPathFiles(suffix),
}
}

// ActionFiles _path_files with all options except -g and -/. These options depend on file-patterns style setting. // TODO fix doc
// [http://zsh.sourceforge.net/Doc/Release/Completion-System.html#index-_005ffiles]
func ActionFiles(pattern string) Action {
if pattern == "" {
return Action{Value: "_files"}
} else {
return Action{Value: fmt.Sprintf("_files -g '%v'", pattern)}
}
func ActionFiles(suffix string) Action {
return Action{
Fish: fish.ActionFiles(suffix),
Zsh: zsh.ActionFiles(suffix),
}
}

// ActionNetInterfaces completes network interface names
func ActionNetInterfaces() Action {
return Action{Value: "_net_interfaces"}
return Action{
Fish: fish.ActionNetInterfaces(),
Zsh: zsh.ActionNetInterfaces(),
}
}

// ActionUsers completes user names
func ActionUsers() Action {
return Action{Value: "_users"}
return Action{
Fish: fish.ActionUsers(),
Zsh: zsh.ActionUsers(),
}
}

// ActionGroups completes group names
func ActionGroups() Action {
return Action{Value: "_groups"}
return Action{
Fish: fish.ActionGroups(),
Zsh: zsh.ActionGroups(),
}
}

// ActionHosts completes host names
func ActionHosts() Action {
return Action{Value: "_hosts"}
return Action{
Fish: fish.ActionHosts(),
Zsh: zsh.ActionHosts(),
}
}

// ActionOptions completes the names of shell options
func ActionOptions() Action {
return Action{Value: "_options"}
return Action{
Fish: fish.ActionOptions(),
Zsh: zsh.ActionOptions(),
}
}

// ActionValues completes arbitrary keywords (values)
func ActionValues(values ...string) Action {
if len(strings.TrimSpace(strings.Join(values, ""))) == 0 {
return ActionMessage("no values to complete")
}

vals := make([]string, len(values))
for index, val := range values {
// TODO escape special characters
vals[index] = strings.Replace(val, " ", `\ `, -1)
}
return Action{Value: fmt.Sprintf(`_values '' %v`, strings.Join(vals, " "))}
return Action{
Fish: fish.ActionValues(values...),
Zsh: zsh.ActionValues(values...),
}
}

// ActionValuesDescribed completes arbitrary key (values) with an additional description (value, description pairs)
func ActionValuesDescribed(values ...string) Action {
// TODO verify length (description always exists)
vals := make([]string, len(values))
for index, val := range values {
if index%2 == 0 {
vals[index/2] = fmt.Sprintf("'%v[%v]'", val, values[index+1])
}
}
return ActionValues(vals...)
return Action{
Fish: fish.ActionValuesDescribed(values...),
Zsh: zsh.ActionValuesDescribed(values...),
}
}

// ActionMessage displays a help messages in places where no completions can be generated
func ActionMessage(msg string) Action {
return Action{Value: fmt.Sprintf(" _message -r '%v'", msg)} // space before _message is necessary
return Action{
Fish: fish.ActionMessage(msg),
Zsh: zsh.ActionMessage(msg),
}
}

// ActionMultiParts completes multiple parts of words separately where each part is separated by some char
func ActionMultiParts(separator rune, values ...string) Action {
return Action{Value: fmt.Sprintf("_multi_parts %v '(%v)'", string(separator), strings.Join(values, " "))}
return Action{
Fish: fish.ActionMultiParts(separator, values...),
Zsh: zsh.ActionMultiParts(separator, values...),
}
}
69 changes: 0 additions & 69 deletions action_test.go

This file was deleted.

Loading

0 comments on commit 378fc35

Please sign in to comment.