Skip to content

Commit

Permalink
Merge branch 'simon-anz-feature/value-expressions'
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Aug 29, 2021
2 parents fb79419 + 271cf90 commit 1f0f6d3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
23 changes: 16 additions & 7 deletions cointop/portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"
"unicode/utf8"

"github.com/Knetic/govaluate"
"github.com/miguelmota/cointop/pkg/asciitable"
"github.com/miguelmota/cointop/pkg/humanize"
"github.com/miguelmota/cointop/pkg/pad"
Expand Down Expand Up @@ -434,16 +435,24 @@ func (ct *Cointop) SetPortfolioHoldings() error {
return nil
}

value := normalizeFloatString(string(b), true)
shouldDelete := value == ""
var holdings float64

if !shouldDelete {
holdings, err = strconv.ParseFloat(value, 64)
var holdings float64 = 0
input := strings.TrimSpace(string(b[:n])) // remove trailing \0s
if input != "" {
expression, err := govaluate.NewEvaluableExpression(input)
if err != nil {
return err
return nil // invalid expression - don't change anything
}
result, err := expression.Evaluate(nil)
if err != nil {
return nil // could not evaluate - don't change anything
}
var ok bool
holdings, ok = result.(float64)
if !ok {
return nil // not a float64 - don't change anything
}
}
shouldDelete := holdings == 0

idx := ct.GetPortfolioCoinIndex(coin)
if err := ct.SetPortfolioEntry(coin.Name, holdings); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions docs/content/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ draft: false

Press <kbd>e</kbd> on the highlighted coin to enter holdings and add to your portfolio.

This dialog supports basic expressions including `+` `-` `*` etc.

## How do I edit the holdings of a coin in my portfolio?

Press <kbd>e</kbd> on the highlighted coin to edit the holdings.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module github.com/miguelmota/cointop

require (
github.com/BurntSushi/toml v0.3.1
github.com/Knetic/govaluate v3.0.0+incompatible // indirect
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/anaskhan96/soup v1.1.1 // indirect
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
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/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
Expand Down

0 comments on commit 1f0f6d3

Please sign in to comment.