mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-09 21:55:29 +02:00
More work on porting themes UI to Go
This commit is contained in:
49
tools/cmd/themes/list.go
Normal file
49
tools/cmd/themes/list.go
Normal file
@@ -0,0 +1,49 @@
|
||||
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package themes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"kitty/tools/themes"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
type ThemesList struct {
|
||||
themes, all_themes *themes.Themes
|
||||
current_search string
|
||||
display_strings []string
|
||||
widths []int
|
||||
max_width, current_idx int
|
||||
}
|
||||
|
||||
func (self *ThemesList) Len() int {
|
||||
if self.themes == nil {
|
||||
return 0
|
||||
}
|
||||
return self.themes.Len()
|
||||
}
|
||||
|
||||
func (self *ThemesList) Next(delta int, allow_wrapping bool) bool {
|
||||
if len(self.display_strings) == 0 {
|
||||
return false
|
||||
}
|
||||
idx := self.current_idx + delta
|
||||
if !allow_wrapping && (idx < 0 || idx > self.Len()) {
|
||||
return false
|
||||
}
|
||||
for idx < 0 {
|
||||
idx += self.Len()
|
||||
}
|
||||
self.current_idx = idx % self.Len()
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *ThemesList) UpdateThemes(themes *themes.Themes) {
|
||||
self.themes, self.all_themes = themes, themes
|
||||
if self.current_search != "" {
|
||||
self.themes = self.all_themes.Copy()
|
||||
} else {
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"kitty/tools/cli"
|
||||
"kitty/tools/themes"
|
||||
"kitty/tools/tui/loop"
|
||||
"kitty/tools/utils"
|
||||
)
|
||||
|
||||
@@ -54,6 +55,35 @@ func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
|
||||
if len(args) == 1 {
|
||||
return non_interactive(opts, args[0])
|
||||
}
|
||||
lp, err := loop.New()
|
||||
if err != nil {
|
||||
return 1, err
|
||||
}
|
||||
cv := utils.NewCachedValues("unicode-input", &CachedData{Category: "All"})
|
||||
h := &handler{lp: lp, opts: opts, cached_data: cv.Load()}
|
||||
defer cv.Save()
|
||||
lp.OnInitialize = func() (string, error) {
|
||||
lp.AllowLineWrapping(false)
|
||||
lp.SetWindowTitle(`Choose a theme for kitty`)
|
||||
h.initialize()
|
||||
return "", nil
|
||||
}
|
||||
lp.OnWakeup = h.on_wakeup
|
||||
lp.OnFinalize = func() string {
|
||||
h.finalize()
|
||||
lp.SetCursorVisible(true)
|
||||
return ``
|
||||
}
|
||||
err = lp.Run()
|
||||
if err != nil {
|
||||
return 1, err
|
||||
}
|
||||
ds := lp.DeathSignalName()
|
||||
if ds != "" {
|
||||
fmt.Println("Killed by signal: ", ds)
|
||||
lp.KillIfSignalled()
|
||||
return 1, nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
84
tools/cmd/themes/ui.go
Normal file
84
tools/cmd/themes/ui.go
Normal file
@@ -0,0 +1,84 @@
|
||||
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package themes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"kitty/tools/themes"
|
||||
"kitty/tools/tui/loop"
|
||||
"time"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
type State int
|
||||
|
||||
const (
|
||||
FETCHING State = iota
|
||||
BROWSING
|
||||
SEARCHING
|
||||
ACCEPTING
|
||||
)
|
||||
|
||||
type CachedData struct {
|
||||
Recent []string `json:"recent"`
|
||||
Category string `json:"category"`
|
||||
}
|
||||
|
||||
type fetch_data struct {
|
||||
themes *themes.Themes
|
||||
err error
|
||||
closer io.Closer
|
||||
}
|
||||
|
||||
type handler struct {
|
||||
lp *loop.Loop
|
||||
opts *Options
|
||||
cached_data *CachedData
|
||||
|
||||
state State
|
||||
fetch_result chan fetch_data
|
||||
all_themes *themes.Themes
|
||||
themes_closer io.Closer
|
||||
}
|
||||
|
||||
func (self *handler) fetch_themes() {
|
||||
r := fetch_data{}
|
||||
r.themes, r.closer, r.err = themes.LoadThemes(time.Duration(self.opts.CacheAge * float64(time.Hour*24)))
|
||||
self.lp.WakeupMainThread()
|
||||
self.fetch_result <- r
|
||||
}
|
||||
|
||||
func (self *handler) on_wakeup() error {
|
||||
r := <-self.fetch_result
|
||||
if r.err != nil {
|
||||
return r.err
|
||||
}
|
||||
self.state = BROWSING
|
||||
self.all_themes = r.themes
|
||||
self.themes_closer = r.closer
|
||||
self.redraw_after_category_change()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *handler) finalize() {
|
||||
t := self.themes_closer
|
||||
if t != nil {
|
||||
t.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (self *handler) initialize() {
|
||||
self.fetch_result = make(chan fetch_data)
|
||||
go self.fetch_themes()
|
||||
self.draw_screen()
|
||||
}
|
||||
|
||||
func (self *handler) draw_screen() {
|
||||
// TODO: Implement me
|
||||
}
|
||||
|
||||
func (self *handler) redraw_after_category_change() {
|
||||
// TODO: Implement me
|
||||
}
|
||||
Reference in New Issue
Block a user