scroll_window.go 803 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package at
  3. import (
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. )
  8. func parse_scroll_amount(amt string) ([]any, error) {
  9. var ans = make([]any, 2)
  10. if amt == "start" || amt == "end" {
  11. ans[0] = amt
  12. ans[1] = nil
  13. } else {
  14. pages := strings.Contains(amt, "p")
  15. unscroll := strings.Contains(amt, "u")
  16. var mult float64 = 1
  17. if strings.HasSuffix(amt, "-") && !unscroll {
  18. mult = -1
  19. }
  20. q, err := strconv.ParseFloat(strings.TrimRight(amt, "+-plu"), 64)
  21. if err != nil {
  22. return ans, err
  23. }
  24. if !pages && q != float64(int(q)) {
  25. return ans, fmt.Errorf("The number must be an integer")
  26. }
  27. ans[0] = q * mult
  28. if pages {
  29. ans[1] = "p"
  30. } else if unscroll {
  31. ans[1] = "u"
  32. } else {
  33. ans[1] = "l"
  34. }
  35. }
  36. return ans, nil
  37. }