util.go 480 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package diffview
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/charmbracelet/x/ansi"
  6. )
  7. func pad(v any, width int) string {
  8. s := fmt.Sprintf("%v", v)
  9. w := ansi.StringWidth(s)
  10. if w >= width {
  11. return s
  12. }
  13. return strings.Repeat(" ", width-w) + s
  14. }
  15. func isEven(n int) bool {
  16. return n%2 == 0
  17. }
  18. func isOdd(n int) bool {
  19. return !isEven(n)
  20. }
  21. func btoi(b bool) int {
  22. if b {
  23. return 1
  24. }
  25. return 0
  26. }
  27. func ternary[T any](cond bool, t, f T) T {
  28. if cond {
  29. return t
  30. }
  31. return f
  32. }