/
selection.go
52 lines (45 loc) · 1.45 KB
/
selection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package group
import (
"fmt"
"sort"
"github.com/orbatschow/kontext/pkg/config"
"github.com/pterm/pterm"
"github.com/samber/lo"
)
// start an interactive group selection
func (c *Client) buildInteractiveSelectPrinter() (*pterm.InteractiveSelectPrinter, error) {
// compute all selection options
var keys []string
for _, value := range c.Config.Group.Items {
keys = append(keys, value.Name)
}
// sort the selection
switch c.Config.Group.Selection.Sort {
case SortAsc:
sort.Strings(keys)
case SortDesc:
sort.Sort(sort.Reverse(sort.StringSlice(keys)))
}
selector := pterm.DefaultInteractiveSelect.
WithMaxHeight(MaxSelectHeight).
WithOptions(keys)
// check if there are defaults for the selection and set them accordingly
switch c.Config.Group.Selection.Default {
// if the default is empty, return without setting default option
case "":
return selector, nil
// if the default select is "-", set the current group as the default selection option
case "-":
return selector.WithDefaultOption(c.State.Group.Active), nil
// search for the given default selection group
default:
// get the default selection group
_, ok := lo.Find(c.Config.Group.Items, func(item config.GroupItem) bool {
return item.Name == c.Config.Group.Selection.Default
})
if !ok {
return nil, fmt.Errorf("could not find default selection group: '%s'", c.State.Group.Active)
}
return selector.WithDefaultOption(c.Config.Group.Selection.Default), nil
}
}