Add completion for the kittens

This commit is contained in:
Kovid Goyal
2022-09-17 11:59:41 +05:30
parent 25a7ec9a07
commit 7737369fc9
12 changed files with 114 additions and 15 deletions

View File

@@ -185,7 +185,17 @@ func complete_by_fnmatch(prefix, cwd string, patterns []string) []string {
}
func complete_by_mimepat(prefix, cwd string, patterns []string) []string {
all_allowed := false
for _, p := range patterns {
if p == "*" {
all_allowed = true
break
}
}
return fname_based_completer(prefix, cwd, func(name string) bool {
if all_allowed {
return true
}
idx := strings.Index(name, ".")
if idx < 1 {
return false

View File

@@ -3,8 +3,12 @@
package completion
import (
"bufio"
"bytes"
"fmt"
"kitty/tools/utils"
"os"
"os/exec"
"path/filepath"
"strings"
@@ -97,3 +101,20 @@ func complete_plus_runpy(completions *Completions, word string, arg_num int) {
func complete_plus_open(completions *Completions, word string, arg_num int) {
fnmatch_completer("Files", CWD, "*")(completions, word, arg_num)
}
func complete_themes(completions *Completions, word string, arg_num int) {
kitty, err := utils.KittyExe()
if err == nil {
out, err := exec.Command(kitty, "+runpy", "from kittens.themes.collection import *; print_theme_names()").Output()
if err == nil {
mg := completions.add_match_group("Themes")
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
theme_name := strings.TrimSpace(scanner.Text())
if theme_name != "" && strings.HasPrefix(theme_name, word) {
mg.add_match(theme_name)
}
}
}
}
}

View File

@@ -139,6 +139,7 @@ func (cmd *Command) parse_args(words []string, completions *Completions) {
for i, word := range words {
cmd = completions.current_cmd
completions.current_word_idx = i
completions.current_word_idx_in_parent++
is_last_word := i == len(words)-1
if expecting_arg_for == nil && !strings.HasPrefix(word, "-") {
arg_num++
@@ -172,6 +173,7 @@ func (cmd *Command) parse_args(words []string, completions *Completions) {
completions.current_cmd = sc
cmd = sc
arg_num = 0
completions.current_word_idx_in_parent = 0
only_args_allowed = false
} else if cmd.Stop_processing_at_arg > 0 && arg_num >= cmd.Stop_processing_at_arg {
return

View File

@@ -31,9 +31,10 @@ func (self *MatchGroup) add_prefix_to_all_matches(prefix string) {
type Completions struct {
Groups []*MatchGroup `json:"groups,omitempty"`
current_cmd *Command
all_words []string // all words passed to parse_args()
current_word_idx int // index of current word in all_words
current_cmd *Command
all_words []string // all words passed to parse_args()
current_word_idx int // index of current word in all_words
current_word_idx_in_parent int // index of current word in parents command line 1 for first word after parent
}
func (self *Completions) add_prefix_to_all_matches(prefix string) {
@@ -141,7 +142,7 @@ func (self *Command) has_subcommands() bool {
func (self *Command) sub_command_allowed_at(completions *Completions, arg_num int) bool {
if self.Subcommand_must_be_first {
return arg_num == 1 && completions.current_word_idx == 0
return arg_num == 1 && completions.current_word_idx_in_parent == 1
}
return arg_num == 1
}