suppress intermediate results unless -i, addresses #11, fix man (#13)

This commit is contained in:
T.v.Dein
2023-11-08 14:43:34 +01:00
committed by GitHub
parent 64e66e9d7b
commit fa5f8dcb3b
4 changed files with 56 additions and 23 deletions

25
calc.go
View File

@@ -33,6 +33,8 @@ type Calc struct {
batch bool
stdin bool
showstack bool
intermediate bool
notdone bool // set to true as long as there are items left in the eval loop
stack *Stack
history []string
completer readline.AutoCompleter
@@ -198,7 +200,15 @@ func (c *Calc) Eval(line string) {
return
}
for _, item := range c.Space.Split(line, -1) {
items := c.Space.Split(line, -1)
for pos, item := range items {
if pos+1 < len(items) {
c.notdone = true
} else {
c.notdone = false
}
num, err := strconv.ParseFloat(item, 64)
if err == nil {
@@ -379,11 +389,16 @@ func (c *Calc) History(format string, args ...any) {
// print the result
func (c *Calc) Result() float64 {
if !c.stdin {
fmt.Print("= ")
}
// we only print the result if it's either a final result or
// (if it is intermediate) if -i has been given
if c.intermediate || !c.notdone {
// only needed in repl
if !c.stdin {
fmt.Print("= ")
}
fmt.Println(c.stack.Last()[0])
fmt.Println(c.stack.Last()[0])
}
return c.stack.Last()[0]
}