104 lines
1.9 KiB
Go
104 lines
1.9 KiB
Go
package tui
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
)
|
|
|
|
// Clear clears the screen.
|
|
func Clear() {
|
|
_, h := termSize()
|
|
buf := make([]byte, 2*h)
|
|
for i := range buf {
|
|
buf[i] = '\n'
|
|
}
|
|
mustWrite(buf)
|
|
}
|
|
|
|
// Line prints a line across the screen followed by a newline.
|
|
func PrintLine() {
|
|
w, _ := termSize()
|
|
|
|
runes := make([]rune, w)
|
|
for i := range runes {
|
|
runes[i] = '━'
|
|
}
|
|
|
|
runes[0] = '┅'
|
|
runes[len(runes)-2] = '┅'
|
|
runes[len(runes)-1] = '\n'
|
|
|
|
buf := []byte(string(runes))
|
|
mustWrite(buf)
|
|
}
|
|
|
|
// Prints a box around text followed by a newline.
|
|
func PrintBoxed(s string, args ...interface{}) {
|
|
if len(args) > 0 {
|
|
s = fmt.Sprintf(s, args...)
|
|
}
|
|
|
|
w, _ := termSize()
|
|
|
|
lines, textWidth := wrapText(s, w-4)
|
|
|
|
if w < textWidth+4 {
|
|
w = textWidth + 4
|
|
}
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
// Top of box.
|
|
buf.Write([]byte("╭"))
|
|
for i := 0; i < w-2; i++ {
|
|
buf.Write([]byte("─"))
|
|
}
|
|
buf.Write([]byte("╮\n"))
|
|
|
|
// Lines.
|
|
for _, line := range lines {
|
|
buf.Write([]byte("│ "))
|
|
buf.Write([]byte(line))
|
|
for i := len([]rune(line)); i < w-4; i++ {
|
|
buf.Write([]byte(" "))
|
|
}
|
|
buf.Write([]byte(" │\n"))
|
|
}
|
|
|
|
// Bottom of box.
|
|
buf.Write([]byte("╰"))
|
|
for i := 0; i < w-2; i++ {
|
|
buf.Write([]byte("─"))
|
|
}
|
|
buf.Write([]byte("╯\n"))
|
|
|
|
mustWrite(buf.Bytes())
|
|
}
|
|
|
|
// Print a string to the terminal followed by a newline. No wrapping.
|
|
func PrintString(s string, args ...interface{}) {
|
|
if len(args) > 0 {
|
|
s = fmt.Sprintf(s, args...)
|
|
}
|
|
mustWrite([]byte(s + "\n"))
|
|
}
|
|
|
|
// Format and wrap text, print to screen with given prefix on each wrapped
|
|
// line.
|
|
func PrintTextWithPrefix(prefix, s string, args ...interface{}) {
|
|
if len(args) > 0 {
|
|
s = fmt.Sprintf(s, args...)
|
|
}
|
|
indent := len([]rune(prefix))
|
|
//sIndent := strings.Repeat(" ", indent)
|
|
w, _ := termSize()
|
|
lines, _ := wrapText(s, w-1-indent)
|
|
for _, line := range lines {
|
|
PrintString(prefix + line)
|
|
}
|
|
}
|
|
|
|
func PrintText(s string, args ...interface{}) {
|
|
PrintTextWithPrefix("", s, args...)
|
|
}
|