|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "golang.org/x/term" |
| 10 | +) |
| 11 | + |
| 12 | +func Init() *term.State { |
| 13 | + oldState, err := term.MakeRaw(int(os.Stdin.Fd())) |
| 14 | + if err != nil { |
| 15 | + //?log |
| 16 | + return nil |
| 17 | + } |
| 18 | + return oldState |
| 19 | +} |
| 20 | + |
| 21 | +func Deinit(oldState *term.State) { |
| 22 | + if oldState != nil { |
| 23 | + term.Restore(int(os.Stdin.Fd()), oldState) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func ClearLine() { |
| 28 | + fmt.Printf("\033[2K") |
| 29 | +} |
| 30 | + |
| 31 | +func ClearLineEnd() { |
| 32 | + fmt.Printf("\033[0K") |
| 33 | +} |
| 34 | + |
| 35 | +func ClearLineStart() { |
| 36 | + fmt.Printf("\033[1K") |
| 37 | +} |
| 38 | + |
| 39 | +func CursorHide() { |
| 40 | + fmt.Printf("\033[?25l") |
| 41 | +} |
| 42 | + |
| 43 | +func CursorPositionRestore() { |
| 44 | + fmt.Printf("\033[u") |
| 45 | +} |
| 46 | + |
| 47 | +func CursorSavePosition() { |
| 48 | + fmt.Printf("\033[s") |
| 49 | +} |
| 50 | + |
| 51 | +func CursorShow() { |
| 52 | + fmt.Printf("\033[?25h") |
| 53 | +} |
| 54 | + |
| 55 | +func MoveTo(x, y int) { |
| 56 | + fmt.Printf("\033[%d;%df", y, x) |
| 57 | +} |
| 58 | + |
| 59 | +// MoveDown moves the cursor to the beginning of n lines down. |
| 60 | +func MoveDown(n int) { |
| 61 | + fmt.Printf("\033[%dE", n) |
| 62 | +} |
| 63 | + |
| 64 | +// MoveUp moves the cursor to the beginning of n lines up. |
| 65 | +func MoveUp(n int) { |
| 66 | + fmt.Printf("\033[%dF", n) |
| 67 | +} |
| 68 | + |
| 69 | +func ScreenClear() { |
| 70 | + fmt.Printf("\033[2J") |
| 71 | + MoveTo(1, 1) |
| 72 | +} |
| 73 | + |
| 74 | +func ScreenRestore() { |
| 75 | + fmt.Printf("\033[?47l") |
| 76 | +} |
| 77 | + |
| 78 | +func ScreenSave() { |
| 79 | + fmt.Printf("\033[?47h") |
| 80 | +} |
| 81 | + |
| 82 | +func ScreenSize() (w int, h int) { |
| 83 | + fmt.Printf("\033[18t") // Report Size |
| 84 | + // The response will be in the form ESC [ 8 ; height ; width t |
| 85 | + // read from stdin to get the response |
| 86 | + buf := "" |
| 87 | + err := error(nil) |
| 88 | + for { |
| 89 | + var charBuf [1]byte |
| 90 | + _, err = os.Stdin.Read(charBuf[:]) |
| 91 | + if err != nil { |
| 92 | + break |
| 93 | + } |
| 94 | + b := charBuf[0] |
| 95 | + if b == 't' { |
| 96 | + break |
| 97 | + } |
| 98 | + |
| 99 | + buf += string(b) |
| 100 | + } |
| 101 | + if err != nil || len(buf) < 6 || buf[0] != '\033' || buf[1] != '[' || buf[2] != '8' || buf[3] != ';' { |
| 102 | + // ?log? |
| 103 | + return 80, 25 // default size |
| 104 | + } |
| 105 | + // parse height and width |
| 106 | + hw := strings.Split(buf[4:], ";") |
| 107 | + if len(hw) != 2 { |
| 108 | + return 80, 25 |
| 109 | + } |
| 110 | + w, err = strconv.Atoi(hw[1]) |
| 111 | + if err != nil { |
| 112 | + return 80, 25 |
| 113 | + } |
| 114 | + |
| 115 | + h, err = strconv.Atoi(hw[0]) |
| 116 | + if err != nil { |
| 117 | + return 80, 25 |
| 118 | + } |
| 119 | + |
| 120 | + return w, h |
| 121 | +} |
| 122 | + |
| 123 | +/* |
| 124 | +ANSI codes for screen management |
| 125 | +Save the current screen: ESC[?47h |
| 126 | +This saves the content of the current screen buffer to an alternate buffer. |
| 127 | +Restore the saved screen: ESC[?47l |
| 128 | +This restores the content from the alternate buffer, effectively "capturing" and then "recapturing" the screen. |
| 129 | +Clear the entire screen: ESC[2J |
| 130 | +This code clears the screen content but does not save it to an alternate buffer. |
| 131 | +
|
| 132 | +smcup |
| 133 | +\E7 saves the cursor's position |
| 134 | +\E[?47h switches to the alternate screen |
| 135 | +rmcup |
| 136 | +\E[2J clears the screen (assumed to be the alternate screen) |
| 137 | +\E[?47l switches back to the normal screen |
| 138 | +\E8 restores the cursor's position. |
| 139 | +
|
| 140 | +The syntax is ESC[?1049h and ESC[?1049l -- here h activates the alternate buffer and l returns to normal mode. Other similar codes are 1047 and 1048. But apparently they are older and have less functionality. |
| 141 | +
|
| 142 | +
|
| 143 | +https://en.wikipedia.org/wiki/ANSI_escape_code |
| 144 | +https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 |
| 145 | +https://xtermjs.org/docs/api/vtfeatures/ |
| 146 | +https://invisible-island.net/xterm/ctlseqs/ctlseqs.html |
| 147 | +https://github.com/leaanthony/go-ansi-parser |
| 148 | +
|
| 149 | +Xterm allows the window title to be set by ESC ]0;this is the window title BEL |
| 150 | +
|
| 151 | +*/ |
0 commit comments