1package termenv 2 3import ( 4 "fmt" 5 "strings" 6) 7 8const ( 9 CursorUpSeq = "%dA" 10 CursorDownSeq = "%dB" 11 CursorForwardSeq = "%dC" 12 CursorBackSeq = "%dD" 13 CursorNextLineSeq = "%dE" 14 CursorPreviousLineSeq = "%dF" 15 CursorHorizontalSeq = "%dG" 16 CursorPositionSeq = "%d;%dH" 17 EraseDisplaySeq = "%dJ" 18 EraseLineSeq = "%dK" 19 ScrollUpSeq = "%dS" 20 ScrollDownSeq = "%dT" 21 SaveCursorPositionSeq = "s" 22 RestoreCursorPositionSeq = "u" 23 ChangeScrollingRegionSeq = "%d;%dr" 24 InsertLineSeq = "%dL" 25 DeleteLineSeq = "%dM" 26 27 // Explicit values for EraseLineSeq. 28 EraseLineRightSeq = "0K" 29 EraseLineLeftSeq = "1K" 30 EraseEntireLineSeq = "2K" 31 32 ShowCursorSeq = "?25h" 33 HideCursorSeq = "?25l" 34 EnableMousePressSeq = "?9h" // press only (X10) 35 DisableMousePressSeq = "?9l" 36 EnableMouseSeq = "?1000h" // press, release, wheel 37 DisableMouseSeq = "?1000l" 38 EnableMouseHiliteSeq = "?1001h" // highlight 39 DisableMouseHiliteSeq = "?1001l" 40 EnableMouseCellMotionSeq = "?1002h" // press, release, move on pressed, wheel 41 DisableMouseCellMotionSeq = "?1002l" 42 EnableMouseAllMotionSeq = "?1003h" // press, release, move, wheel 43 DisableMouseAllMotionSeq = "?1003l" 44 AltScreenSeq = "?1049h" 45 ExitAltScreenSeq = "?1049l" 46) 47 48// Reset the terminal to its default style, removing any active styles. 49func Reset() { 50 fmt.Print(CSI + ResetSeq + "m") 51} 52 53// AltScreen switches to the alternate screen buffer. The former view can be 54// restored with ExitAltScreen(). 55func AltScreen() { 56 fmt.Print(CSI + AltScreenSeq) 57} 58 59// ExitAltScreen exits the alternate screen buffer and returns to the former 60// terminal view. 61func ExitAltScreen() { 62 fmt.Print(CSI + ExitAltScreenSeq) 63} 64 65// ClearScreen clears the visible portion of the terminal. 66func ClearScreen() { 67 fmt.Printf(CSI+EraseDisplaySeq, 2) 68 MoveCursor(1, 1) 69} 70 71// MoveCursor moves the cursor to a given position. 72func MoveCursor(row int, column int) { 73 fmt.Printf(CSI+CursorPositionSeq, row, column) 74} 75 76// HideCursor hides the cursor. 77func HideCursor() { 78 fmt.Printf(CSI + HideCursorSeq) 79} 80 81// ShowCursor shows the cursor. 82func ShowCursor() { 83 fmt.Printf(CSI + ShowCursorSeq) 84} 85 86// SaveCursorPosition saves the cursor position. 87func SaveCursorPosition() { 88 fmt.Print(CSI + SaveCursorPositionSeq) 89} 90 91// RestoreCursorPosition restores a saved cursor position. 92func RestoreCursorPosition() { 93 fmt.Print(CSI + RestoreCursorPositionSeq) 94} 95 96// CursorUp moves the cursor up a given number of lines. 97func CursorUp(n int) { 98 fmt.Printf(CSI+CursorUpSeq, n) 99} 100 101// CursorDown moves the cursor down a given number of lines. 102func CursorDown(n int) { 103 fmt.Printf(CSI+CursorDownSeq, n) 104} 105 106// CursorForward moves the cursor up a given number of lines. 107func CursorForward(n int) { 108 fmt.Printf(CSI+CursorForwardSeq, n) 109} 110 111// CursorBack moves the cursor backwards a given number of cells. 112func CursorBack(n int) { 113 fmt.Printf(CSI+CursorBackSeq, n) 114} 115 116// CursorNextLine moves the cursor down a given number of lines and places it at 117// the beginning of the line. 118func CursorNextLine(n int) { 119 fmt.Printf(CSI+CursorNextLineSeq, n) 120} 121 122// CursorPrevLine moves the cursor up a given number of lines and places it at 123// the beginning of the line. 124func CursorPrevLine(n int) { 125 fmt.Printf(CSI+CursorPreviousLineSeq, n) 126} 127 128// ClearLine clears the current line. 129func ClearLine() { 130 fmt.Print(CSI + EraseEntireLineSeq) 131} 132 133// ClearLineLeft clears the line to the left of the cursor. 134func ClearLineLeft() { 135 fmt.Print(CSI + EraseLineLeftSeq) 136} 137 138// ClearLineRight clears the line to the right of the cursor. 139func ClearLineRight() { 140 fmt.Print(CSI + EraseLineRightSeq) 141} 142 143// ClearLines clears a given number of lines. 144func ClearLines(n int) { 145 clearLine := fmt.Sprintf(CSI+EraseLineSeq, 2) 146 cursorUp := fmt.Sprintf(CSI+CursorUpSeq, 1) 147 fmt.Print(clearLine + strings.Repeat(cursorUp+clearLine, n)) 148} 149 150// ChangeScrollingRegion sets the scrolling region of the terminal. 151func ChangeScrollingRegion(top, bottom int) { 152 fmt.Printf(CSI+ChangeScrollingRegionSeq, top, bottom) 153} 154 155// InsertLines inserts the given number of lines at the top of the scrollable 156// region, pushing lines below down. 157func InsertLines(n int) { 158 fmt.Printf(CSI+InsertLineSeq, n) 159} 160 161// DeleteLines deletes the given number of lines, pulling any lines in 162// the scrollable region below up. 163func DeleteLines(n int) { 164 fmt.Printf(CSI+DeleteLineSeq, n) 165} 166 167// EnableMousePress enables X10 mouse mode. Button press events are sent only. 168func EnableMousePress() { 169 fmt.Print(CSI + EnableMousePressSeq) 170} 171 172// DisableMousePress disables X10 mouse mode. 173func DisableMousePress() { 174 fmt.Print(CSI + EnableMousePressSeq) 175} 176 177// EnableMouse enables Mouse Tracking mode. 178func EnableMouse() { 179 fmt.Print(CSI + EnableMouseSeq) 180} 181 182// DisableMouse disables Mouse Tracking mode. 183func DisableMouse() { 184 fmt.Print(CSI + DisableMouseSeq) 185} 186 187// EnableMouseHilite enables Hilite Mouse Tracking mode. 188func EnableMouseHilite() { 189 fmt.Print(CSI + EnableMouseHiliteSeq) 190} 191 192// DisableMouseHilite disables Hilite Mouse Tracking mode. 193func DisableMouseHilite() { 194 fmt.Print(CSI + DisableMouseHiliteSeq) 195} 196 197// EnableMouseCellMotion enables Cell Motion Mouse Tracking mode. 198func EnableMouseCellMotion() { 199 fmt.Print(CSI + EnableMouseCellMotionSeq) 200} 201 202// DisableMouseCellMotion disables Cell Motion Mouse Tracking mode. 203func DisableMouseCellMotion() { 204 fmt.Print(CSI + DisableMouseCellMotionSeq) 205} 206 207// EnableMouseAllMotion enables All Motion Mouse mode. 208func EnableMouseAllMotion() { 209 fmt.Print(CSI + EnableMouseAllMotionSeq) 210} 211 212// DisableMouseAllMotion disables All Motion Mouse mode. 213func DisableMouseAllMotion() { 214 fmt.Print(CSI + DisableMouseAllMotionSeq) 215} 216