text.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 response string
  44. var postFix string
  45. yes := gotext.Get("yes")
  46. no := gotext.Get("no")
  47. y := string([]rune(yes)[0])
  48. n := string([]rune(no)[0])
  49. if cont {
  50. postFix = fmt.Sprintf(" [%s/%s] ", strings.ToUpper(y), n)
  51. } else {
  52. postFix = fmt.Sprintf(" [%s/%s] ", y, strings.ToUpper(n))
  53. }
  54. Info(Bold(s), Bold(postFix))
  55. if _, err := fmt.Scanln(&response); err != nil {
  56. return cont
  57. }
  58. response = strings.ToLower(response)
  59. return response == yes || response == y
  60. }