|
6 | 6 | "fmt" |
7 | 7 |
|
8 | 8 | "kitty/tools/themes" |
| 9 | + "kitty/tools/utils" |
| 10 | + "kitty/tools/wcswidth" |
9 | 11 | ) |
10 | 12 |
|
11 | 13 | var _ = fmt.Print |
@@ -40,10 +42,58 @@ func (self *ThemesList) Next(delta int, allow_wrapping bool) bool { |
40 | 42 | return true |
41 | 43 | } |
42 | 44 |
|
| 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 | + |
43 | 53 | func (self *ThemesList) UpdateThemes(themes *themes.Themes) { |
44 | 54 | self.themes, self.all_themes = themes, themes |
45 | 55 | if self.current_search != "" { |
46 | 56 | self.themes = self.all_themes.Copy() |
| 57 | + self.display_strings = utils.Map(limit_lengths, self.themes.ApplySearch(self.current_search)) |
47 | 58 | } 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 |
48 | 97 | } |
| 98 | + return self.themes.At(self.current_idx) |
49 | 99 | } |
0 commit comments