Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Dec 17, 2023
1 parent 2785ea0 commit 893f26f
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 94 deletions.
34 changes: 34 additions & 0 deletions cmd/carapace/cmd/invoke.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cmd

import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/rsteube/carapace"
Expand Down Expand Up @@ -78,3 +81,34 @@ func init() {
carapace.Gen(invokeCmd).Standalone()

}

func invokeCompleter(completer string) string {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()

os.Args[1] = "_carapace"
executeCompleter(completer)

w.Close()
out := <-outC
os.Stdout = old

executable, err := os.Executable()
if err != nil {
panic(err.Error()) // TODO exit with error message
}
executableName := filepath.Base(executable)
patched := strings.Replace(string(out), fmt.Sprintf("%v _carapace", executableName), fmt.Sprintf("%v %v", executableName, completer), -1) // general callback
patched = strings.Replace(patched, fmt.Sprintf("'%v', '_carapace'", executableName), fmt.Sprintf("'%v', '%v'", executableName, completer), -1) // xonsh callback
return patched

}
26 changes: 26 additions & 0 deletions cmd/carapace/cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"encoding/json"
"fmt"
"strconv"

Expand Down Expand Up @@ -37,3 +38,28 @@ func printCompleters() {
fmt.Printf("%-"+strconv.Itoa(maxlen)+"v %v\n", name, completers.Description(name))
}
}

func printCompletersJson() {
// TODO move to completers package
type _completer struct {
Name string
Description string
Spec string `json:",omitempty"`
Overlay string `json:",omitempty"`
}

_completers := make([]_completer, 0)
for _, name := range completers.Names() {
specPath, _ := completers.SpecPath(name) // TODO handle error (log?)
overlayPath, _ := completers.OverlayPath(name) // TODO handle error (log?)
_completers = append(_completers, _completer{
Name: name,
Description: completers.Description(name),
Spec: specPath,
Overlay: overlayPath,
})
}
if m, err := json.Marshal(_completers); err == nil { // TODO handle error (log?)
fmt.Println(string(m))
}
}
36 changes: 36 additions & 0 deletions cmd/carapace/cmd/macros.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package cmd

import (
"fmt"
"path/filepath"
"sort"
"strconv"
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -41,3 +46,34 @@ func init() {
}),
)
}

func printMacros() {
maxlen := 0
names := make([]string, 0)
for name := range actions.MacroMap {
names = append(names, name)
if len := len(name); len > maxlen {
maxlen = len
}
}

sort.Strings(names)
for _, name := range names {
fmt.Printf("%-"+strconv.Itoa(maxlen)+"v %v\n", name, actions.MacroDescriptions[name])
}
}

func printMacro(name string) {
if m, ok := actions.MacroMap[name]; ok {
path := strings.Replace(name, ".", "/", -1)
signature := ""
if s := m.Signature(); s != "" {
signature = fmt.Sprintf("(%v)", s)
}

fmt.Printf(`signature: $_%v%v
description: %v
reference: https://pkg.go.dev/github.com/rsteube/carapace-bin/pkg/actions/%v#Action%v
`, name, signature, actions.MacroDescriptions[name], filepath.Dir(path), filepath.Base(path))
}
}
94 changes: 0 additions & 94 deletions cmd/carapace/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@ package cmd
//go:generate go run ../../carapace-generate/gen.go

import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"

"github.com/rsteube/carapace"
Expand Down Expand Up @@ -135,93 +128,6 @@ func overlayCompletion(overlayPath string, args ...string) carapace.Action {
})
}

func printCompletersJson() {
// TODO move to completers package
type _completer struct {
Name string
Description string
Spec string `json:",omitempty"`
Overlay string `json:",omitempty"`
}

_completers := make([]_completer, 0)
for _, name := range completers.Names() {
specPath, _ := completers.SpecPath(name) // TODO handle error (log?)
overlayPath, _ := completers.OverlayPath(name) // TODO handle error (log?)
_completers = append(_completers, _completer{
Name: name,
Description: completers.Description(name),
Spec: specPath,
Overlay: overlayPath,
})
}
if m, err := json.Marshal(_completers); err == nil { // TODO handle error (log?)
fmt.Println(string(m))
}
}

func printMacros() {
maxlen := 0
names := make([]string, 0)
for name := range actions.MacroMap {
names = append(names, name)
if len := len(name); len > maxlen {
maxlen = len
}
}

sort.Strings(names)
for _, name := range names {
fmt.Printf("%-"+strconv.Itoa(maxlen)+"v %v\n", name, actions.MacroDescriptions[name])
}
}

func printMacro(name string) {
if m, ok := actions.MacroMap[name]; ok {
path := strings.Replace(name, ".", "/", -1)
signature := ""
if s := m.Signature(); s != "" {
signature = fmt.Sprintf("(%v)", s)
}

fmt.Printf(`signature: $_%v%v
description: %v
reference: https://pkg.go.dev/github.com/rsteube/carapace-bin/pkg/actions/%v#Action%v
`, name, signature, actions.MacroDescriptions[name], filepath.Dir(path), filepath.Base(path))
}
}

func invokeCompleter(completer string) string {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()

os.Args[1] = "_carapace"
executeCompleter(completer)

w.Close()
out := <-outC
os.Stdout = old

executable, err := os.Executable()
if err != nil {
panic(err.Error()) // TODO exit with error message
}
executableName := filepath.Base(executable)
patched := strings.Replace(string(out), fmt.Sprintf("%v _carapace", executableName), fmt.Sprintf("%v %v", executableName, completer), -1) // general callback
patched = strings.Replace(patched, fmt.Sprintf("'%v', '_carapace'", executableName), fmt.Sprintf("'%v', '%v'", executableName, completer), -1) // xonsh callback
return patched

}

func updateSchema() error {
confDir, err := xdg.UserConfigDir()
if err != nil {
Expand Down

0 comments on commit 893f26f

Please sign in to comment.