Start work on porting themes kitten to Go

This commit is contained in:
Kovid Goyal
2023-03-11 10:14:03 +05:30
parent 3741d3d1be
commit f9b0b54ee5
7 changed files with 508 additions and 24 deletions

View File

@@ -4,11 +4,10 @@ package completion
import (
"fmt"
"os/exec"
"strings"
"kitty/tools/cli"
"kitty/tools/utils"
"kitty/tools/themes"
)
var _ = fmt.Print
@@ -64,20 +63,7 @@ func complete_plus_open(completions *cli.Completions, word string, arg_num int)
}
func complete_themes(completions *cli.Completions, word string, arg_num int) {
kitty := utils.KittyExe()
if kitty != "" {
out, err := exec.Command(kitty, "+runpy", "from kittens.themes.collection import *; print_theme_names()").Output()
if err == nil {
mg := completions.AddMatchGroup("Themes")
scanner := utils.NewLineScanner(utils.UnsafeBytesToString(out))
for scanner.Scan() {
theme_name := strings.TrimSpace(scanner.Text())
if theme_name != "" && strings.HasPrefix(theme_name, word) {
mg.AddMatch(theme_name)
}
}
}
}
themes.CompleteThemes(completions, word, arg_num)
}
func EntryPoint(tool_root *cli.Command) {

62
tools/cmd/themes/main.go Normal file
View File

@@ -0,0 +1,62 @@
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
package themes
import (
"fmt"
"strings"
"time"
"kitty/tools/cli"
"kitty/tools/themes"
"kitty/tools/utils"
)
var _ = fmt.Print
func complete_themes(completions *cli.Completions, word string, arg_num int) {
themes.CompleteThemes(completions, word, arg_num)
}
func non_interactive(opts *Options, theme_name string) (rc int, err error) {
themes, closer, err := themes.LoadThemes(time.Duration(opts.CacheAge * float64(time.Hour*24)))
if err != nil {
return 1, err
}
defer closer.Close()
theme := themes.ThemeByName(theme_name)
if theme == nil {
theme_name = strings.ReplaceAll(theme_name, `\`, ``)
theme = themes.ThemeByName(theme_name)
if theme == nil {
return 1, fmt.Errorf("No theme named: %s", theme_name)
}
}
if opts.DumpTheme {
code, err := theme.Code()
if err != nil {
return 1, err
}
fmt.Println(code)
} else {
err = theme.SaveInConf(utils.ConfigDir(), opts.ReloadIn, opts.ConfigFileName)
if err != nil {
return 1, err
}
}
return
}
func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
if len(args) > 1 {
args = []string{strings.Join(args, ` `)}
}
if len(args) == 1 {
return non_interactive(opts, args[0])
}
return
}
func EntryPoint(parent *cli.Command) {
create_cmd(parent, main)
}

View File

@@ -15,6 +15,7 @@ import (
"kitty/tools/cmd/icat"
"kitty/tools/cmd/pytest"
"kitty/tools/cmd/ssh"
"kitty/tools/cmd/themes"
"kitty/tools/cmd/unicode_input"
"kitty/tools/cmd/update_self"
"kitty/tools/tui"
@@ -45,6 +46,8 @@ func KittyToolEntryPoints(root *cli.Command) {
ask.EntryPoint(root)
// hints
hints.EntryPoint(root)
// themes
themes.EntryPoint(root)
// __pytest__
pytest.EntryPoint(root)
// __hold_till_enter__