Skip to content

Commit

Permalink
Merge pull request #25 from debeando/terminal-refresh
Browse files Browse the repository at this point in the history
Terminal refresh
  • Loading branch information
nstrappazzonc committed Nov 26, 2023
2 parents ee10a80 + 543300b commit 1cef686
Showing 1 changed file with 99 additions and 16 deletions.
115 changes: 99 additions & 16 deletions terminal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,58 @@ import (
"fmt"
"math"
"os"
"time"

"golang.org/x/sys/unix"
)

const (
// Clear screen
CLEAR = "\033[2J"
// Return cursor to top left
RESET_CURSOR = "\033[H"
// Reset all custom styles
// Reset all custom styles.
RESET = "\033[0m"
// Reset to default color
// Reset to default color.
RESET_COLOR = "\033[39;49m"
// Return cursor to start of line and clean it
RESET_LINE = "\r\033[K"
// Return cursor to start of line and clean it.
LINE_RESET = "\r\033[K"
// Erase the entire line.
LINE_ERASE = "\x1B[2K"
// Restore screen.
SCREEN_RESTORE = "\x1B[?47l"
// Save screen.
SCREEN_SAVE = "\x1B[?47h"
// Clear complete screen.
SCREEN_CLEAR = "\033[2J"
// Return cursor to top left.
CURSOR_RESET = "\033[H"
// Hide cursor.
CURSOR_HIDE = "\x1b[?25l"
// Show cursor.
CURSOR_SHOW = "\x1b[?25h"
// Erase from cursor to beginning of screen.
CURSOR_ERASE_FBS = "\x1B[1J"
// Erase from cursor to end of screen.
CURSOR_ERASE_FES = "\x1B[0J"
// Set new cursor position on screen.
CURSOR_SET_POSITION = "\x1B[%d;%dH"
// Save the cursor position.
CURSOR_SAVE_POSITION = "\x1B7"
// Restore the cursor position.
CURSOR_RESTORE_POSITION = "\x1B8"
)

var Output *bufio.Writer = bufio.NewWriter(os.Stdout)
var Screen *bytes.Buffer = new(bytes.Buffer)

func Clear() {
// Clear screen
Output.WriteString(CLEAR)
Output.WriteString(SCREEN_CLEAR)
// Return cursor to top left
Output.WriteString(RESET_CURSOR)
Output.WriteString(CURSOR_RESET)
}

func Reset() {
fmt.Print(RESET)
fmt.Print(RESET_COLOR)
fmt.Print(RESET_LINE)
Output.WriteString(RESET)
Output.WriteString(RESET_COLOR)
Output.WriteString(LINE_RESET)
}

func Flush() {
Expand Down Expand Up @@ -65,12 +86,74 @@ func Height() int {
return int(ws.Row)
}

func Cursor(x, y int) {
fmt.Printf("\x1B[%d;%dH", x, y)
func ScreenSave() {
Output.WriteString(SCREEN_SAVE)
}

func ScreenRestore() {
Output.WriteString(SCREEN_RESTORE)
}

func Refresh(f func()) {
func LineReset() {
Output.WriteString(LINE_RESET)
}

func LineErase() {
Output.WriteString(LINE_ERASE)
}

func CursorHide() {
Output.WriteString(CURSOR_HIDE)
}

func CursorShow() {
Output.WriteString(CURSOR_SHOW)
Flush()
}

func CursorRestore() {
Output.WriteString(CURSOR_RESTORE_POSITION)
Flush()
}

func CursorSave() {
Output.WriteString(CURSOR_SAVE_POSITION)
}

func CursorSet(x, y int) {
Output.WriteString(fmt.Sprintf(CURSOR_SET_POSITION, x, y))
}

func CursorEraseToEndScreen() {
Output.WriteString(CURSOR_ERASE_FES)
}

func CursorEraseToBeginningScreen() {
Output.WriteString(CURSOR_ERASE_FBS)
}

func Refresh(wait int, f func()) {
Reset()
Clear()
CursorHide()
Flush()

for {
CursorSave()
LineErase()
CursorEraseToEndScreen()
ScreenSave()
CursorEraseToBeginningScreen()
ScreenRestore()
defer CursorRestore()

CursorSet(0,0)
Flush()
f()
time.Sleep(time.Duration(wait) * time.Second)
}

CursorShow()
}

func getWinsize() (*unix.Winsize, error) {
Expand Down

0 comments on commit 1cef686

Please sign in to comment.