screen.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. package ansi
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. // EraseDisplay (ED) clears the display or parts of the display. A screen is
  7. // the shown part of the terminal display excluding the scrollback buffer.
  8. // Possible values:
  9. //
  10. // Default is 0.
  11. //
  12. // 0: Clear from cursor to end of screen.
  13. // 1: Clear from cursor to beginning of the screen.
  14. // 2: Clear entire screen (and moves cursor to upper left on DOS).
  15. // 3: Clear entire display which delete all lines saved in the scrollback buffer (xterm).
  16. //
  17. // CSI <n> J
  18. //
  19. // See: https://vt100.net/docs/vt510-rm/ED.html
  20. func EraseDisplay(n int) string {
  21. var s string
  22. if n > 0 {
  23. s = strconv.Itoa(n)
  24. }
  25. return "\x1b[" + s + "J"
  26. }
  27. // ED is an alias for [EraseDisplay].
  28. func ED(n int) string {
  29. return EraseDisplay(n)
  30. }
  31. // EraseDisplay constants.
  32. // These are the possible values for the EraseDisplay function.
  33. const (
  34. EraseScreenBelow = "\x1b[J"
  35. EraseScreenAbove = "\x1b[1J"
  36. EraseEntireScreen = "\x1b[2J"
  37. EraseEntireDisplay = "\x1b[3J"
  38. )
  39. // EraseLine (EL) clears the current line or parts of the line. Possible values:
  40. //
  41. // 0: Clear from cursor to end of line.
  42. // 1: Clear from cursor to beginning of the line.
  43. // 2: Clear entire line.
  44. //
  45. // The cursor position is not affected.
  46. //
  47. // CSI <n> K
  48. //
  49. // See: https://vt100.net/docs/vt510-rm/EL.html
  50. func EraseLine(n int) string {
  51. var s string
  52. if n > 0 {
  53. s = strconv.Itoa(n)
  54. }
  55. return "\x1b[" + s + "K"
  56. }
  57. // EL is an alias for [EraseLine].
  58. func EL(n int) string {
  59. return EraseLine(n)
  60. }
  61. // EraseLine constants.
  62. // These are the possible values for the EraseLine function.
  63. const (
  64. EraseLineRight = "\x1b[K"
  65. EraseLineLeft = "\x1b[1K"
  66. EraseEntireLine = "\x1b[2K"
  67. )
  68. // ScrollUp (SU) scrolls the screen up n lines. New lines are added at the
  69. // bottom of the screen.
  70. //
  71. // CSI Pn S
  72. //
  73. // See: https://vt100.net/docs/vt510-rm/SU.html
  74. func ScrollUp(n int) string {
  75. var s string
  76. if n > 1 {
  77. s = strconv.Itoa(n)
  78. }
  79. return "\x1b[" + s + "S"
  80. }
  81. // PanDown is an alias for [ScrollUp].
  82. func PanDown(n int) string {
  83. return ScrollUp(n)
  84. }
  85. // SU is an alias for [ScrollUp].
  86. func SU(n int) string {
  87. return ScrollUp(n)
  88. }
  89. // ScrollDown (SD) scrolls the screen down n lines. New lines are added at the
  90. // top of the screen.
  91. //
  92. // CSI Pn T
  93. //
  94. // See: https://vt100.net/docs/vt510-rm/SD.html
  95. func ScrollDown(n int) string {
  96. var s string
  97. if n > 1 {
  98. s = strconv.Itoa(n)
  99. }
  100. return "\x1b[" + s + "T"
  101. }
  102. // PanUp is an alias for [ScrollDown].
  103. func PanUp(n int) string {
  104. return ScrollDown(n)
  105. }
  106. // SD is an alias for [ScrollDown].
  107. func SD(n int) string {
  108. return ScrollDown(n)
  109. }
  110. // InsertLine (IL) inserts n blank lines at the current cursor position.
  111. // Existing lines are moved down.
  112. //
  113. // CSI Pn L
  114. //
  115. // See: https://vt100.net/docs/vt510-rm/IL.html
  116. func InsertLine(n int) string {
  117. var s string
  118. if n > 1 {
  119. s = strconv.Itoa(n)
  120. }
  121. return "\x1b[" + s + "L"
  122. }
  123. // IL is an alias for [InsertLine].
  124. func IL(n int) string {
  125. return InsertLine(n)
  126. }
  127. // DeleteLine (DL) deletes n lines at the current cursor position. Existing
  128. // lines are moved up.
  129. //
  130. // CSI Pn M
  131. //
  132. // See: https://vt100.net/docs/vt510-rm/DL.html
  133. func DeleteLine(n int) string {
  134. var s string
  135. if n > 1 {
  136. s = strconv.Itoa(n)
  137. }
  138. return "\x1b[" + s + "M"
  139. }
  140. // DL is an alias for [DeleteLine].
  141. func DL(n int) string {
  142. return DeleteLine(n)
  143. }
  144. // SetTopBottomMargins (DECSTBM) sets the top and bottom margins for the scrolling
  145. // region. The default is the entire screen.
  146. //
  147. // Default is 1 and the bottom of the screen.
  148. //
  149. // CSI Pt ; Pb r
  150. //
  151. // See: https://vt100.net/docs/vt510-rm/DECSTBM.html
  152. func SetTopBottomMargins(top, bot int) string {
  153. var t, b string
  154. if top > 0 {
  155. t = strconv.Itoa(top)
  156. }
  157. if bot > 0 {
  158. b = strconv.Itoa(bot)
  159. }
  160. return "\x1b[" + t + ";" + b + "r"
  161. }
  162. // DECSTBM is an alias for [SetTopBottomMargins].
  163. func DECSTBM(top, bot int) string {
  164. return SetTopBottomMargins(top, bot)
  165. }
  166. // SetLeftRightMargins (DECSLRM) sets the left and right margins for the scrolling
  167. // region.
  168. //
  169. // Default is 1 and the right of the screen.
  170. //
  171. // CSI Pl ; Pr s
  172. //
  173. // See: https://vt100.net/docs/vt510-rm/DECSLRM.html
  174. func SetLeftRightMargins(left, right int) string {
  175. var l, r string
  176. if left > 0 {
  177. l = strconv.Itoa(left)
  178. }
  179. if right > 0 {
  180. r = strconv.Itoa(right)
  181. }
  182. return "\x1b[" + l + ";" + r + "s"
  183. }
  184. // DECSLRM is an alias for [SetLeftRightMargins].
  185. func DECSLRM(left, right int) string {
  186. return SetLeftRightMargins(left, right)
  187. }
  188. // SetScrollingRegion (DECSTBM) sets the top and bottom margins for the scrolling
  189. // region. The default is the entire screen.
  190. //
  191. // CSI <top> ; <bottom> r
  192. //
  193. // See: https://vt100.net/docs/vt510-rm/DECSTBM.html
  194. //
  195. // Deprecated: use [SetTopBottomMargins] instead.
  196. func SetScrollingRegion(t, b int) string {
  197. if t < 0 {
  198. t = 0
  199. }
  200. if b < 0 {
  201. b = 0
  202. }
  203. return "\x1b[" + strconv.Itoa(t) + ";" + strconv.Itoa(b) + "r"
  204. }
  205. // InsertCharacter (ICH) inserts n blank characters at the current cursor
  206. // position. Existing characters move to the right. Characters moved past the
  207. // right margin are lost. ICH has no effect outside the scrolling margins.
  208. //
  209. // Default is 1.
  210. //
  211. // CSI Pn @
  212. //
  213. // See: https://vt100.net/docs/vt510-rm/ICH.html
  214. func InsertCharacter(n int) string {
  215. var s string
  216. if n > 1 {
  217. s = strconv.Itoa(n)
  218. }
  219. return "\x1b[" + s + "@"
  220. }
  221. // ICH is an alias for [InsertCharacter].
  222. func ICH(n int) string {
  223. return InsertCharacter(n)
  224. }
  225. // DeleteCharacter (DCH) deletes n characters at the current cursor position.
  226. // As the characters are deleted, the remaining characters move to the left and
  227. // the cursor remains at the same position.
  228. //
  229. // Default is 1.
  230. //
  231. // CSI Pn P
  232. //
  233. // See: https://vt100.net/docs/vt510-rm/DCH.html
  234. func DeleteCharacter(n int) string {
  235. var s string
  236. if n > 1 {
  237. s = strconv.Itoa(n)
  238. }
  239. return "\x1b[" + s + "P"
  240. }
  241. // DCH is an alias for [DeleteCharacter].
  242. func DCH(n int) string {
  243. return DeleteCharacter(n)
  244. }
  245. // SetTabEvery8Columns (DECST8C) sets the tab stops at every 8 columns.
  246. //
  247. // CSI ? 5 W
  248. //
  249. // See: https://vt100.net/docs/vt510-rm/DECST8C.html
  250. const (
  251. SetTabEvery8Columns = "\x1b[?5W"
  252. DECST8C = SetTabEvery8Columns
  253. )
  254. // HorizontalTabSet (HTS) sets a horizontal tab stop at the current cursor
  255. // column.
  256. //
  257. // This is equivalent to [HTS].
  258. //
  259. // ESC H
  260. //
  261. // See: https://vt100.net/docs/vt510-rm/HTS.html
  262. const HorizontalTabSet = "\x1bH"
  263. // TabClear (TBC) clears tab stops.
  264. //
  265. // Default is 0.
  266. //
  267. // Possible values:
  268. // 0: Clear tab stop at the current column. (default)
  269. // 3: Clear all tab stops.
  270. //
  271. // CSI Pn g
  272. //
  273. // See: https://vt100.net/docs/vt510-rm/TBC.html
  274. func TabClear(n int) string {
  275. var s string
  276. if n > 0 {
  277. s = strconv.Itoa(n)
  278. }
  279. return "\x1b[" + s + "g"
  280. }
  281. // TBC is an alias for [TabClear].
  282. func TBC(n int) string {
  283. return TabClear(n)
  284. }
  285. // RequestPresentationStateReport (DECRQPSR) requests the terminal to send a
  286. // report of the presentation state. This includes the cursor information [DECCIR],
  287. // and tab stop [DECTABSR] reports.
  288. //
  289. // Default is 0.
  290. //
  291. // Possible values:
  292. // 0: Error, request ignored.
  293. // 1: Cursor information report [DECCIR].
  294. // 2: Tab stop report [DECTABSR].
  295. //
  296. // CSI Ps $ w
  297. //
  298. // See: https://vt100.net/docs/vt510-rm/DECRQPSR.html
  299. func RequestPresentationStateReport(n int) string {
  300. var s string
  301. if n > 0 {
  302. s = strconv.Itoa(n)
  303. }
  304. return "\x1b[" + s + "$w"
  305. }
  306. // DECRQPSR is an alias for [RequestPresentationStateReport].
  307. func DECRQPSR(n int) string {
  308. return RequestPresentationStateReport(n)
  309. }
  310. // TabStopReport (DECTABSR) is the response to a tab stop report request.
  311. // It reports the tab stops set in the terminal.
  312. //
  313. // The response is a list of tab stops separated by a slash (/) character.
  314. //
  315. // DCS 2 $ u D ... D ST
  316. //
  317. // Where D is a decimal number representing a tab stop.
  318. //
  319. // See: https://vt100.net/docs/vt510-rm/DECTABSR.html
  320. func TabStopReport(stops ...int) string {
  321. var s []string
  322. for _, v := range stops {
  323. s = append(s, strconv.Itoa(v))
  324. }
  325. return "\x1bP2$u" + strings.Join(s, "/") + "\x1b\\"
  326. }
  327. // DECTABSR is an alias for [TabStopReport].
  328. func DECTABSR(stops ...int) string {
  329. return TabStopReport(stops...)
  330. }
  331. // CursorInformationReport (DECCIR) is the response to a cursor information
  332. // report request. It reports the cursor position, visual attributes, and
  333. // character protection attributes. It also reports the status of origin mode
  334. // [DECOM] and the current active character set.
  335. //
  336. // The response is a list of values separated by a semicolon (;) character.
  337. //
  338. // DCS 1 $ u D ... D ST
  339. //
  340. // Where D is a decimal number representing a value.
  341. //
  342. // See: https://vt100.net/docs/vt510-rm/DECCIR.html
  343. func CursorInformationReport(values ...int) string {
  344. var s []string
  345. for _, v := range values {
  346. s = append(s, strconv.Itoa(v))
  347. }
  348. return "\x1bP1$u" + strings.Join(s, ";") + "\x1b\\"
  349. }
  350. // DECCIR is an alias for [CursorInformationReport].
  351. func DECCIR(values ...int) string {
  352. return CursorInformationReport(values...)
  353. }
  354. // RepeatPreviousCharacter (REP) repeats the previous character n times.
  355. // This is identical to typing the same character n times.
  356. //
  357. // Default is 1.
  358. //
  359. // CSI Pn b
  360. //
  361. // See: ECMA-48 § 8.3.103
  362. func RepeatPreviousCharacter(n int) string {
  363. var s string
  364. if n > 1 {
  365. s = strconv.Itoa(n)
  366. }
  367. return "\x1b[" + s + "b"
  368. }
  369. // REP is an alias for [RepeatPreviousCharacter].
  370. func REP(n int) string {
  371. return RepeatPreviousCharacter(n)
  372. }