| 1 |
package app |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"github.com/mattn/go-runewidth" |
| 8 |
|
| 9 |
"git.sr.ht/~rjarry/aerc/config" |
| 10 |
"git.sr.ht/~rjarry/aerc/lib/ui" |
| 11 |
"go.rockorager.dev/vaxis" |
| 12 |
) |
| 13 |
|
| 14 |
type Selector struct { |
| 15 |
chooser bool |
| 16 |
focused bool |
| 17 |
focus int |
| 18 |
options []string |
| 19 |
uiConfig *config.UIConfig |
| 20 |
|
| 21 |
onChoose func(option string) |
| 22 |
onSelect func(option string) |
| 23 |
} |
| 24 |
|
| 25 |
func NewSelector(options []string, focus int, uiConfig *config.UIConfig) *Selector { |
| 26 |
return &Selector{ |
| 27 |
focus: focus, |
| 28 |
options: options, |
| 29 |
uiConfig: uiConfig, |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
func (sel *Selector) Chooser(chooser bool) *Selector { |
| 34 |
sel.chooser = chooser |
| 35 |
return sel |
| 36 |
} |
| 37 |
|
| 38 |
func (sel *Selector) Invalidate() { |
| 39 |
ui.Invalidate() |
| 40 |
} |
| 41 |
|
| 42 |
func (sel *Selector) Draw(ctx *ui.Context) { |
| 43 |
defaultSelectorStyle := sel.uiConfig.GetStyle(config.STYLE_SELECTOR_DEFAULT) |
| 44 |
w, h := ctx.Width(), ctx.Height() |
| 45 |
ctx.Fill(0, 0, w, h, ' ', defaultSelectorStyle) |
| 46 |
|
| 47 |
if w < 5 || h < 1 { |
| 48 |
// if width and height are that small, don't even try to draw |
| 49 |
// something |
| 50 |
return |
| 51 |
} |
| 52 |
|
| 53 |
y := 1 |
| 54 |
if h == 1 { |
| 55 |
y = 0 |
| 56 |
} |
| 57 |
|
| 58 |
format := "[%s]" |
| 59 |
|
| 60 |
calculateWidth := func(space int) int { |
| 61 |
neededWidth := 2 |
| 62 |
for i, option := range sel.options { |
| 63 |
neededWidth += runewidth.StringWidth(fmt.Sprintf(format, option)) |
| 64 |
if i < len(sel.options)-1 { |
| 65 |
neededWidth += space |
| 66 |
} |
| 67 |
} |
| 68 |
return neededWidth - space |
| 69 |
} |
| 70 |
|
| 71 |
space := 5 |
| 72 |
for ; space > 0; space-- { |
| 73 |
if w > calculateWidth(space) { |
| 74 |
break |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
x := 2 |
| 79 |
for i, option := range sel.options { |
| 80 |
style := defaultSelectorStyle |
| 81 |
if sel.focus == i { |
| 82 |
if sel.focused { |
| 83 |
style = sel.uiConfig.GetStyle(config.STYLE_SELECTOR_FOCUSED) |
| 84 |
} else if sel.chooser { |
| 85 |
style = sel.uiConfig.GetStyle(config.STYLE_SELECTOR_CHOOSER) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if space == 0 { |
| 90 |
if sel.focus == i { |
| 91 |
leftArrow, rightArrow := ' ', ' ' |
| 92 |
if i > 0 { |
| 93 |
leftArrow = '❮' |
| 94 |
} |
| 95 |
if i < len(sel.options)-1 { |
| 96 |
rightArrow = '❯' |
| 97 |
} |
| 98 |
|
| 99 |
s := runewidth.Truncate(option, |
| 100 |
w-runewidth.RuneWidth(leftArrow)-runewidth.RuneWidth(rightArrow)-runewidth.StringWidth(fmt.Sprintf(format, "")), |
| 101 |
"…") |
| 102 |
|
| 103 |
nextPos := 0 |
| 104 |
nextPos += ctx.Printf(nextPos, y, defaultSelectorStyle, "%c", leftArrow) |
| 105 |
nextPos += ctx.Printf(nextPos, y, style, format, s) |
| 106 |
ctx.Printf(nextPos, y, defaultSelectorStyle, "%c", rightArrow) |
| 107 |
} |
| 108 |
} else { |
| 109 |
x += ctx.Printf(x, y, style, format, option) |
| 110 |
x += space |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func (sel *Selector) OnChoose(fn func(option string)) *Selector { |
| 116 |
sel.onChoose = fn |
| 117 |
return sel |
| 118 |
} |
| 119 |
|
| 120 |
func (sel *Selector) OnSelect(fn func(option string)) *Selector { |
| 121 |
sel.onSelect = fn |
| 122 |
return sel |
| 123 |
} |
| 124 |
|
| 125 |
func (sel *Selector) Select(option string) { |
| 126 |
for i, opt := range sel.options { |
| 127 |
if option == opt { |
| 128 |
sel.focus = i |
| 129 |
if sel.onSelect != nil { |
| 130 |
sel.onSelect(opt) |
| 131 |
} |
| 132 |
break |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
func (sel *Selector) Selected() string { |
| 138 |
return sel.options[sel.focus] |
| 139 |
} |
| 140 |
|
| 141 |
func (sel *Selector) Focus(focus bool) { |
| 142 |
sel.focused = focus |
| 143 |
sel.Invalidate() |
| 144 |
} |
| 145 |
|
| 146 |
func (sel *Selector) Event(event vaxis.Event) bool { |
| 147 |
if key, ok := event.(vaxis.Key); ok { |
| 148 |
switch { |
| 149 |
case key.Matches('h', vaxis.ModCtrl): |
| 150 |
fallthrough |
| 151 |
case key.Matches(vaxis.KeyLeft): |
| 152 |
if sel.focus > 0 { |
| 153 |
sel.focus-- |
| 154 |
sel.Invalidate() |
| 155 |
} |
| 156 |
if sel.onSelect != nil { |
| 157 |
sel.onSelect(sel.Selected()) |
| 158 |
} |
| 159 |
case key.Matches('l', vaxis.ModCtrl): |
| 160 |
fallthrough |
| 161 |
case key.Matches(vaxis.KeyRight): |
| 162 |
if sel.focus < len(sel.options)-1 { |
| 163 |
sel.focus++ |
| 164 |
sel.Invalidate() |
| 165 |
} |
| 166 |
if sel.onSelect != nil { |
| 167 |
sel.onSelect(sel.Selected()) |
| 168 |
} |
| 169 |
case key.Matches(vaxis.KeyEnter): |
| 170 |
if sel.onChoose != nil { |
| 171 |
sel.onChoose(sel.Selected()) |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
return false |
| 176 |
} |
| 177 |
|
| 178 |
var ErrNoOptionSelected = fmt.Errorf("no option selected") |
| 179 |
|
| 180 |
type SelectorDialog struct { |
| 181 |
callback func(string, error) |
| 182 |
title string |
| 183 |
prompt string |
| 184 |
uiConfig *config.UIConfig |
| 185 |
selector *Selector |
| 186 |
} |
| 187 |
|
| 188 |
func NewSelectorDialog(title string, prompt string, options []string, focus int, |
| 189 |
uiConfig *config.UIConfig, cb func(string, error), |
| 190 |
) *SelectorDialog { |
| 191 |
sd := &SelectorDialog{ |
| 192 |
callback: cb, |
| 193 |
title: title, |
| 194 |
prompt: strings.TrimSpace(prompt), |
| 195 |
uiConfig: uiConfig, |
| 196 |
selector: NewSelector(options, focus, uiConfig).Chooser(true), |
| 197 |
} |
| 198 |
sd.selector.Focus(true) |
| 199 |
return sd |
| 200 |
} |
| 201 |
|
| 202 |
func (gp *SelectorDialog) Draw(ctx *ui.Context) { |
| 203 |
defaultStyle := gp.uiConfig.GetStyle(config.STYLE_DEFAULT) |
| 204 |
titleStyle := gp.uiConfig.GetStyle(config.STYLE_TITLE) |
| 205 |
|
| 206 |
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', defaultStyle) |
| 207 |
ctx.Fill(0, 0, ctx.Width(), 1, ' ', titleStyle) |
| 208 |
ctx.Printf(1, 0, titleStyle, "%s", gp.title) |
| 209 |
var i int |
| 210 |
lines := strings.Split(gp.prompt, "\n") |
| 211 |
for i = range lines { |
| 212 |
ctx.Printf(1, 2+i, defaultStyle, "%s", lines[i]) |
| 213 |
} |
| 214 |
gp.selector.Draw(ctx.Subcontext(1, ctx.Height()-1, ctx.Width()-2, 1)) |
| 215 |
} |
| 216 |
|
| 217 |
func (gp *SelectorDialog) ContextWidth() (func(int) int, func(int) int) { |
| 218 |
// horizontal starting position in columns from the left |
| 219 |
start := func(int) int { |
| 220 |
return 4 |
| 221 |
} |
| 222 |
// dialog width from the starting column |
| 223 |
width := func(w int) int { |
| 224 |
return w - 8 |
| 225 |
} |
| 226 |
return start, width |
| 227 |
} |
| 228 |
|
| 229 |
func (gp *SelectorDialog) ContextHeight() (func(int) int, func(int) int) { |
| 230 |
totalHeight := 2 // title + empty line |
| 231 |
totalHeight += strings.Count(gp.prompt, "\n") + 1 |
| 232 |
totalHeight += 2 // empty line + selector |
| 233 |
start := func(h int) int { |
| 234 |
s := max(h/2-totalHeight/2, 0) |
| 235 |
return s |
| 236 |
} |
| 237 |
height := func(h int) int { |
| 238 |
if totalHeight > h { |
| 239 |
return h |
| 240 |
} else { |
| 241 |
return totalHeight |
| 242 |
} |
| 243 |
} |
| 244 |
return start, height |
| 245 |
} |
| 246 |
|
| 247 |
func (gp *SelectorDialog) Invalidate() { |
| 248 |
ui.Invalidate() |
| 249 |
} |
| 250 |
|
| 251 |
func (gp *SelectorDialog) Event(event vaxis.Event) bool { |
| 252 |
switch event := event.(type) { |
| 253 |
case vaxis.Key: |
| 254 |
switch { |
| 255 |
case event.Matches(vaxis.KeyEnter): |
| 256 |
gp.selector.Focus(false) |
| 257 |
gp.callback(gp.selector.Selected(), nil) |
| 258 |
case event.Matches(vaxis.KeyEsc): |
| 259 |
gp.selector.Focus(false) |
| 260 |
gp.callback("", ErrNoOptionSelected) |
| 261 |
default: |
| 262 |
gp.selector.Event(event) |
| 263 |
} |
| 264 |
default: |
| 265 |
gp.selector.Event(event) |
| 266 |
} |
| 267 |
return true |
| 268 |
} |
| 269 |
|
| 270 |
func (gp *SelectorDialog) Focus(f bool) { |
| 271 |
gp.selector.Focus(f) |
| 272 |
} |