2015-10-21 16:51:22 +00:00
|
|
|
package termboxUtil
|
2015-05-02 03:15:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2015-10-21 16:51:22 +00:00
|
|
|
|
|
|
|
"github.com/nsf/termbox-go"
|
2015-05-02 03:15:36 +00:00
|
|
|
)
|
|
|
|
|
2015-10-21 16:51:22 +00:00
|
|
|
// TextAlignment is an int value for how we're aligning text
|
2015-05-02 03:15:36 +00:00
|
|
|
type TextAlignment int
|
|
|
|
|
|
|
|
const (
|
2015-10-21 16:51:22 +00:00
|
|
|
// AlignLeft Aligns text to the left
|
|
|
|
AlignLeft = iota
|
|
|
|
// AlignCenter Aligns text to the center
|
|
|
|
AlignCenter
|
|
|
|
// AlignRight Aligns text to the right
|
|
|
|
AlignRight
|
2015-05-02 03:15:36 +00:00
|
|
|
)
|
|
|
|
|
2015-12-26 16:46:18 +00:00
|
|
|
/* Basic Input Helpers */
|
|
|
|
|
|
|
|
// KeyIsAlphaNumeric Returns whether the termbox event is an
|
|
|
|
// Alpha-Numeric Key Press
|
2015-12-29 04:19:08 +00:00
|
|
|
func KeyIsAlphaNumeric(event termbox.Event) bool {
|
2016-01-14 03:17:08 +00:00
|
|
|
return KeyIsAlpha(event) || KeyIsNumeric(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyIsAlpha Returns whether the termbox event is a
|
|
|
|
// alphabetic Key press
|
|
|
|
func KeyIsAlpha(event termbox.Event) bool {
|
|
|
|
k := event.Ch
|
|
|
|
if (k >= 'a' && k <= 'z') || (k >= 'A' && k <= 'Z') {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyIsNumeric Returns whether the termbox event is a
|
|
|
|
// numeric Key press
|
|
|
|
func KeyIsNumeric(event termbox.Event) bool {
|
2015-12-26 16:46:18 +00:00
|
|
|
k := event.Ch
|
2016-01-14 03:17:08 +00:00
|
|
|
if k >= '0' && k <= '9' {
|
2015-12-26 16:46:18 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-01-14 03:17:08 +00:00
|
|
|
// KeyIsSymbol Returns whether the termbox event is a
|
|
|
|
// symbol Key press
|
|
|
|
func KeyIsSymbol(event termbox.Event) bool {
|
|
|
|
symbols := []rune{'!', '@', '#', '$', '%', '^', '&', '*',
|
|
|
|
'(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '|',
|
|
|
|
';', ':', '"', '\'', ',', '<', '.', '>', '/', '?', '`', '~'}
|
|
|
|
k := event.Ch
|
|
|
|
for i := range symbols {
|
|
|
|
if k == symbols[i] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-02 03:15:36 +00:00
|
|
|
/* Basic Output Helpers */
|
2015-10-21 16:51:22 +00:00
|
|
|
|
|
|
|
// DrawStringAtPoint Draw a string of text at x, y with foreground color fg, background color bg
|
2015-05-02 03:15:36 +00:00
|
|
|
func DrawStringAtPoint(str string, x int, y int, fg termbox.Attribute, bg termbox.Attribute) (int, int) {
|
2015-10-21 16:51:22 +00:00
|
|
|
xPos := x
|
2015-05-02 03:15:36 +00:00
|
|
|
for _, runeValue := range str {
|
2015-10-21 16:51:22 +00:00
|
|
|
termbox.SetCell(xPos, y, runeValue, fg, bg)
|
|
|
|
xPos++
|
2015-05-02 03:15:36 +00:00
|
|
|
}
|
2015-10-21 16:51:22 +00:00
|
|
|
return xPos, y
|
2015-05-02 03:15:36 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 16:51:22 +00:00
|
|
|
// FillWithChar Fills from x1,y1 through x2,y2 with the rune r, foreground color fg, background bg
|
2015-05-02 03:15:36 +00:00
|
|
|
func FillWithChar(r rune, x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) {
|
|
|
|
for xx := x1; xx <= x2; xx++ {
|
|
|
|
for yx := y1; yx <= y2; yx++ {
|
|
|
|
termbox.SetCell(xx, yx, r, fg, bg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-21 16:51:22 +00:00
|
|
|
// DrawBorder Draw a border around the area inside x1,y1 -> x2, y2
|
2015-05-02 03:15:36 +00:00
|
|
|
func DrawBorder(x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) {
|
2015-10-26 12:39:18 +00:00
|
|
|
termbox.SetCell(x1, y1, '+', fg, bg)
|
|
|
|
FillWithChar('-', x1+1, y1, x2-1, y1, fg, bg)
|
|
|
|
termbox.SetCell(x2, y1, '+', fg, bg)
|
2015-05-04 22:42:35 +00:00
|
|
|
|
|
|
|
FillWithChar('|', x1, y1+1, x1, y2-1, fg, bg)
|
|
|
|
FillWithChar('|', x2, y1+1, x2, y2-1, fg, bg)
|
|
|
|
|
2015-10-26 12:39:18 +00:00
|
|
|
termbox.SetCell(x1, y2, '+', fg, bg)
|
|
|
|
FillWithChar('-', x1+1, y2, x2-1, y2, fg, bg)
|
|
|
|
termbox.SetCell(x2, y2, '+', fg, bg)
|
2015-05-02 03:15:36 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 16:51:22 +00:00
|
|
|
// AlignText Aligns the text txt within width characters using the specified alignment
|
2015-05-02 03:15:36 +00:00
|
|
|
func AlignText(txt string, width int, align TextAlignment) string {
|
2015-10-21 16:51:22 +00:00
|
|
|
numSpaces := width - len(txt)
|
2015-05-02 03:15:36 +00:00
|
|
|
switch align {
|
2015-10-21 16:51:22 +00:00
|
|
|
case AlignCenter:
|
|
|
|
if numSpaces/2 > 0 {
|
2015-05-12 21:05:47 +00:00
|
|
|
return fmt.Sprintf("%s%s%s",
|
2015-10-21 16:51:22 +00:00
|
|
|
strings.Repeat(" ", numSpaces/2),
|
|
|
|
txt, strings.Repeat(" ", numSpaces/2),
|
2015-05-12 21:05:47 +00:00
|
|
|
)
|
|
|
|
}
|
2015-10-21 16:51:22 +00:00
|
|
|
return txt
|
|
|
|
case AlignRight:
|
|
|
|
return fmt.Sprintf("%s%s", strings.Repeat(" ", numSpaces), txt)
|
2015-05-02 03:15:36 +00:00
|
|
|
default:
|
2015-10-26 22:38:49 +00:00
|
|
|
if numSpaces >= 0 {
|
|
|
|
return fmt.Sprintf("%s%s", txt, strings.Repeat(" ", numSpaces))
|
|
|
|
}
|
|
|
|
return txt
|
2015-05-02 03:15:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-21 16:51:22 +00:00
|
|
|
/* More advanced things are in their respective files */
|