mirror of
https://github.com/wavetermdev/inshellisense.git
synced 2026-08-05 13:43:30 -07:00
feat: update cli & support adding bindings
Signed-off-by: cpendery <cpendery@vt.edu>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package autocomplete
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"log/slog"
|
||||
|
||||
"github.com/adrg/xdg"
|
||||
)
|
||||
|
||||
const (
|
||||
cacheFilePath = "clac/clac.cache"
|
||||
)
|
||||
|
||||
func CacheResult(result string) {
|
||||
path, err := xdg.CacheFile(cacheFilePath)
|
||||
if err != nil {
|
||||
slog.Error("unable to create cache file", slog.String("error", err.Error()))
|
||||
return
|
||||
}
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
slog.Error("unable to open cache file", slog.String("error", err.Error()))
|
||||
return
|
||||
}
|
||||
if _, err := f.WriteString(result); err != nil {
|
||||
slog.Error("unable to write to cache file", slog.String("error", err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
func ReadResult() string {
|
||||
path, err := xdg.CacheFile(cacheFilePath)
|
||||
if err != nil {
|
||||
slog.Error("unable to create cache file", slog.String("error", err.Error()))
|
||||
return ""
|
||||
}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
slog.Error("unable to open cache file", slog.String("error", err.Error()))
|
||||
return ""
|
||||
}
|
||||
content, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
slog.Error("unable to read from cache file", slog.String("error", err.Error()))
|
||||
return ""
|
||||
}
|
||||
return string(content)
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/cpendery/clac/shell"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(bindCmd)
|
||||
}
|
||||
|
||||
var bindCmd = &cobra.Command{
|
||||
Use: "bind [shell]",
|
||||
Short: "adds keybindings to the selected shell",
|
||||
Args: func(_ *cobra.Command, args []string) error {
|
||||
if len(args) < 1 {
|
||||
return fmt.Errorf("you must select one of the supported shells: [%s]", strings.Join(shell.SupportedShells, ","))
|
||||
} else if len(args) > 1 {
|
||||
return fmt.Errorf("you can only bind to 1 shell at a time")
|
||||
}
|
||||
userSelectedShell := strings.TrimSpace(args[0])
|
||||
if !slices.Contains(shell.SupportedShells, userSelectedShell) {
|
||||
return fmt.Errorf("%s is not one of the supported shells: [%s]", userSelectedShell, strings.Join(shell.SupportedShells, ","))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
RunE: runBindCmd,
|
||||
}
|
||||
|
||||
func runBindCmd(_ *cobra.Command, args []string) error {
|
||||
userSelectedShell := args[0]
|
||||
switch userSelectedShell {
|
||||
case "bash":
|
||||
return shell.CreateBashBinding()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/cpendery/clac/autocomplete"
|
||||
"github.com/cpendery/clac/ui"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
rootCmd = &cobra.Command{
|
||||
Use: `clac <command>`,
|
||||
Short: "command line autocomplete",
|
||||
Long: `clac - command line autocomplete
|
||||
|
||||
clac provides inshellisense for the command line to make developers
|
||||
more productive
|
||||
|
||||
complete documentation is available at https://github.com/cpendery/clac`,
|
||||
SilenceUsage: true,
|
||||
RunE: rootExec,
|
||||
}
|
||||
outputCmd bool
|
||||
enableLogging bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.Flags().BoolVarP(&outputCmd, "output-cmd", "o", false, "outputs the last cmd from a clac execution")
|
||||
rootCmd.Flags().BoolVarP(&outputCmd, "logging", "l", false, "enable logging to the `clac.log` file")
|
||||
}
|
||||
|
||||
func rootExec(cmd *cobra.Command, args []string) error {
|
||||
if outputCmd {
|
||||
result := autocomplete.ReadResult()
|
||||
_, err := os.Stdout.WriteString(result)
|
||||
return err
|
||||
}
|
||||
if enableLogging {
|
||||
f, err := tea.LogToFile("clac.log", "debug")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
}
|
||||
|
||||
startingInput := ""
|
||||
if len(args) >= 1 {
|
||||
startingInput = args[0]
|
||||
}
|
||||
p := tea.NewProgram(ui.Start(startingInput))
|
||||
if _, err := p.Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -3,14 +3,17 @@ module github.com/cpendery/clac
|
||||
go 1.21.0
|
||||
|
||||
require (
|
||||
github.com/adrg/xdg v0.4.0
|
||||
github.com/charmbracelet/bubbles v0.16.1
|
||||
github.com/charmbracelet/bubbletea v0.24.2
|
||||
github.com/charmbracelet/lipgloss v0.8.0
|
||||
github.com/google/uuid v1.3.1
|
||||
github.com/lithammer/fuzzysearch v1.1.8
|
||||
github.com/mattn/go-runewidth v0.0.14
|
||||
github.com/muesli/reflow v0.3.0
|
||||
github.com/spf13/cobra v1.7.0
|
||||
github.com/stretchr/testify v1.8.4
|
||||
golang.org/x/term v0.6.0
|
||||
golang.org/x/term v0.12.0
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -18,6 +21,7 @@ require (
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.18 // indirect
|
||||
github.com/mattn/go-localereader v0.0.1 // indirect
|
||||
@@ -26,8 +30,9 @@ require (
|
||||
github.com/muesli/termenv v0.15.2 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/rivo/uniseg v0.2.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
golang.org/x/sync v0.1.0 // indirect
|
||||
golang.org/x/sys v0.7.0 // indirect
|
||||
golang.org/x/text v0.9.0 // indirect
|
||||
golang.org/x/sys v0.12.0 // indirect
|
||||
golang.org/x/text v0.13.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
|
||||
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
|
||||
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
|
||||
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
|
||||
@@ -10,8 +12,14 @@ github.com/charmbracelet/lipgloss v0.8.0 h1:IS00fk4XAHcf8uZKc3eHeMUTCxUH6NkaTrdy
|
||||
github.com/charmbracelet/lipgloss v0.8.0/go.mod h1:p4eYUZZJ/0oXTuCQKFF8mqyKCz0ja6y+7DniDDw5KKU=
|
||||
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
|
||||
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
|
||||
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
|
||||
github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||
@@ -36,6 +44,13 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
||||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
|
||||
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
@@ -54,24 +69,26 @@ golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
|
||||
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
|
||||
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
|
||||
golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU=
|
||||
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
|
||||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
@@ -79,5 +96,6 @@ golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -1,23 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/cpendery/clac/ui"
|
||||
"github.com/cpendery/clac/cmd"
|
||||
)
|
||||
|
||||
func main() {
|
||||
f, err := tea.LogToFile("debug.log", "debug")
|
||||
if err != nil {
|
||||
fmt.Println("fatal:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer f.Close()
|
||||
p := tea.NewProgram(ui.Start())
|
||||
if _, err := p.Run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
cmd.Execute()
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
__clac_cmd() {
|
||||
return "./clac"
|
||||
}
|
||||
|
||||
__clac__() {
|
||||
./clac "$READLINE_LINE"
|
||||
output=$(./clac -o)
|
||||
eval "$output"
|
||||
}
|
||||
|
||||
bind -x '"\C-a": __clac__'
|
||||
@@ -0,0 +1,50 @@
|
||||
package shell
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/adrg/xdg"
|
||||
)
|
||||
|
||||
var (
|
||||
SupportedShells = []string{"bash"}
|
||||
)
|
||||
|
||||
const (
|
||||
clacHomeFolder = ".clac"
|
||||
bashrcFile = ".bashrc"
|
||||
)
|
||||
|
||||
//go:embed key-bindings.bash
|
||||
var bashBindings []byte
|
||||
|
||||
func CreateBashBinding() error {
|
||||
if err := os.MkdirAll(path.Join(xdg.Home, clacHomeFolder), 0770); err != nil {
|
||||
return fmt.Errorf("unable to create .clac folder: %w", err)
|
||||
}
|
||||
|
||||
f, err := os.Create(path.Join(xdg.Home, clacHomeFolder, "key-bindings.bash"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create bash keybindings file: %w", err)
|
||||
}
|
||||
|
||||
if _, err = f.Write(bashBindings); err != nil {
|
||||
return fmt.Errorf("unable to write to bash keybindings file: %w", err)
|
||||
}
|
||||
|
||||
f, err = os.OpenFile(path.Join(xdg.Home, bashrcFile), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to append to bashrc: %w", err)
|
||||
}
|
||||
|
||||
bindingsPath := "~/.clac/key-bindings.bash"
|
||||
content := fmt.Sprintf("\n[ -f %s ] && source %s", bindingsPath, bindingsPath)
|
||||
if _, err := f.Write([]byte(content)); err != nil {
|
||||
return fmt.Errorf("unable to append to bashrc: %w", err)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -3,11 +3,13 @@ package ui
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/bubbles/cursor"
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/cpendery/clac/autocomplete"
|
||||
"github.com/cpendery/clac/ui/suggestions"
|
||||
"github.com/cpendery/clac/ui/theme"
|
||||
"github.com/cpendery/clac/ui/utils"
|
||||
@@ -18,17 +20,20 @@ type model struct {
|
||||
suggestions suggestions.Model
|
||||
windowHeight int
|
||||
windowWidth int
|
||||
complete bool
|
||||
}
|
||||
|
||||
const (
|
||||
prompt = "> "
|
||||
promptOffset = len(prompt)
|
||||
cursorOffset = 1
|
||||
widthOffset = promptOffset + cursorOffset
|
||||
clacOutputEnvVar = "CLAC_COMPLETED_CMD"
|
||||
prompt = "> "
|
||||
promptOffset = len(prompt)
|
||||
cursorOffset = 1
|
||||
widthOffset = promptOffset + cursorOffset
|
||||
)
|
||||
|
||||
func Start() model {
|
||||
func Start(startingContent string) model {
|
||||
ti := textinput.New()
|
||||
ti.SetValue(strings.TrimSpace(startingContent))
|
||||
ti.Cursor.Style = lipgloss.NewStyle().Foreground(theme.CursorForeground)
|
||||
ti.Focus()
|
||||
sug := suggestions.New()
|
||||
@@ -44,7 +49,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.Type {
|
||||
case tea.KeyEnter, tea.KeyCtrlC, tea.KeyEsc:
|
||||
case tea.KeyEnter:
|
||||
autocomplete.CacheResult(m.textInput.Value())
|
||||
m.complete = true
|
||||
return m, tea.Quit
|
||||
case tea.KeyCtrlC, tea.KeyEsc:
|
||||
m.complete = true
|
||||
return m, tea.Quit
|
||||
case tea.KeyTab:
|
||||
if !m.suggestions.HasActiveSuggestion() {
|
||||
@@ -76,6 +86,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
}
|
||||
|
||||
func (m model) View() string {
|
||||
if m.complete {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"%s\n%s",
|
||||
m.textInput.View(),
|
||||
|
||||
Reference in New Issue
Block a user