diff --git a/README.md b/README.md index a72d2d5..ce175a3 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ An RPN calculator with a simple terminal interface - +![Basil](tiny.png) **Commands:** diff --git a/main.go b/main.go index cc3da00..a795dd9 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/ui/draw.go b/ui/draw.go index bf0227b..50dacac 100644 --- a/ui/draw.go +++ b/ui/draw.go @@ -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) { @@ -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 "..." @@ -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)) } diff --git a/ui/ui.go b/ui/ui.go index 13c32ce..65dd246 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -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 } @@ -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-- } } @@ -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) } } @@ -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 @@ -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)