text.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package text
  2. import (
  3. "fmt"
  4. "strings"
  5. "unicode"
  6. "github.com/leonelquinteros/gotext"
  7. )
  8. // SplitDBFromName split apart db/package to db and package.
  9. func SplitDBFromName(pkg string) (db, name string) {
  10. split := strings.SplitN(pkg, "/", 2)
  11. if len(split) == 2 {
  12. return split[0], split[1]
  13. }
  14. return "", split[0]
  15. }
  16. // LessRunes compares two rune values, and returns true if the first argument is lexicographicaly smaller.
  17. func LessRunes(iRunes, jRunes []rune) bool {
  18. max := len(iRunes)
  19. if max > len(jRunes) {
  20. max = len(jRunes)
  21. }
  22. for idx := 0; idx < max; idx++ {
  23. ir := iRunes[idx]
  24. jr := jRunes[idx]
  25. lir := unicode.ToLower(ir)
  26. ljr := unicode.ToLower(jr)
  27. if lir != ljr {
  28. return lir < ljr
  29. }
  30. // the lowercase runes are the same, so compare the original
  31. if ir != jr {
  32. return ir < jr
  33. }
  34. }
  35. return len(iRunes) < len(jRunes)
  36. }
  37. // ContinueTask prompts if user wants to continue task.
  38. // If NoConfirm is set the action will continue without user input.
  39. func ContinueTask(s string, cont, noConfirm bool) bool {
  40. if noConfirm {
  41. return cont
  42. }
  43. var (
  44. response string
  45. postFix string
  46. yes = gotext.Get("yes")
  47. no = gotext.Get("no")
  48. y = string([]rune(yes)[0]) // nolint
  49. n = string([]rune(no)[0]) // nolint
  50. )
  51. if cont {
  52. postFix = fmt.Sprintf(" [%s/%s] ", strings.ToUpper(y), n)
  53. } else {
  54. postFix = fmt.Sprintf(" [%s/%s] ", y, strings.ToUpper(n))
  55. }
  56. Info(Bold(s), Bold(postFix))
  57. if _, err := fmt.Scanln(&response); err != nil {
  58. return cont
  59. }
  60. response = strings.ToLower(response)
  61. return response == yes || response == y
  62. }