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 61cca28
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ replace (

require (
fyne.io/fyne/v2 v2.3.5
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible
github.com/bcicen/go-units v1.0.5
github.com/expr-lang/expr v1.15.8
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20231124074035-2de0cf0c80af
github.com/groob/plist v0.0.0-20220217120414-63fa881b19a5
github.com/hashicorp/golang-lru v1.0.2
Expand All @@ -32,6 +32,7 @@ require (

require (
fyne.io/systray v1.10.1-0.20231115130155-104f5ef7839e // indirect
github.com/Knetic/govaluate v3.0.0+incompatible // indirect
github.com/bcicen/bfstree v1.0.0 // indirect
github.com/blacktop/lzfse-cgo v1.1.19 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
6 changes: 4 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 Expand Up @@ -79,6 +79,8 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/expr-lang/expr v1.15.8 h1:FL8+d3rSSP4tmK9o+vKfSMqqpGL8n15pEPiHcnBpxoI=
github.com/expr-lang/expr v1.15.8/go.mod h1:uCkhfG+x7fcZ5A5sXHKuQ07jGZRl6J0FCAaf2k4PtVQ=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/felixge/fgprof v0.9.3 h1:VvyZxILNuCiUCSXtPtYmmtGvb65nqXh2QFWc0Wpf2/g=
github.com/felixge/fgprof v0.9.3/go.mod h1:RdbpDgzqYVh/T9fPELJyV7EYJuHB55UTEULNun8eiPw=
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
11 changes: 8 additions & 3 deletions pkg/mathexpr/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package mathexpr
import (
"fmt"
"strconv"
"strings"

"github.com/Knetic/govaluate"
exprlang "github.com/expr-lang/expr"
"github.com/ventsislav-georgiev/prosper/pkg/helpers"
)

Expand All @@ -14,12 +15,16 @@ func Eval(expr string) (s string, icon []byte, onEnter func(), err error) {
return "", nil, nil, helpers.ErrSkip
}

e, err := govaluate.NewEvaluableExpression(expr)
expr = strings.ReplaceAll(expr, " % ", "__MOD__")
expr = strings.ReplaceAll(expr, "%", "/100")
expr = strings.ReplaceAll(expr, "__MOD__", "%")

program, err := exprlang.Compile(expr)
if err != nil {
return "", nil, nil, err
}

r, err := e.Eval(nil)
r, err := exprlang.Run(program, nil)
if err != nil {
return err.Error(), nil, nil, nil
}
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 61cca28

Please sign in to comment.