Skip to content

Commit

Permalink
added -w flag
Browse files Browse the repository at this point in the history
  • Loading branch information
geremachek committed Sep 19, 2021
1 parent 69f7241 commit eaa3abb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

An RPN calculator with a simple terminal interface

<img src="tiny.png"></img>
![Basil](tiny.png)

**Commands:**

Expand Down
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import (
)

func main() {
var height int
var (
height int
width int
)

pflag.IntVarP(&height, "height", "H", 10, "set how many elements are displayed at once")
pflag.IntVarP(&width, "width", "w", 30, "set the width of the stack window")
pflag.Parse()

// start the ui, trapping errors

if u, e := ui.NewUi(height); e == nil {
if u, e := ui.NewUi(height, width); e == nil {
if err := u.Start(); err != nil {
printErr(e)
}
Expand Down
18 changes: 2 additions & 16 deletions ui/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@ package ui

import (
"fmt"
"strings"
"strconv"
"github.com/gdamore/tcell"
)

const width = 30

var (
strw = strconv.Itoa(width)
bar = strings.Repeat("─", width) // barrier decoration string
)

// draw text to the screen

func addstr(s tcell.Screen, style tcell.Style, x int, y int, text string) {
Expand All @@ -24,15 +16,9 @@ func addstr(s tcell.Screen, style tcell.Style, x int, y int, text string) {
}
}

// draw the barrier decoration...

func drawLine(s tcell.Screen, x, y int) {
addstr(s, tcell.StyleDefault, x, y, bar)
}

// allign and trim our text

func drawAligned(s tcell.Screen, x, y int, text string) {
func drawAligned(s tcell.Screen, x, y, width int, text string) {
disp := text

// if there isn't enough room, add "..."
Expand All @@ -41,5 +27,5 @@ func drawAligned(s tcell.Screen, x, y int, text string) {
disp = disp[:l - (l - width) - 3] + "..."
}

addstr(s, tcell.StyleDefault, x, y, fmt.Sprintf("%" + strw + "s", disp))
addstr(s, tcell.StyleDefault, x, y, fmt.Sprintf("%" + strconv.Itoa(width) + "s", disp))
}
25 changes: 17 additions & 8 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ import (
"github.com/geremachek/basil/stack"
)

var clearLine = strings.Repeat(" ", width)

type ui struct {
stack *stack.Stack
buff *lineBuff

scr tcell.Screen

height int
width int

clearLine string

running bool
}

// create a new ui struct

func NewUi(h int) (*ui, error) {
func NewUi(h, w int) (*ui, error) {
if s, err := tcell.NewScreen(); err == nil {
return &ui { stack.NewStack(), newLineBuff(0, h+2), s, h, true }, nil
return &ui { stack.NewStack(), newLineBuff(0, h+2), s, h, w, strings.Repeat(" ", w), true }, nil
} else {
return &ui{}, err
}
Expand All @@ -35,7 +38,7 @@ func (u *ui) drawStackWindow() {
y := u.height-1

for i := u.stack.Size()-1; i >= 0 && y >= 0; i-- {
drawAligned(u.scr, 0, y, strconv.FormatFloat(u.stack.Get(i), 'f', -1, 64))
drawAligned(u.scr, 0, y, u.width, strconv.FormatFloat(u.stack.Get(i), 'f', -1, 64))
y--
}
}
Expand All @@ -45,7 +48,7 @@ func (u *ui) drawStackWindow() {

func (u *ui) clearStackWindow(lines int) {
for y := u.height-1; y >= u.height-lines; y-- {
addstr(u.scr, tcell.StyleDefault, 0, y, clearLine)
addstr(u.scr, tcell.StyleDefault, 0, y, u.clearLine)
}
}

Expand All @@ -60,7 +63,13 @@ func (u ui) drawAngleMode() {
m = 'R'
}

u.scr.SetContent(width+1, u.height, m, []rune(""), tcell.StyleDefault)
u.scr.SetContent(u.width+1, u.height, m, []rune(""), tcell.StyleDefault)
}

// draw the barrier decoration...

func (u ui) drawLine() {
addstr(u.scr, tcell.StyleDefault, 0, u.height, strings.Repeat("─", u.width))
}

// match keys with actions
Expand Down Expand Up @@ -100,7 +109,7 @@ func (u *ui) Start() error {
if e := u.scr.Init(); e == nil {
// draw to the screen

drawLine(u.scr, 0, u.height)
u.drawLine()
u.drawAngleMode()

u.scr.ShowCursor(0, u.height + 2)
Expand Down

0 comments on commit eaa3abb

Please sign in to comment.