yay/pkg/text/text.go

79 lines
1.6 KiB
Go
Raw Normal View History

2020-07-10 00:36:45 +00:00
package text
import (
2020-07-10 22:48:30 +00:00
"fmt"
2020-07-10 00:36:45 +00:00
"strings"
"unicode"
2020-07-10 22:48:30 +00:00
"github.com/leonelquinteros/gotext"
2020-07-10 00:36:45 +00:00
)
2021-08-11 18:13:28 +00:00
// SplitDBFromName split apart db/package to db and package.
2020-07-10 00:36:45 +00:00
func SplitDBFromName(pkg string) (db, name string) {
split := strings.SplitN(pkg, "/", 2)
if len(split) == 2 {
return split[0], split[1]
}
2021-08-11 18:13:28 +00:00
2020-07-10 00:36:45 +00:00
return "", split[0]
}
// LessRunes compares two rune values, and returns true if the first argument is lexicographicaly smaller.
func LessRunes(iRunes, jRunes []rune) bool {
max := len(iRunes)
if max > len(jRunes) {
max = len(jRunes)
}
for idx := 0; idx < max; idx++ {
ir := iRunes[idx]
jr := jRunes[idx]
lir := unicode.ToLower(ir)
ljr := unicode.ToLower(jr)
if lir != ljr {
return lir < ljr
}
// the lowercase runes are the same, so compare the original
if ir != jr {
return ir < jr
}
}
return len(iRunes) < len(jRunes)
}
2020-07-10 22:48:30 +00:00
// ContinueTask prompts if user wants to continue task.
// If NoConfirm is set the action will continue without user input.
func ContinueTask(s string, cont, noConfirm bool) bool {
if noConfirm {
return cont
}
2021-08-11 18:13:28 +00:00
var (
response string
postFix string
yes = gotext.Get("yes")
no = gotext.Get("no")
2021-12-18 21:37:50 +00:00
y = string([]rune(yes)[0]) // nolint
n = string([]rune(no)[0]) // nolint
2021-08-11 18:13:28 +00:00
)
2020-07-10 22:48:30 +00:00
if cont {
postFix = fmt.Sprintf(" [%s/%s] ", strings.ToUpper(y), n)
} else {
postFix = fmt.Sprintf(" [%s/%s] ", y, strings.ToUpper(n))
}
Info(Bold(s), Bold(postFix))
if _, err := fmt.Scanln(&response); err != nil {
return cont
}
return strings.EqualFold(response, yes) || strings.EqualFold(response, y)
2020-07-10 22:48:30 +00:00
}