Skip to content

Instantly share code, notes, and snippets.

@metaory
Created January 6, 2025 01:05
Show Gist options
  • Save metaory/114e9feb4daa767ae2ee9e6286af1994 to your computer and use it in GitHub Desktop.
Save metaory/114e9feb4daa767ae2ee9e6286af1994 to your computer and use it in GitHub Desktop.
TUI Basic

TUI Basic

go run basic.go
package main
import (
"bufio"
"fmt"
"os"
"strings"
"time"
)
const (
hide = "\033[?25l"
show = "\033[?25h"
save = "\033[s"
load = "\033[u"
top = "\033[1;0H"
clear = "\033[2K"
cyan = "\033[36m"
reset = "\033[0m"
)
func main() {
stop := make(chan bool)
fmt.Print(hide, save, "\n\n", load) // init screen
// Animation in goroutine
go func() {
f := []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
for i := 0; ; i = (i + 1) % len(f) {
select {
case <-stop:
return
default:
fmt.Printf("%s%s%s%s%s Loading...%s", save, top, clear, cyan, f[i], load)
time.Sleep(100 * time.Millisecond)
}
}
}()
// Handle input
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("> ")
for scanner.Scan() {
cmd := strings.TrimSpace(scanner.Text())
if cmd == "exit" {
break
}
fmt.Printf("You typed: %s\n> ", cmd)
}
stop <- true
fmt.Print(show) // restore cursor
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment