mirror of
https://github.com/br0xen/termbox-util.git
synced 2024-11-22 21:43:14 +00:00
Linted termbox_util.go
This commit is contained in:
parent
2dd65ed8b8
commit
3b810d6387
@ -1,4 +1,4 @@
|
|||||||
package termbox_util
|
package termboxUtil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -7,24 +7,31 @@ import (
|
|||||||
"github.com/nsf/termbox-go"
|
"github.com/nsf/termbox-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TextAlignment is an int value for how we're aligning text
|
||||||
type TextAlignment int
|
type TextAlignment int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ALIGN_LEFT = iota
|
// AlignLeft Aligns text to the left
|
||||||
ALIGN_CENTER
|
AlignLeft = iota
|
||||||
ALIGN_RIGHT
|
// AlignCenter Aligns text to the center
|
||||||
|
AlignCenter
|
||||||
|
// AlignRight Aligns text to the right
|
||||||
|
AlignRight
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Basic Output Helpers */
|
/* Basic Output Helpers */
|
||||||
|
|
||||||
|
// DrawStringAtPoint Draw a string of text at x, y with foreground color fg, background color bg
|
||||||
func DrawStringAtPoint(str string, x int, y int, fg termbox.Attribute, bg termbox.Attribute) (int, int) {
|
func DrawStringAtPoint(str string, x int, y int, fg termbox.Attribute, bg termbox.Attribute) (int, int) {
|
||||||
x_pos := x
|
xPos := x
|
||||||
for _, runeValue := range str {
|
for _, runeValue := range str {
|
||||||
termbox.SetCell(x_pos, y, runeValue, fg, bg)
|
termbox.SetCell(xPos, y, runeValue, fg, bg)
|
||||||
x_pos++
|
xPos++
|
||||||
}
|
}
|
||||||
return x_pos, y
|
return xPos, y
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FillWithChar Fills from x1,y1 through x2,y2 with the rune r, foreground color fg, background bg
|
||||||
func FillWithChar(r rune, x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) {
|
func FillWithChar(r rune, x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) {
|
||||||
for xx := x1; xx <= x2; xx++ {
|
for xx := x1; xx <= x2; xx++ {
|
||||||
for yx := y1; yx <= y2; yx++ {
|
for yx := y1; yx <= y2; yx++ {
|
||||||
@ -33,6 +40,7 @@ func FillWithChar(r rune, x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.A
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DrawBorder Draw a border around the area inside x1,y1 -> x2, y2
|
||||||
func DrawBorder(x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) {
|
func DrawBorder(x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute) {
|
||||||
termbox.SetCell(x1, y1, '┌', fg, bg)
|
termbox.SetCell(x1, y1, '┌', fg, bg)
|
||||||
FillWithChar('─', x1+1, y1, x2-1, y1, fg, bg)
|
FillWithChar('─', x1+1, y1, x2-1, y1, fg, bg)
|
||||||
@ -46,22 +54,22 @@ func DrawBorder(x1, y1, x2, y2 int, fg termbox.Attribute, bg termbox.Attribute)
|
|||||||
termbox.SetCell(x2, y2, '┘', fg, bg)
|
termbox.SetCell(x2, y2, '┘', fg, bg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AlignText Aligns the text txt within width characters using the specified alignment
|
||||||
func AlignText(txt string, width int, align TextAlignment) string {
|
func AlignText(txt string, width int, align TextAlignment) string {
|
||||||
num_spaces := width - len(txt)
|
numSpaces := width - len(txt)
|
||||||
switch align {
|
switch align {
|
||||||
case ALIGN_CENTER:
|
case AlignCenter:
|
||||||
if num_spaces/2 > 0 {
|
if numSpaces/2 > 0 {
|
||||||
return fmt.Sprintf("%s%s%s",
|
return fmt.Sprintf("%s%s%s",
|
||||||
strings.Repeat(" ", num_spaces/2),
|
strings.Repeat(" ", numSpaces/2),
|
||||||
txt, strings.Repeat(" ", num_spaces/2),
|
txt, strings.Repeat(" ", numSpaces/2),
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
return txt
|
|
||||||
}
|
}
|
||||||
case ALIGN_RIGHT:
|
return txt
|
||||||
return fmt.Sprintf("%s%s", strings.Repeat(" ", num_spaces), txt)
|
case AlignRight:
|
||||||
|
return fmt.Sprintf("%s%s", strings.Repeat(" ", numSpaces), txt)
|
||||||
default:
|
default:
|
||||||
return fmt.Sprintf("%s%s", txt, strings.Repeat(" ", num_spaces))
|
return fmt.Sprintf("%s%s", txt, strings.Repeat(" ", numSpaces))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user