diff options
| -rw-r--r-- | calc.go | 12 | ||||
| -rw-r--r-- | func.go | 18 | ||||
| -rw-r--r-- | ui.go | 12 |
3 files changed, 20 insertions, 22 deletions
@@ -5,21 +5,21 @@ import "strconv" type AngleMode bool const ( - modeDeg = false - modeRad = true + degrees AngleMode = false + radians AngleMode = true ) func (a AngleMode) String() string { - if a == modeDeg { + if a == degrees { return "deg" } return "rad" } type Calculator struct { - stack Stack - buf string - anglem AngleMode + stack Stack + buf string + angleMode AngleMode } // swap swaps the values of the buffer and the bottom element of the stack. If @@ -31,8 +31,8 @@ func trig(fn string) func(Calculator) { } v := &c.stack[len(c.stack)-1] // The math package expects arguments to trig functions to be in radians. - if c.anglem == modeDeg { - *v = radians(*v) + if c.angleMode == degrees { + *v = degToRad(*v) } switch fn { case "sin": @@ -65,8 +65,8 @@ func invTrig(fn string) func(Calculator) { default: panic(fmt.Sprintf("invalid inverse trig function: '%s'", fn)) } - if c.anglem == modeDeg { - *v = degrees(*v) + if c.angleMode == degrees { + *v = radToDeg(*v) } } } @@ -74,14 +74,14 @@ func invTrig(fn string) func(Calculator) { // Convert radians to degrees. func deg(c Calculator) { if n, err := c.stack.pop(); err == nil { - c.stack.push(degrees(n)) + c.stack.push(radToDeg(n)) } } // Convert degrees to radians. func rad(c Calculator) { if n, err := c.stack.pop(); err == nil { - c.stack.push(radians(n)) + c.stack.push(degToRad(n)) } } @@ -98,13 +98,11 @@ func fac(c Calculator) { c.stack.push(float64(factorial(uint(n)))) } -// radians converts degrees to radians. -func radians(deg float64) float64 { +func degToRad(deg float64) (rad float64) { return deg * math.Pi / 180.0 } -// degrees converts radians to degrees. -func degrees(rad float64) float64 { +func radToDeg(rad float64) (deg float64) { return rad * 180 / math.Pi } @@ -49,7 +49,7 @@ func (ui UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) { ui.calc.buf = "" ui.calc.stack = ui.calc.stack[:0] case "A": - ui.calc.anglem = !ui.calc.anglem + ui.calc.angleMode = !ui.calc.angleMode case "N": ui.calc.negate() case "+", "-", "*", "/", "%", "^": @@ -81,7 +81,7 @@ func (ui UI) View() string { width := min(ui.width, maxWidth) // Angle mode. - s += fmt.Sprintf("%*s\n", width-1, ui.calc.anglem) + s += fmt.Sprintf("%*s\n", width-1, ui.calc.angleMode) // Stack. top := boxTop(width) @@ -101,11 +101,11 @@ func (ui UI) View() string { func padding(ui UI) string { var ( // Number of lines occupied by each ui element. - anglem = 1 - stack = len(ui.calc.stack) + 2 - buf = 3 + angleMode = 1 + stack = len(ui.calc.stack) + 2 + buf = 3 ) - padlines := ui.height - anglem - stack - buf + padlines := ui.height - angleMode - stack - buf if padlines < 1 { return "" } |