Skip to content

Commit

Permalink
feat(math): support % expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
ventsislav-georgiev committed Jan 13, 2024
1 parent 96da864 commit 6f6e235
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ replace (

require (
fyne.io/fyne/v2 v2.3.5
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible
github.com/Knetic/govaluate v3.0.0+incompatible
github.com/bcicen/go-units v1.0.5
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20231124074035-2de0cf0c80af
github.com/groob/plist v0.0.0-20220217120414-63fa881b19a5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ fyne.io/systray v1.10.1-0.20231115130155-104f5ef7839e/go.mod h1:oM2AQqGJ1AMo4nNq
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1:1G1pk05UrOh0NlF1oeaaix1x8XzrfjIDK47TY0Zehcw=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/Knetic/govaluate v3.0.0+incompatible h1:7o6+MAPhYTCF0+fdvoz1xDedhRb4f6s9Tn1Tt7/WTEg=
github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/akavel/rsrc v0.10.2/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
Expand Down
5 changes: 5 additions & 0 deletions pkg/currency/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (
)

func Eval(expr string) (s string, icon []byte, onEnter func(), err error) {
isDigit := expr[0] >= 48 && expr[0] <= 57
if !isDigit {
return "", nil, nil, helpers.ErrSkip
}

parts := strings.Split(strings.ToLower(expr), " ")
if len(parts) != 4 {
return "", nil, nil, helpers.ErrSkip
Expand Down
5 changes: 5 additions & 0 deletions pkg/mathexpr/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mathexpr
import (
"fmt"
"strconv"
"strings"

"github.com/Knetic/govaluate"
"github.com/ventsislav-georgiev/prosper/pkg/helpers"
Expand All @@ -14,6 +15,10 @@ func Eval(expr string) (s string, icon []byte, onEnter func(), err error) {
return "", nil, nil, helpers.ErrSkip
}

expr = strings.ReplaceAll(expr, " % ", "__MOD__")
expr = strings.ReplaceAll(expr, "%", "/100")
expr = strings.ReplaceAll(expr, "__MOD__", "%")

e, err := govaluate.NewEvaluableExpression(expr)
if err != nil {
return "", nil, nil, err
Expand Down
5 changes: 5 additions & 0 deletions pkg/translate/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
)

func Eval(expr string) (s string, icon []byte, onEnter func(), err error) {
isDigit := expr[0] >= 48 && expr[0] <= 57
if isDigit || expr[0] != '(' {
return "", nil, nil, helpers.ErrSkip
}

parts := strings.Split(strings.ToLower(expr), " ")
if len(parts) < 3 {
return "", nil, nil, helpers.ErrSkip
Expand Down
5 changes: 5 additions & 0 deletions pkg/units/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
)

func Eval(expr string) (s string, icon []byte, onEnter func(), err error) {
isDigit := expr[0] >= 48 && expr[0] <= 57
if !isDigit {
return "", nil, nil, helpers.ErrSkip
}

parts := strings.Split(strings.ToLower(expr), " ")
if len(parts) != 4 {
return "", nil, nil, helpers.ErrSkip
Expand Down

0 comments on commit 6f6e235

Please sign in to comment.