Add fish shell

This commit is contained in:
Anton Medvedev 2024-03-14 20:02:46 +01:00
parent f3af6a086d
commit c89dcc2864
No known key found for this signature in database
5 changed files with 46 additions and 20 deletions

View File

@ -37,6 +37,7 @@ var globals = []string{
"Object.fromEntries", "Object.fromEntries",
"Array.isArray", "Array.isArray",
"Array.from", "Array.from",
"console.log",
"len", "len",
"uniq", "uniq",
"sort", "sort",
@ -49,12 +50,13 @@ var globals = []string{
"reverse", "reverse",
"keys", "keys",
"values", "values",
"skip",
} }
func Complete() bool { func Complete() bool {
compLine, ok := os.LookupEnv("COMP_LINE") compLine, ok := os.LookupEnv("COMP_LINE")
if ok && len(os.Args) == 3 { if ok && len(os.Args) >= 3 {
doComplete(compLine, os.Args[2]) doComplete(compLine, os.Args[2])
return true return true
} }
@ -65,6 +67,12 @@ func Complete() bool {
return true return true
} }
compFish, ok := os.LookupEnv("COMP_FISH")
if ok {
doComplete(compFish, lastWord(compFish))
return true
}
return false return false
} }
@ -108,11 +116,11 @@ func doComplete(compLine string, compWord string) {
isSecondArgIsFile = isFile(args[1]) isSecondArgIsFile = isFile(args[1])
} }
if isSecondArgIsFile { if globalsComplete(compWord) {
if globalsComplete(compWord) { return
return }
}
if isSecondArgIsFile {
file := args[1] file := args[1]
hasYamlExt, _ := regexp.MatchString(`(?i)\.ya?ml$`, file) hasYamlExt, _ := regexp.MatchString(`(?i)\.ya?ml$`, file)
@ -190,12 +198,25 @@ func codeComplete(input string, args []string, compWord string) {
prefix := dropTail(compWord) prefix := dropTail(compWord)
var reply []string var reply []string
for _, key := range array { for _, key := range array {
reply = append(reply, prefix+key.(string)) reply = append(reply, join(prefix, key.(string)))
} }
compReply(filterReply(reply, compWord)) compReply(filterReply(reply, compWord))
} }
} }
var alphaRe = regexp.MustCompile(`^\w+$`)
func join(prefix, key string) string {
if alphaRe.MatchString(key) {
return prefix + "." + key
} else {
if prefix == "" {
return fmt.Sprintf(".[%q]", key)
}
return fmt.Sprintf("%s[%q]", prefix, key)
}
}
func filterArgs(args []string) []string { func filterArgs(args []string) []string {
filtered := make([]string, 0, len(args)) filtered := make([]string, 0, len(args))
for _, arg := range args { for _, arg := range args {

View File

@ -0,0 +1,6 @@
package complete
func Fish() string {
return `complete --command fx --arguments '(COMP_FISH=(commandline -cp) fx)'
`
}

View File

@ -8,11 +8,7 @@ Object.prototype.__keys = function () {
if (typeof this === 'string') return if (typeof this === 'string') return
if (this instanceof String) return if (this instanceof String) return
if (typeof this === 'object' && this !== null) if (typeof this === 'object' && this !== null)
Object.keys(this) Object.keys(this).forEach(x => __keys.add(x))
.forEach(x => /^\w+$/.test(x) ?
__keys.add('.' + x) :
__keys.add('.[' + JSON.stringify(x) + ']')
)
} }
function apply(fn, ...args) { function apply(fn, ...args) {

View File

@ -3,14 +3,11 @@ package complete
import ( import (
"fmt" "fmt"
"os" "os"
"regexp"
"strings" "strings"
) )
func compReply(reply []string) { func compReply(reply []string) {
for _, word := range reply { fmt.Print(strings.Join(reply, "\n"))
fmt.Println(word)
}
} }
func filterReply(reply []string, compWord string) []string { func filterReply(reply []string, compWord string) []string {
@ -31,10 +28,9 @@ func isFile(path string) bool {
return !info.IsDir() return !info.IsDir()
} }
var tailRe = regexp.MustCompile(`\.?\w*$`)
func dropTail(s string) string { func dropTail(s string) string {
return tailRe.ReplaceAllString(s, "") parts := strings.Split(s, ".")
return strings.Join(parts[:len(parts)-1], ".")
} }
func balanceBrackets(code string) string { func balanceBrackets(code string) string {

11
main.go
View File

@ -89,6 +89,8 @@ func main() {
fmt.Print(complete.Bash()) fmt.Print(complete.Bash())
case "zsh": case "zsh":
fmt.Print(complete.Zsh()) fmt.Print(complete.Zsh())
case "fish":
fmt.Print(complete.Fish())
default: default:
fmt.Println("unknown shell type") fmt.Println("unknown shell type")
} }
@ -176,9 +178,14 @@ func main() {
lipgloss.SetColorProfile(termOutput.ColorProfile()) lipgloss.SetColorProfile(termOutput.ColorProfile())
withMouse := tea.WithMouseCellMotion()
if _, ok := os.LookupEnv("FX_NO_MOUSE"); ok {
withMouse = tea.WithAltScreen()
}
p := tea.NewProgram(m, p := tea.NewProgram(m,
tea.WithAltScreen(), tea.WithAltScreen(),
tea.WithMouseCellMotion(), withMouse,
tea.WithOutput(os.Stderr), tea.WithOutput(os.Stderr),
) )
_, err = p.Run() _, err = p.Run()
@ -422,7 +429,7 @@ func (m *model) handleYankKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
_ = clipboard.WriteAll(m.cursorPath()) _ = clipboard.WriteAll(m.cursorPath())
case key.Matches(msg, yankKey): case key.Matches(msg, yankKey):
_ = clipboard.WriteAll(m.cursorKey()) _ = clipboard.WriteAll(m.cursorKey())
case key.Matches(msg, yankValue): case key.Matches(msg, yankValueY, yankValueV):
_ = clipboard.WriteAll(m.cursorValue()) _ = clipboard.WriteAll(m.cursorValue())
} }
m.yank = false m.yank = false