| 1 |
package app |
| 2 |
|
| 3 |
import ( |
| 4 |
"math" |
| 5 |
"strings" |
| 6 |
"sync" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/config" |
| 9 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 10 |
"git.sr.ht/~rjarry/aerc/lib/ui" |
| 11 |
"github.com/mattn/go-runewidth" |
| 12 |
"go.rockorager.dev/vaxis" |
| 13 |
) |
| 14 |
|
| 15 |
type ListBox struct { |
| 16 |
Scrollable |
| 17 |
title string |
| 18 |
lines []string |
| 19 |
selected string |
| 20 |
cursorPos int |
| 21 |
horizPos int |
| 22 |
jump int |
| 23 |
showCursor bool |
| 24 |
showFilter bool |
| 25 |
filterMutex sync.Mutex |
| 26 |
filter *ui.TextInput |
| 27 |
uiConfig *config.UIConfig |
| 28 |
textFilter func([]string, string) []string |
| 29 |
cb func(string) |
| 30 |
} |
| 31 |
|
| 32 |
func NewListBox(title string, lines []string, uiConfig *config.UIConfig, cb func(string)) *ListBox { |
| 33 |
lb := &ListBox{ |
| 34 |
title: title, |
| 35 |
lines: lines, |
| 36 |
cursorPos: -1, |
| 37 |
jump: -1, |
| 38 |
uiConfig: uiConfig, |
| 39 |
textFilter: nil, |
| 40 |
cb: cb, |
| 41 |
filter: ui.NewTextInput("", uiConfig), |
| 42 |
} |
| 43 |
lb.filter.OnChange(func(ti *ui.TextInput) { |
| 44 |
var show bool |
| 45 |
if ti.String() == "" { |
| 46 |
show = false |
| 47 |
} else { |
| 48 |
show = true |
| 49 |
} |
| 50 |
lb.setShowFilterField(show) |
| 51 |
lb.filter.Focus(show) |
| 52 |
lb.Invalidate() |
| 53 |
}) |
| 54 |
lb.dedup() |
| 55 |
return lb |
| 56 |
} |
| 57 |
|
| 58 |
func (lb *ListBox) SetTextFilter(fn func([]string, string) []string) *ListBox { |
| 59 |
lb.textFilter = fn |
| 60 |
return lb |
| 61 |
} |
| 62 |
|
| 63 |
func (lb *ListBox) dedup() { |
| 64 |
dedupped := make([]string, 0, len(lb.lines)) |
| 65 |
dedup := make(map[string]struct{}) |
| 66 |
for _, line := range lb.lines { |
| 67 |
if _, dup := dedup[line]; dup { |
| 68 |
log.Warnf("ignore duplicate: %s", line) |
| 69 |
continue |
| 70 |
} |
| 71 |
dedup[line] = struct{}{} |
| 72 |
dedupped = append(dedupped, line) |
| 73 |
} |
| 74 |
lb.lines = dedupped |
| 75 |
} |
| 76 |
|
| 77 |
func (lb *ListBox) setShowFilterField(b bool) { |
| 78 |
lb.filterMutex.Lock() |
| 79 |
defer lb.filterMutex.Unlock() |
| 80 |
lb.showFilter = b |
| 81 |
} |
| 82 |
|
| 83 |
func (lb *ListBox) showFilterField() bool { |
| 84 |
lb.filterMutex.Lock() |
| 85 |
defer lb.filterMutex.Unlock() |
| 86 |
return lb.showFilter |
| 87 |
} |
| 88 |
|
| 89 |
func (lb *ListBox) Draw(ctx *ui.Context) { |
| 90 |
defaultStyle := lb.uiConfig.GetStyle(config.STYLE_DEFAULT) |
| 91 |
titleStyle := lb.uiConfig.GetStyle(config.STYLE_TITLE) |
| 92 |
w, h := ctx.Width(), ctx.Height() |
| 93 |
ctx.Fill(0, 0, w, h, ' ', defaultStyle) |
| 94 |
ctx.Fill(0, 0, w, 1, ' ', titleStyle) |
| 95 |
ctx.Printf(0, 0, titleStyle, "%s", lb.title) |
| 96 |
|
| 97 |
y := 0 |
| 98 |
if lb.showFilterField() { |
| 99 |
y = 1 |
| 100 |
x := ctx.Printf(0, y, defaultStyle, "Filter (%d/%d): ", |
| 101 |
len(lb.filtered()), len(lb.lines)) |
| 102 |
lb.filter.Draw(ctx.Subcontext(x, y, w-x, 1)) |
| 103 |
} |
| 104 |
|
| 105 |
lb.drawBox(ctx.Subcontext(0, y+1, w, h-(y+1))) |
| 106 |
} |
| 107 |
|
| 108 |
func (lb *ListBox) moveCursor(delta int) { |
| 109 |
list := lb.filtered() |
| 110 |
if len(list) == 0 { |
| 111 |
return |
| 112 |
} |
| 113 |
lb.cursorPos += delta |
| 114 |
if lb.cursorPos < 0 { |
| 115 |
lb.cursorPos = 0 |
| 116 |
} |
| 117 |
if lb.cursorPos >= len(list) { |
| 118 |
lb.cursorPos = len(list) - 1 |
| 119 |
} |
| 120 |
lb.selected = list[lb.cursorPos] |
| 121 |
lb.showCursor = true |
| 122 |
lb.horizPos = 0 |
| 123 |
} |
| 124 |
|
| 125 |
func (lb *ListBox) moveHorizontal(delta int) { |
| 126 |
lb.horizPos += delta |
| 127 |
if lb.horizPos > len(lb.selected) { |
| 128 |
lb.horizPos = len(lb.selected) |
| 129 |
} |
| 130 |
if lb.horizPos < 0 { |
| 131 |
lb.horizPos = 0 |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
func (lb *ListBox) filtered() []string { |
| 136 |
term := lb.filter.String() |
| 137 |
|
| 138 |
if lb.textFilter != nil { |
| 139 |
return lb.textFilter(lb.lines, term) |
| 140 |
} |
| 141 |
|
| 142 |
list := make([]string, 0, len(lb.lines)) |
| 143 |
for _, line := range lb.lines { |
| 144 |
if strings.Contains(line, term) { |
| 145 |
list = append(list, line) |
| 146 |
} |
| 147 |
} |
| 148 |
return list |
| 149 |
} |
| 150 |
|
| 151 |
func (lb *ListBox) drawBox(ctx *ui.Context) { |
| 152 |
defaultStyle := lb.uiConfig.GetStyle(config.STYLE_DEFAULT) |
| 153 |
selectedStyle := lb.uiConfig.GetComposedStyleSelected(config.STYLE_MSGLIST_DEFAULT, nil) |
| 154 |
|
| 155 |
w, h := ctx.Width(), ctx.Height() |
| 156 |
lb.jump = h |
| 157 |
list := lb.filtered() |
| 158 |
|
| 159 |
lb.UpdateScroller(ctx.Height(), len(list)) |
| 160 |
scroll := 0 |
| 161 |
lb.cursorPos = -1 |
| 162 |
for i := range list { |
| 163 |
if lb.selected == list[i] { |
| 164 |
scroll = i |
| 165 |
lb.cursorPos = i |
| 166 |
break |
| 167 |
} |
| 168 |
} |
| 169 |
if len(list) > 0 { |
| 170 |
if lb.cursorPos == -1 { |
| 171 |
// The list is not empty and we did not find the selection, if any, |
| 172 |
// so clear it. |
| 173 |
lb.selected = "" |
| 174 |
// Select the fist matching item to avoid the user needing to |
| 175 |
// explicitly select it in case it matches what they want. |
| 176 |
lb.moveCursor(0) |
| 177 |
} |
| 178 |
} else { |
| 179 |
// The list is now empty, the selection, if any, needs to be cleared. |
| 180 |
lb.selected = "" |
| 181 |
} |
| 182 |
lb.EnsureScroll(scroll) |
| 183 |
|
| 184 |
needScrollbar := lb.NeedScrollbar() |
| 185 |
if needScrollbar { |
| 186 |
w -= 1 |
| 187 |
if w < 0 { |
| 188 |
w = 0 |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
if lb.lines == nil || len(list) == 0 { |
| 193 |
return |
| 194 |
} |
| 195 |
|
| 196 |
y := 0 |
| 197 |
for i := lb.Scroll(); i < len(list) && y < h; i++ { |
| 198 |
style := defaultStyle |
| 199 |
line := runewidth.Truncate(list[i], w-1, "❯") |
| 200 |
if lb.selected == list[i] && lb.showCursor { |
| 201 |
style = selectedStyle |
| 202 |
if len(list[i]) > w { |
| 203 |
if len(list[i])-lb.horizPos < w { |
| 204 |
lb.horizPos = len(list[i]) - w + 1 |
| 205 |
} |
| 206 |
rest := list[i][lb.horizPos:] |
| 207 |
line = runewidth.Truncate(rest, |
| 208 |
w-1, "❯") |
| 209 |
if lb.horizPos > 0 && len(line) > 0 { |
| 210 |
line = "❮" + line[1:] |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
ctx.Printf(1, y, style, "%s", line) |
| 215 |
y += 1 |
| 216 |
} |
| 217 |
|
| 218 |
if needScrollbar { |
| 219 |
scrollBarCtx := ctx.Subcontext(w, 0, 1, ctx.Height()) |
| 220 |
lb.drawScrollbar(scrollBarCtx) |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
func (lb *ListBox) drawScrollbar(ctx *ui.Context) { |
| 225 |
gutterStyle := vaxis.Style{} |
| 226 |
pillStyle := vaxis.Style{Attribute: vaxis.AttrReverse} |
| 227 |
|
| 228 |
// gutter |
| 229 |
h := ctx.Height() |
| 230 |
ctx.Fill(0, 0, 1, h, ' ', gutterStyle) |
| 231 |
|
| 232 |
// pill |
| 233 |
pillSize := int(math.Ceil(float64(h) * lb.PercentVisible())) |
| 234 |
pillOffset := int(math.Floor(float64(h) * lb.PercentScrolled())) |
| 235 |
ctx.Fill(0, pillOffset, 1, pillSize, ' ', pillStyle) |
| 236 |
} |
| 237 |
|
| 238 |
func (lb *ListBox) Invalidate() { |
| 239 |
ui.Invalidate() |
| 240 |
} |
| 241 |
|
| 242 |
func (lb *ListBox) Event(event vaxis.Event) bool { |
| 243 |
showFilter := lb.showFilterField() |
| 244 |
if key, ok := event.(vaxis.Key); ok { |
| 245 |
switch { |
| 246 |
case key.Matches(vaxis.KeyLeft): |
| 247 |
if showFilter { |
| 248 |
break |
| 249 |
} |
| 250 |
lb.moveHorizontal(-1) |
| 251 |
lb.Invalidate() |
| 252 |
return true |
| 253 |
case key.Matches(vaxis.KeyRight): |
| 254 |
if showFilter { |
| 255 |
break |
| 256 |
} |
| 257 |
lb.moveHorizontal(+1) |
| 258 |
lb.Invalidate() |
| 259 |
return true |
| 260 |
case key.Matches('b', vaxis.ModCtrl): |
| 261 |
if len(lb.selected) <= lb.horizPos { |
| 262 |
break |
| 263 |
} |
| 264 |
line := lb.selected[:lb.horizPos] |
| 265 |
fds := strings.Fields(line) |
| 266 |
if len(fds) > 1 { |
| 267 |
lb.moveHorizontal( |
| 268 |
strings.LastIndex(line, |
| 269 |
fds[len(fds)-1]) - lb.horizPos - 1) |
| 270 |
} else { |
| 271 |
lb.horizPos = 0 |
| 272 |
} |
| 273 |
lb.Invalidate() |
| 274 |
return true |
| 275 |
case key.Matches('w', vaxis.ModCtrl): |
| 276 |
if len(lb.selected) <= lb.horizPos { |
| 277 |
break |
| 278 |
} |
| 279 |
line := lb.selected[lb.horizPos+1:] |
| 280 |
fds := strings.Fields(line) |
| 281 |
if len(fds) > 1 { |
| 282 |
lb.moveHorizontal(strings.Index(line, fds[1])) |
| 283 |
} |
| 284 |
lb.Invalidate() |
| 285 |
return true |
| 286 |
case key.Matches('a', vaxis.ModCtrl), key.Matches(vaxis.KeyHome): |
| 287 |
if showFilter { |
| 288 |
break |
| 289 |
} |
| 290 |
lb.horizPos = 0 |
| 291 |
lb.Invalidate() |
| 292 |
return true |
| 293 |
case key.Matches('e', vaxis.ModCtrl), key.Matches(vaxis.KeyEnd): |
| 294 |
if showFilter { |
| 295 |
break |
| 296 |
} |
| 297 |
lb.horizPos = len(lb.selected) |
| 298 |
lb.Invalidate() |
| 299 |
return true |
| 300 |
case key.Matches('p', vaxis.ModCtrl), key.Matches(vaxis.KeyUp): |
| 301 |
lb.moveCursor(-1) |
| 302 |
lb.Invalidate() |
| 303 |
return true |
| 304 |
case key.Matches('n', vaxis.ModCtrl), key.Matches(vaxis.KeyDown): |
| 305 |
lb.moveCursor(+1) |
| 306 |
lb.Invalidate() |
| 307 |
return true |
| 308 |
case key.Matches(vaxis.KeyPgUp): |
| 309 |
if lb.jump >= 0 { |
| 310 |
lb.moveCursor(-lb.jump) |
| 311 |
lb.Invalidate() |
| 312 |
} |
| 313 |
return true |
| 314 |
case key.Matches(vaxis.KeyPgDown): |
| 315 |
if lb.jump >= 0 { |
| 316 |
lb.moveCursor(+lb.jump) |
| 317 |
lb.Invalidate() |
| 318 |
} |
| 319 |
return true |
| 320 |
case key.Matches(vaxis.KeyEnter): |
| 321 |
return lb.quit(lb.selected) |
| 322 |
case key.Matches(vaxis.KeyEsc): |
| 323 |
return lb.quit("") |
| 324 |
} |
| 325 |
} |
| 326 |
if lb.filter != nil { |
| 327 |
handled := lb.filter.Event(event) |
| 328 |
lb.Invalidate() |
| 329 |
return handled |
| 330 |
} |
| 331 |
return false |
| 332 |
} |
| 333 |
|
| 334 |
func (lb *ListBox) quit(s string) bool { |
| 335 |
lb.filter.Focus(false) |
| 336 |
if lb.cb != nil { |
| 337 |
lb.cb(s) |
| 338 |
} |
| 339 |
return true |
| 340 |
} |
| 341 |
|
| 342 |
func (lb *ListBox) Focus(f bool) { |
| 343 |
lb.filter.Focus(f) |
| 344 |
} |