2019-03-05 07:05:43 +00:00
|
|
|
package cui
|
|
|
|
|
|
|
|
import (
|
2019-04-28 19:46:47 +00:00
|
|
|
"bufio"
|
2019-03-05 07:05:43 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
2019-09-12 05:53:36 +00:00
|
|
|
var Shapes = map[string]string{
|
2019-09-17 02:38:07 +00:00
|
|
|
"walll": "╎",
|
|
|
|
"wallr": " ",
|
|
|
|
"ceiling": " ",
|
|
|
|
"floor": " ",
|
|
|
|
"tl": "╎",
|
|
|
|
"tr": " ",
|
|
|
|
"bl": "╎",
|
|
|
|
"br": " ",
|
|
|
|
"awalll": "▌",
|
|
|
|
"awallr": "▐",
|
|
|
|
"aceiling": "▀",
|
|
|
|
"afloor": "▄",
|
|
|
|
"atl": "▞",
|
|
|
|
"atr": "▜",
|
|
|
|
"abl": "▚",
|
|
|
|
"abr": "▟",
|
2019-03-05 07:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func drawShape(shape string) {
|
2019-09-12 05:53:36 +00:00
|
|
|
if val, ok := Shapes[shape]; ok {
|
2019-03-05 07:05:43 +00:00
|
|
|
fmt.Printf("%s", val)
|
|
|
|
} else {
|
|
|
|
fmt.Print("x")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func moveThenDrawShape(r, c int, s string) {
|
|
|
|
MoveCursorTo(r, c)
|
|
|
|
drawShape(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func MoveCursorTo(row, col int) {
|
|
|
|
fmt.Printf("\033[%d;%dH", row, col)
|
|
|
|
}
|
|
|
|
|
|
|
|
func moveCursorToward(dir string, amount int) {
|
|
|
|
directions := map[string]string{
|
2019-04-28 19:46:47 +00:00
|
|
|
"up": "A",
|
|
|
|
"down": "B",
|
|
|
|
"left": "D",
|
2019-03-05 07:05:43 +00:00
|
|
|
"right": "C",
|
|
|
|
}
|
|
|
|
|
|
|
|
if val, ok := directions[dir]; ok {
|
|
|
|
fmt.Printf("\033[%d%s", amount, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 02:42:38 +00:00
|
|
|
// Exit performs cleanup operations before exiting the application
|
2019-03-05 07:05:43 +00:00
|
|
|
func Exit() {
|
2019-10-24 02:25:31 +00:00
|
|
|
CleanupTerm()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2019-10-24 02:42:38 +00:00
|
|
|
// InitTerm sets the terminal modes appropriate for Bombadillo
|
2019-10-24 02:25:31 +00:00
|
|
|
func InitTerm() {
|
|
|
|
SetCharMode()
|
|
|
|
Tput("rmam") // turn off line wrapping
|
|
|
|
Tput("smcup") // use alternate screen
|
|
|
|
}
|
|
|
|
|
2019-10-24 02:42:38 +00:00
|
|
|
// CleanupTerm reverts changs to terminal mode made by InitTerm
|
2019-10-24 02:25:31 +00:00
|
|
|
func CleanupTerm() {
|
2019-03-05 07:05:43 +00:00
|
|
|
moveCursorToward("down", 500)
|
|
|
|
moveCursorToward("right", 500)
|
|
|
|
SetLineMode()
|
2019-05-01 00:18:28 +00:00
|
|
|
|
2019-03-05 07:05:43 +00:00
|
|
|
fmt.Print("\n")
|
2019-10-24 02:42:38 +00:00
|
|
|
fmt.Print("\033[?25h") // reenables cursor blinking
|
|
|
|
Tput("smam") // turn on line wrap
|
|
|
|
Tput("rmcup") // stop using alternate screen
|
2019-03-05 07:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Clear(dir string) {
|
|
|
|
directions := map[string]string{
|
2019-04-28 19:46:47 +00:00
|
|
|
"up": "\033[1J",
|
|
|
|
"down": "\033[0J",
|
|
|
|
"left": "\033[1K",
|
|
|
|
"right": "\033[0K",
|
|
|
|
"line": "\033[2K",
|
2019-03-05 07:05:43 +00:00
|
|
|
"screen": "\033[2J",
|
|
|
|
}
|
|
|
|
|
|
|
|
if val, ok := directions[dir]; ok {
|
|
|
|
fmt.Print(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func Getch() rune {
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
char, _, err := reader.ReadRune()
|
|
|
|
if err != nil {
|
|
|
|
return '@'
|
|
|
|
}
|
|
|
|
return char
|
|
|
|
}
|
|
|
|
|
2019-05-01 00:18:28 +00:00
|
|
|
func GetLine() (string, error) {
|
2019-03-05 07:05:43 +00:00
|
|
|
SetLineMode()
|
2019-05-01 00:18:28 +00:00
|
|
|
|
2019-03-05 07:05:43 +00:00
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
fmt.Print(": ")
|
2019-05-01 00:18:28 +00:00
|
|
|
text, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-03-05 07:05:43 +00:00
|
|
|
SetCharMode()
|
2019-05-01 00:18:28 +00:00
|
|
|
return text[:len(text)-1], nil
|
2019-03-05 07:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetCharMode() {
|
2019-03-19 03:11:02 +00:00
|
|
|
cmd := exec.Command("stty", "cbreak", "-echo")
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
2019-05-01 00:18:28 +00:00
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
2019-05-01 22:25:45 +00:00
|
|
|
panic(err)
|
2019-05-01 00:18:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 07:05:43 +00:00
|
|
|
fmt.Print("\033[?25l")
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetLineMode() {
|
2019-03-19 03:11:02 +00:00
|
|
|
cmd := exec.Command("stty", "-cbreak", "echo")
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
2019-05-01 22:25:45 +00:00
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-03-05 07:05:43 +00:00
|
|
|
}
|
2019-05-04 03:58:55 +00:00
|
|
|
|
2019-09-20 16:18:16 +00:00
|
|
|
func Tput(opt string) {
|
2019-05-04 03:58:55 +00:00
|
|
|
cmd := exec.Command("tput", opt)
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
2019-05-07 20:12:16 +00:00
|
|
|
// explicitly ignoring the error here as
|
|
|
|
// the alternate screen is an optional feature
|
|
|
|
// that may not be available everywhere we expect
|
|
|
|
// to run
|
|
|
|
_ = cmd.Run()
|
2019-05-04 03:58:55 +00:00
|
|
|
}
|