Skip to content

Commit 2195493

Browse files
committed
More work on porting themes
1 parent c473177 commit 2195493

File tree

5 files changed

+616
-10
lines changed

5 files changed

+616
-10
lines changed

tools/cmd/themes/list.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77

88
"kitty/tools/themes"
9+
"kitty/tools/utils"
10+
"kitty/tools/wcswidth"
911
)
1012

1113
var _ = fmt.Print
@@ -40,10 +42,58 @@ func (self *ThemesList) Next(delta int, allow_wrapping bool) bool {
4042
return true
4143
}
4244

45+
func limit_lengths(text string) string {
46+
t, x := wcswidth.TruncateToVisualLengthWithWidth(text, 31)
47+
if x >= len(text) {
48+
return text
49+
}
50+
return t + "…"
51+
}
52+
4353
func (self *ThemesList) UpdateThemes(themes *themes.Themes) {
4454
self.themes, self.all_themes = themes, themes
4555
if self.current_search != "" {
4656
self.themes = self.all_themes.Copy()
57+
self.display_strings = utils.Map(limit_lengths, self.themes.ApplySearch(self.current_search))
4758
} else {
59+
self.display_strings = utils.Map(limit_lengths, self.themes.Names())
60+
}
61+
self.widths = utils.Map(wcswidth.Stringwidth, self.display_strings)
62+
self.max_width = utils.Max(0, self.widths...)
63+
self.current_idx = 0
64+
}
65+
66+
func (self *ThemesList) UpdateSearch(query string) bool {
67+
if query == self.current_search || self.all_themes == nil {
68+
return false
69+
}
70+
self.current_search = query
71+
self.UpdateThemes(self.all_themes)
72+
return true
73+
}
74+
75+
type Line struct {
76+
text string
77+
width int
78+
is_current bool
79+
}
80+
81+
func (self *ThemesList) Lines(num_rows int) []Line {
82+
if num_rows < 1 {
83+
return nil
84+
}
85+
ans := make([]Line, 0, len(self.display_strings))
86+
before_num := utils.Min(self.current_idx, num_rows-1)
87+
start := self.current_idx - before_num
88+
for i := start; i < utils.Min(start+num_rows, len(self.display_strings)); i++ {
89+
ans = append(ans, Line{self.display_strings[i], self.widths[i], i == self.current_idx})
90+
}
91+
return ans
92+
}
93+
94+
func (self *ThemesList) CurrentTheme() *themes.Theme {
95+
if self.themes == nil {
96+
return nil
4897
}
98+
return self.themes.At(self.current_idx)
4999
}

tools/cmd/themes/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
7474
lp.SetCursorVisible(true)
7575
return ``
7676
}
77+
lp.OnResize = func(_, _ loop.ScreenSize) error {
78+
h.draw_screen()
79+
return nil
80+
}
81+
lp.OnKeyEvent = h.on_key_event
7782
err = lp.Run()
7883
if err != nil {
7984
return 1, err

0 commit comments

Comments
 (0)