screen.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. package termenv
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // Sequence definitions.
  7. const (
  8. // Cursor positioning.
  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. // Explicit values for EraseLineSeq.
  27. EraseLineRightSeq = "0K"
  28. EraseLineLeftSeq = "1K"
  29. EraseEntireLineSeq = "2K"
  30. // Mouse.
  31. EnableMousePressSeq = "?9h" // press only (X10)
  32. DisableMousePressSeq = "?9l"
  33. EnableMouseSeq = "?1000h" // press, release, wheel
  34. DisableMouseSeq = "?1000l"
  35. EnableMouseHiliteSeq = "?1001h" // highlight
  36. DisableMouseHiliteSeq = "?1001l"
  37. EnableMouseCellMotionSeq = "?1002h" // press, release, move on pressed, wheel
  38. DisableMouseCellMotionSeq = "?1002l"
  39. EnableMouseAllMotionSeq = "?1003h" // press, release, move, wheel
  40. DisableMouseAllMotionSeq = "?1003l"
  41. EnableMouseExtendedModeSeq = "?1006h" // press, release, move, wheel, extended coordinates
  42. DisableMouseExtendedModeSeq = "?1006l"
  43. EnableMousePixelsModeSeq = "?1016h" // press, release, move, wheel, extended pixel coordinates
  44. DisableMousePixelsModeSeq = "?1016l"
  45. // Screen.
  46. RestoreScreenSeq = "?47l"
  47. SaveScreenSeq = "?47h"
  48. AltScreenSeq = "?1049h"
  49. ExitAltScreenSeq = "?1049l"
  50. // Bracketed paste.
  51. // https://en.wikipedia.org/wiki/Bracketed-paste
  52. EnableBracketedPasteSeq = "?2004h"
  53. DisableBracketedPasteSeq = "?2004l"
  54. StartBracketedPasteSeq = "200~"
  55. EndBracketedPasteSeq = "201~"
  56. // Session.
  57. SetWindowTitleSeq = "2;%s" + string(BEL)
  58. SetForegroundColorSeq = "10;%s" + string(BEL)
  59. SetBackgroundColorSeq = "11;%s" + string(BEL)
  60. SetCursorColorSeq = "12;%s" + string(BEL)
  61. ShowCursorSeq = "?25h"
  62. HideCursorSeq = "?25l"
  63. )
  64. // Reset the terminal to its default style, removing any active styles.
  65. func (o Output) Reset() {
  66. fmt.Fprint(o.tty, CSI+ResetSeq+"m")
  67. }
  68. // SetForegroundColor sets the default foreground color.
  69. func (o Output) SetForegroundColor(color Color) {
  70. fmt.Fprintf(o.tty, OSC+SetForegroundColorSeq, color)
  71. }
  72. // SetBackgroundColor sets the default background color.
  73. func (o Output) SetBackgroundColor(color Color) {
  74. fmt.Fprintf(o.tty, OSC+SetBackgroundColorSeq, color)
  75. }
  76. // SetCursorColor sets the cursor color.
  77. func (o Output) SetCursorColor(color Color) {
  78. fmt.Fprintf(o.tty, OSC+SetCursorColorSeq, color)
  79. }
  80. // RestoreScreen restores a previously saved screen state.
  81. func (o Output) RestoreScreen() {
  82. fmt.Fprint(o.tty, CSI+RestoreScreenSeq)
  83. }
  84. // SaveScreen saves the screen state.
  85. func (o Output) SaveScreen() {
  86. fmt.Fprint(o.tty, CSI+SaveScreenSeq)
  87. }
  88. // AltScreen switches to the alternate screen buffer. The former view can be
  89. // restored with ExitAltScreen().
  90. func (o Output) AltScreen() {
  91. fmt.Fprint(o.tty, CSI+AltScreenSeq)
  92. }
  93. // ExitAltScreen exits the alternate screen buffer and returns to the former
  94. // terminal view.
  95. func (o Output) ExitAltScreen() {
  96. fmt.Fprint(o.tty, CSI+ExitAltScreenSeq)
  97. }
  98. // ClearScreen clears the visible portion of the terminal.
  99. func (o Output) ClearScreen() {
  100. fmt.Fprintf(o.tty, CSI+EraseDisplaySeq, 2)
  101. o.MoveCursor(1, 1)
  102. }
  103. // MoveCursor moves the cursor to a given position.
  104. func (o Output) MoveCursor(row int, column int) {
  105. fmt.Fprintf(o.tty, CSI+CursorPositionSeq, row, column)
  106. }
  107. // HideCursor hides the cursor.
  108. func (o Output) HideCursor() {
  109. fmt.Fprint(o.tty, CSI+HideCursorSeq)
  110. }
  111. // ShowCursor shows the cursor.
  112. func (o Output) ShowCursor() {
  113. fmt.Fprint(o.tty, CSI+ShowCursorSeq)
  114. }
  115. // SaveCursorPosition saves the cursor position.
  116. func (o Output) SaveCursorPosition() {
  117. fmt.Fprint(o.tty, CSI+SaveCursorPositionSeq)
  118. }
  119. // RestoreCursorPosition restores a saved cursor position.
  120. func (o Output) RestoreCursorPosition() {
  121. fmt.Fprint(o.tty, CSI+RestoreCursorPositionSeq)
  122. }
  123. // CursorUp moves the cursor up a given number of lines.
  124. func (o Output) CursorUp(n int) {
  125. fmt.Fprintf(o.tty, CSI+CursorUpSeq, n)
  126. }
  127. // CursorDown moves the cursor down a given number of lines.
  128. func (o Output) CursorDown(n int) {
  129. fmt.Fprintf(o.tty, CSI+CursorDownSeq, n)
  130. }
  131. // CursorForward moves the cursor up a given number of lines.
  132. func (o Output) CursorForward(n int) {
  133. fmt.Fprintf(o.tty, CSI+CursorForwardSeq, n)
  134. }
  135. // CursorBack moves the cursor backwards a given number of cells.
  136. func (o Output) CursorBack(n int) {
  137. fmt.Fprintf(o.tty, CSI+CursorBackSeq, n)
  138. }
  139. // CursorNextLine moves the cursor down a given number of lines and places it at
  140. // the beginning of the line.
  141. func (o Output) CursorNextLine(n int) {
  142. fmt.Fprintf(o.tty, CSI+CursorNextLineSeq, n)
  143. }
  144. // CursorPrevLine moves the cursor up a given number of lines and places it at
  145. // the beginning of the line.
  146. func (o Output) CursorPrevLine(n int) {
  147. fmt.Fprintf(o.tty, CSI+CursorPreviousLineSeq, n)
  148. }
  149. // ClearLine clears the current line.
  150. func (o Output) ClearLine() {
  151. fmt.Fprint(o.tty, CSI+EraseEntireLineSeq)
  152. }
  153. // ClearLineLeft clears the line to the left of the cursor.
  154. func (o Output) ClearLineLeft() {
  155. fmt.Fprint(o.tty, CSI+EraseLineLeftSeq)
  156. }
  157. // ClearLineRight clears the line to the right of the cursor.
  158. func (o Output) ClearLineRight() {
  159. fmt.Fprint(o.tty, CSI+EraseLineRightSeq)
  160. }
  161. // ClearLines clears a given number of lines.
  162. func (o Output) ClearLines(n int) {
  163. clearLine := fmt.Sprintf(CSI+EraseLineSeq, 2)
  164. cursorUp := fmt.Sprintf(CSI+CursorUpSeq, 1)
  165. fmt.Fprint(o.tty, clearLine+strings.Repeat(cursorUp+clearLine, n))
  166. }
  167. // ChangeScrollingRegion sets the scrolling region of the terminal.
  168. func (o Output) ChangeScrollingRegion(top, bottom int) {
  169. fmt.Fprintf(o.tty, CSI+ChangeScrollingRegionSeq, top, bottom)
  170. }
  171. // InsertLines inserts the given number of lines at the top of the scrollable
  172. // region, pushing lines below down.
  173. func (o Output) InsertLines(n int) {
  174. fmt.Fprintf(o.tty, CSI+InsertLineSeq, n)
  175. }
  176. // DeleteLines deletes the given number of lines, pulling any lines in
  177. // the scrollable region below up.
  178. func (o Output) DeleteLines(n int) {
  179. fmt.Fprintf(o.tty, CSI+DeleteLineSeq, n)
  180. }
  181. // EnableMousePress enables X10 mouse mode. Button press events are sent only.
  182. func (o Output) EnableMousePress() {
  183. fmt.Fprint(o.tty, CSI+EnableMousePressSeq)
  184. }
  185. // DisableMousePress disables X10 mouse mode.
  186. func (o Output) DisableMousePress() {
  187. fmt.Fprint(o.tty, CSI+DisableMousePressSeq)
  188. }
  189. // EnableMouse enables Mouse Tracking mode.
  190. func (o Output) EnableMouse() {
  191. fmt.Fprint(o.tty, CSI+EnableMouseSeq)
  192. }
  193. // DisableMouse disables Mouse Tracking mode.
  194. func (o Output) DisableMouse() {
  195. fmt.Fprint(o.tty, CSI+DisableMouseSeq)
  196. }
  197. // EnableMouseHilite enables Hilite Mouse Tracking mode.
  198. func (o Output) EnableMouseHilite() {
  199. fmt.Fprint(o.tty, CSI+EnableMouseHiliteSeq)
  200. }
  201. // DisableMouseHilite disables Hilite Mouse Tracking mode.
  202. func (o Output) DisableMouseHilite() {
  203. fmt.Fprint(o.tty, CSI+DisableMouseHiliteSeq)
  204. }
  205. // EnableMouseCellMotion enables Cell Motion Mouse Tracking mode.
  206. func (o Output) EnableMouseCellMotion() {
  207. fmt.Fprint(o.tty, CSI+EnableMouseCellMotionSeq)
  208. }
  209. // DisableMouseCellMotion disables Cell Motion Mouse Tracking mode.
  210. func (o Output) DisableMouseCellMotion() {
  211. fmt.Fprint(o.tty, CSI+DisableMouseCellMotionSeq)
  212. }
  213. // EnableMouseAllMotion enables All Motion Mouse mode.
  214. func (o Output) EnableMouseAllMotion() {
  215. fmt.Fprint(o.tty, CSI+EnableMouseAllMotionSeq)
  216. }
  217. // DisableMouseAllMotion disables All Motion Mouse mode.
  218. func (o Output) DisableMouseAllMotion() {
  219. fmt.Fprint(o.tty, CSI+DisableMouseAllMotionSeq)
  220. }
  221. // EnableMouseExtendedMotion enables Extended Mouse mode (SGR). This should be
  222. // enabled in conjunction with EnableMouseCellMotion, and EnableMouseAllMotion.
  223. func (o Output) EnableMouseExtendedMode() {
  224. fmt.Fprint(o.tty, CSI+EnableMouseExtendedModeSeq)
  225. }
  226. // DisableMouseExtendedMotion disables Extended Mouse mode (SGR).
  227. func (o Output) DisableMouseExtendedMode() {
  228. fmt.Fprint(o.tty, CSI+DisableMouseExtendedModeSeq)
  229. }
  230. // EnableMousePixelsMotion enables Pixel Motion Mouse mode (SGR-Pixels). This
  231. // should be enabled in conjunction with EnableMouseCellMotion, and
  232. // EnableMouseAllMotion.
  233. func (o Output) EnableMousePixelsMode() {
  234. fmt.Fprint(o.tty, CSI+EnableMousePixelsModeSeq)
  235. }
  236. // DisableMousePixelsMotion disables Pixel Motion Mouse mode (SGR-Pixels).
  237. func (o Output) DisableMousePixelsMode() {
  238. fmt.Fprint(o.tty, CSI+DisableMousePixelsModeSeq)
  239. }
  240. // SetWindowTitle sets the terminal window title.
  241. func (o Output) SetWindowTitle(title string) {
  242. fmt.Fprintf(o.tty, OSC+SetWindowTitleSeq, title)
  243. }
  244. // EnableBracketedPaste enables bracketed paste.
  245. func (o Output) EnableBracketedPaste() {
  246. fmt.Fprintf(o.tty, CSI+EnableBracketedPasteSeq)
  247. }
  248. // DisableBracketedPaste disables bracketed paste.
  249. func (o Output) DisableBracketedPaste() {
  250. fmt.Fprintf(o.tty, CSI+DisableBracketedPasteSeq)
  251. }
  252. // Legacy functions.
  253. // Reset the terminal to its default style, removing any active styles.
  254. //
  255. // Deprecated: please use termenv.Output instead.
  256. func Reset() {
  257. output.Reset()
  258. }
  259. // SetForegroundColor sets the default foreground color.
  260. //
  261. // Deprecated: please use termenv.Output instead.
  262. func SetForegroundColor(color Color) {
  263. output.SetForegroundColor(color)
  264. }
  265. // SetBackgroundColor sets the default background color.
  266. //
  267. // Deprecated: please use termenv.Output instead.
  268. func SetBackgroundColor(color Color) {
  269. output.SetBackgroundColor(color)
  270. }
  271. // SetCursorColor sets the cursor color.
  272. //
  273. // Deprecated: please use termenv.Output instead.
  274. func SetCursorColor(color Color) {
  275. output.SetCursorColor(color)
  276. }
  277. // RestoreScreen restores a previously saved screen state.
  278. //
  279. // Deprecated: please use termenv.Output instead.
  280. func RestoreScreen() {
  281. output.RestoreScreen()
  282. }
  283. // SaveScreen saves the screen state.
  284. //
  285. // Deprecated: please use termenv.Output instead.
  286. func SaveScreen() {
  287. output.SaveScreen()
  288. }
  289. // AltScreen switches to the alternate screen buffer. The former view can be
  290. // restored with ExitAltScreen().
  291. //
  292. // Deprecated: please use termenv.Output instead.
  293. func AltScreen() {
  294. output.AltScreen()
  295. }
  296. // ExitAltScreen exits the alternate screen buffer and returns to the former
  297. // terminal view.
  298. //
  299. // Deprecated: please use termenv.Output instead.
  300. func ExitAltScreen() {
  301. output.ExitAltScreen()
  302. }
  303. // ClearScreen clears the visible portion of the terminal.
  304. //
  305. // Deprecated: please use termenv.Output instead.
  306. func ClearScreen() {
  307. output.ClearScreen()
  308. }
  309. // MoveCursor moves the cursor to a given position.
  310. //
  311. // Deprecated: please use termenv.Output instead.
  312. func MoveCursor(row int, column int) {
  313. output.MoveCursor(row, column)
  314. }
  315. // HideCursor hides the cursor.
  316. //
  317. // Deprecated: please use termenv.Output instead.
  318. func HideCursor() {
  319. output.HideCursor()
  320. }
  321. // ShowCursor shows the cursor.
  322. //
  323. // Deprecated: please use termenv.Output instead.
  324. func ShowCursor() {
  325. output.ShowCursor()
  326. }
  327. // SaveCursorPosition saves the cursor position.
  328. //
  329. // Deprecated: please use termenv.Output instead.
  330. func SaveCursorPosition() {
  331. output.SaveCursorPosition()
  332. }
  333. // RestoreCursorPosition restores a saved cursor position.
  334. //
  335. // Deprecated: please use termenv.Output instead.
  336. func RestoreCursorPosition() {
  337. output.RestoreCursorPosition()
  338. }
  339. // CursorUp moves the cursor up a given number of lines.
  340. //
  341. // Deprecated: please use termenv.Output instead.
  342. func CursorUp(n int) {
  343. output.CursorUp(n)
  344. }
  345. // CursorDown moves the cursor down a given number of lines.
  346. //
  347. // Deprecated: please use termenv.Output instead.
  348. func CursorDown(n int) {
  349. output.CursorDown(n)
  350. }
  351. // CursorForward moves the cursor up a given number of lines.
  352. //
  353. // Deprecated: please use termenv.Output instead.
  354. func CursorForward(n int) {
  355. output.CursorForward(n)
  356. }
  357. // CursorBack moves the cursor backwards a given number of cells.
  358. //
  359. // Deprecated: please use termenv.Output instead.
  360. func CursorBack(n int) {
  361. output.CursorBack(n)
  362. }
  363. // CursorNextLine moves the cursor down a given number of lines and places it at
  364. // the beginning of the line.
  365. //
  366. // Deprecated: please use termenv.Output instead.
  367. func CursorNextLine(n int) {
  368. output.CursorNextLine(n)
  369. }
  370. // CursorPrevLine moves the cursor up a given number of lines and places it at
  371. // the beginning of the line.
  372. //
  373. // Deprecated: please use termenv.Output instead.
  374. func CursorPrevLine(n int) {
  375. output.CursorPrevLine(n)
  376. }
  377. // ClearLine clears the current line.
  378. //
  379. // Deprecated: please use termenv.Output instead.
  380. func ClearLine() {
  381. output.ClearLine()
  382. }
  383. // ClearLineLeft clears the line to the left of the cursor.
  384. //
  385. // Deprecated: please use termenv.Output instead.
  386. func ClearLineLeft() {
  387. output.ClearLineLeft()
  388. }
  389. // ClearLineRight clears the line to the right of the cursor.
  390. //
  391. // Deprecated: please use termenv.Output instead.
  392. func ClearLineRight() {
  393. output.ClearLineRight()
  394. }
  395. // ClearLines clears a given number of lines.
  396. //
  397. // Deprecated: please use termenv.Output instead.
  398. func ClearLines(n int) {
  399. output.ClearLines(n)
  400. }
  401. // ChangeScrollingRegion sets the scrolling region of the terminal.
  402. //
  403. // Deprecated: please use termenv.Output instead.
  404. func ChangeScrollingRegion(top, bottom int) {
  405. output.ChangeScrollingRegion(top, bottom)
  406. }
  407. // InsertLines inserts the given number of lines at the top of the scrollable
  408. // region, pushing lines below down.
  409. //
  410. // Deprecated: please use termenv.Output instead.
  411. func InsertLines(n int) {
  412. output.InsertLines(n)
  413. }
  414. // DeleteLines deletes the given number of lines, pulling any lines in
  415. // the scrollable region below up.
  416. //
  417. // Deprecated: please use termenv.Output instead.
  418. func DeleteLines(n int) {
  419. output.DeleteLines(n)
  420. }
  421. // EnableMousePress enables X10 mouse mode. Button press events are sent only.
  422. //
  423. // Deprecated: please use termenv.Output instead.
  424. func EnableMousePress() {
  425. output.EnableMousePress()
  426. }
  427. // DisableMousePress disables X10 mouse mode.
  428. //
  429. // Deprecated: please use termenv.Output instead.
  430. func DisableMousePress() {
  431. output.DisableMousePress()
  432. }
  433. // EnableMouse enables Mouse Tracking mode.
  434. //
  435. // Deprecated: please use termenv.Output instead.
  436. func EnableMouse() {
  437. output.EnableMouse()
  438. }
  439. // DisableMouse disables Mouse Tracking mode.
  440. //
  441. // Deprecated: please use termenv.Output instead.
  442. func DisableMouse() {
  443. output.DisableMouse()
  444. }
  445. // EnableMouseHilite enables Hilite Mouse Tracking mode.
  446. //
  447. // Deprecated: please use termenv.Output instead.
  448. func EnableMouseHilite() {
  449. output.EnableMouseHilite()
  450. }
  451. // DisableMouseHilite disables Hilite Mouse Tracking mode.
  452. //
  453. // Deprecated: please use termenv.Output instead.
  454. func DisableMouseHilite() {
  455. output.DisableMouseHilite()
  456. }
  457. // EnableMouseCellMotion enables Cell Motion Mouse Tracking mode.
  458. //
  459. // Deprecated: please use termenv.Output instead.
  460. func EnableMouseCellMotion() {
  461. output.EnableMouseCellMotion()
  462. }
  463. // DisableMouseCellMotion disables Cell Motion Mouse Tracking mode.
  464. //
  465. // Deprecated: please use termenv.Output instead.
  466. func DisableMouseCellMotion() {
  467. output.DisableMouseCellMotion()
  468. }
  469. // EnableMouseAllMotion enables All Motion Mouse mode.
  470. //
  471. // Deprecated: please use termenv.Output instead.
  472. func EnableMouseAllMotion() {
  473. output.EnableMouseAllMotion()
  474. }
  475. // DisableMouseAllMotion disables All Motion Mouse mode.
  476. //
  477. // Deprecated: please use termenv.Output instead.
  478. func DisableMouseAllMotion() {
  479. output.DisableMouseAllMotion()
  480. }
  481. // SetWindowTitle sets the terminal window title.
  482. //
  483. // Deprecated: please use termenv.Output instead.
  484. func SetWindowTitle(title string) {
  485. output.SetWindowTitle(title)
  486. }
  487. // EnableBracketedPaste enables bracketed paste.
  488. //
  489. // Deprecated: please use termenv.Output instead.
  490. func EnableBracketedPaste() {
  491. output.EnableBracketedPaste()
  492. }
  493. // DisableBracketedPaste disables bracketed paste.
  494. //
  495. // Deprecated: please use termenv.Output instead.
  496. func DisableBracketedPaste() {
  497. output.DisableBracketedPaste()
  498. }