-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.go
More file actions
41 lines (37 loc) · 828 Bytes
/
ui.go
File metadata and controls
41 lines (37 loc) · 828 Bytes
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
package main
import (
"fmt"
"time"
"github.com/manifoldco/promptui"
)
func InteractiveSelect[T any](items []T, formatter func(item T) string) (int, error) {
strItem := make([]string, len(items))
for i := range items {
strItem[i] = formatter(items[i])
}
listSize := len(strItem)
if listSize > 10 {
listSize = 10
}
prompt := promptui.Select{
Label: "Select Node",
Items: strItem,
Size: listSize,
}
i, _, err := prompt.Run()
if err != nil {
return -1, err
}
return i, nil
}
func FormatDuration(d time.Duration) string {
if d > 24*time.Hour {
return fmt.Sprintf("%dd", int(d.Hours()/24))
} else if d >= time.Hour {
return fmt.Sprintf("%dh", int(d.Hours()))
} else if d >= time.Minute {
return fmt.Sprintf("%dm", int(d.Minutes()))
} else {
return fmt.Sprintf("%ds", int(d.Seconds()))
}
}