terminal.nim 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module contains a few procedures to control the *terminal*
  10. ## (also called *console*). On UNIX, the implementation simply uses ANSI escape
  11. ## sequences and does not depend on any other module, on Windows it uses the
  12. ## Windows API.
  13. ## Changing the style is permanent even after program termination! Use the
  14. ## code `exitprocs.addExitProc(resetAttributes)` to restore the defaults.
  15. ## Similarly, if you hide the cursor, make sure to unhide it with
  16. ## `showCursor` before quitting.
  17. ##
  18. ## Progress bar
  19. ## ============
  20. ##
  21. ## Basic progress bar example:
  22. runnableExamples("-r:off"):
  23. import std/[os, strutils]
  24. for i in 0..100:
  25. stdout.styledWriteLine(fgRed, "0% ", fgWhite, '#'.repeat i, if i > 50: fgGreen else: fgYellow, "\t", $i , "%")
  26. sleep 42
  27. cursorUp 1
  28. eraseLine()
  29. stdout.resetAttributes()
  30. ##[
  31. ## Playing with colorful and styled text
  32. ]##
  33. ## Procs like `styledWriteLine`, `styledEcho` etc. have a temporary effect on
  34. ## text parameters. Style parameters only affect the text parameter right after them.
  35. ## After being called, these procs will reset the default style of the terminal.
  36. ## While `setBackGroundColor`, `setForeGroundColor` etc. have a lasting
  37. ## influence on the terminal, you can use `resetAttributes` to
  38. ## reset the default style of the terminal.
  39. runnableExamples("-r:off"):
  40. stdout.styledWriteLine({styleBright, styleBlink, styleUnderscore}, "styled text ")
  41. stdout.styledWriteLine(fgRed, "red text ")
  42. stdout.styledWriteLine(fgWhite, bgRed, "white text in red background")
  43. stdout.styledWriteLine(" ordinary text without style ")
  44. stdout.setBackGroundColor(bgCyan, true)
  45. stdout.setForeGroundColor(fgBlue)
  46. stdout.write("blue text in cyan background")
  47. stdout.resetAttributes()
  48. # You can specify multiple text parameters. Style parameters
  49. # only affect the text parameter right after them.
  50. styledEcho styleBright, fgGreen, "[PASS]", resetStyle, fgGreen, " Yay!"
  51. stdout.styledWriteLine(fgRed, "red text ", styleBright, "bold red", fgDefault, " bold text")
  52. import macros
  53. import strformat
  54. from strutils import toLowerAscii, `%`
  55. import colors
  56. when defined(windows):
  57. import winlean
  58. when defined(nimPreviewSlimSystem):
  59. import std/[syncio, assertions]
  60. type
  61. PTerminal = ref object
  62. trueColorIsSupported: bool
  63. trueColorIsEnabled: bool
  64. fgSetColor: bool
  65. when defined(windows):
  66. hStdout: Handle
  67. hStderr: Handle
  68. oldStdoutAttr: int16
  69. oldStderrAttr: int16
  70. var gTerm {.threadvar.}: owned(PTerminal)
  71. when defined(windows) and defined(consoleapp):
  72. proc newTerminal(): owned(PTerminal) {.gcsafe, raises: [OSError].}
  73. else:
  74. proc newTerminal(): owned(PTerminal) {.gcsafe, raises: [].}
  75. proc getTerminal(): PTerminal {.inline.} =
  76. if isNil(gTerm):
  77. gTerm = newTerminal()
  78. result = gTerm
  79. const
  80. fgPrefix = "\e[38;2;"
  81. bgPrefix = "\e[48;2;"
  82. ansiResetCode* = "\e[0m"
  83. stylePrefix = "\e["
  84. when defined(windows):
  85. import winlean, os
  86. const
  87. DUPLICATE_SAME_ACCESS = 2
  88. FOREGROUND_BLUE = 1
  89. FOREGROUND_GREEN = 2
  90. FOREGROUND_RED = 4
  91. FOREGROUND_INTENSITY = 8
  92. BACKGROUND_BLUE = 16
  93. BACKGROUND_GREEN = 32
  94. BACKGROUND_RED = 64
  95. BACKGROUND_INTENSITY = 128
  96. FOREGROUND_RGB = FOREGROUND_RED or FOREGROUND_GREEN or FOREGROUND_BLUE
  97. BACKGROUND_RGB = BACKGROUND_RED or BACKGROUND_GREEN or BACKGROUND_BLUE
  98. ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
  99. type
  100. SHORT = int16
  101. COORD = object
  102. x: SHORT
  103. y: SHORT
  104. SMALL_RECT = object
  105. left: SHORT
  106. top: SHORT
  107. right: SHORT
  108. bottom: SHORT
  109. CONSOLE_SCREEN_BUFFER_INFO = object
  110. dwSize: COORD
  111. dwCursorPosition: COORD
  112. wAttributes: int16
  113. srWindow: SMALL_RECT
  114. dwMaximumWindowSize: COORD
  115. CONSOLE_CURSOR_INFO = object
  116. dwSize: DWORD
  117. bVisible: WINBOOL
  118. proc duplicateHandle(hSourceProcessHandle: Handle, hSourceHandle: Handle,
  119. hTargetProcessHandle: Handle, lpTargetHandle: ptr Handle,
  120. dwDesiredAccess: DWORD, bInheritHandle: WINBOOL,
  121. dwOptions: DWORD): WINBOOL{.stdcall, dynlib: "kernel32",
  122. importc: "DuplicateHandle".}
  123. proc getCurrentProcess(): Handle{.stdcall, dynlib: "kernel32",
  124. importc: "GetCurrentProcess".}
  125. proc getConsoleScreenBufferInfo(hConsoleOutput: Handle,
  126. lpConsoleScreenBufferInfo: ptr CONSOLE_SCREEN_BUFFER_INFO): WINBOOL{.stdcall,
  127. dynlib: "kernel32", importc: "GetConsoleScreenBufferInfo".}
  128. proc getConsoleCursorInfo(hConsoleOutput: Handle,
  129. lpConsoleCursorInfo: ptr CONSOLE_CURSOR_INFO): WINBOOL{.
  130. stdcall, dynlib: "kernel32", importc: "GetConsoleCursorInfo".}
  131. proc setConsoleCursorInfo(hConsoleOutput: Handle,
  132. lpConsoleCursorInfo: ptr CONSOLE_CURSOR_INFO): WINBOOL{.
  133. stdcall, dynlib: "kernel32", importc: "SetConsoleCursorInfo".}
  134. proc terminalWidthIoctl*(handles: openArray[Handle]): int =
  135. var csbi: CONSOLE_SCREEN_BUFFER_INFO
  136. for h in handles:
  137. if getConsoleScreenBufferInfo(h, addr csbi) != 0:
  138. return int(csbi.srWindow.right - csbi.srWindow.left + 1)
  139. return 0
  140. proc terminalHeightIoctl*(handles: openArray[Handle]): int =
  141. var csbi: CONSOLE_SCREEN_BUFFER_INFO
  142. for h in handles:
  143. if getConsoleScreenBufferInfo(h, addr csbi) != 0:
  144. return int(csbi.srWindow.bottom - csbi.srWindow.top + 1)
  145. return 0
  146. proc terminalWidth*(): int =
  147. ## Returns the terminal width in columns.
  148. var w: int = 0
  149. w = terminalWidthIoctl([getStdHandle(STD_INPUT_HANDLE),
  150. getStdHandle(STD_OUTPUT_HANDLE),
  151. getStdHandle(STD_ERROR_HANDLE)])
  152. if w > 0: return w
  153. return 80
  154. proc terminalHeight*(): int =
  155. ## Returns the terminal height in rows.
  156. var h: int = 0
  157. h = terminalHeightIoctl([getStdHandle(STD_INPUT_HANDLE),
  158. getStdHandle(STD_OUTPUT_HANDLE),
  159. getStdHandle(STD_ERROR_HANDLE)])
  160. if h > 0: return h
  161. return 0
  162. proc setConsoleCursorPosition(hConsoleOutput: Handle,
  163. dwCursorPosition: COORD): WINBOOL{.
  164. stdcall, dynlib: "kernel32", importc: "SetConsoleCursorPosition".}
  165. proc fillConsoleOutputCharacter(hConsoleOutput: Handle, cCharacter: char,
  166. nLength: DWORD, dwWriteCoord: COORD,
  167. lpNumberOfCharsWritten: ptr DWORD): WINBOOL{.
  168. stdcall, dynlib: "kernel32", importc: "FillConsoleOutputCharacterA".}
  169. proc fillConsoleOutputAttribute(hConsoleOutput: Handle, wAttribute: int16,
  170. nLength: DWORD, dwWriteCoord: COORD,
  171. lpNumberOfAttrsWritten: ptr DWORD): WINBOOL{.
  172. stdcall, dynlib: "kernel32", importc: "FillConsoleOutputAttribute".}
  173. proc setConsoleTextAttribute(hConsoleOutput: Handle,
  174. wAttributes: int16): WINBOOL{.
  175. stdcall, dynlib: "kernel32", importc: "SetConsoleTextAttribute".}
  176. proc getConsoleMode(hConsoleHandle: Handle, dwMode: ptr DWORD): WINBOOL{.
  177. stdcall, dynlib: "kernel32", importc: "GetConsoleMode".}
  178. proc setConsoleMode(hConsoleHandle: Handle, dwMode: DWORD): WINBOOL{.
  179. stdcall, dynlib: "kernel32", importc: "SetConsoleMode".}
  180. proc getCursorPos(h: Handle): tuple [x, y: int] =
  181. var c: CONSOLE_SCREEN_BUFFER_INFO
  182. if getConsoleScreenBufferInfo(h, addr(c)) == 0:
  183. raiseOSError(osLastError())
  184. return (int(c.dwCursorPosition.x), int(c.dwCursorPosition.y))
  185. proc setCursorPos(h: Handle, x, y: int) =
  186. var c: COORD
  187. c.x = int16(x)
  188. c.y = int16(y)
  189. if setConsoleCursorPosition(h, c) == 0:
  190. raiseOSError(osLastError())
  191. proc getAttributes(h: Handle): int16 =
  192. var c: CONSOLE_SCREEN_BUFFER_INFO
  193. # workaround Windows bugs: try several times
  194. if getConsoleScreenBufferInfo(h, addr(c)) != 0:
  195. return c.wAttributes
  196. return 0x70'i16 # ERROR: return white background, black text
  197. proc initTerminal(term: PTerminal) =
  198. var hStdoutTemp = getStdHandle(STD_OUTPUT_HANDLE)
  199. if duplicateHandle(getCurrentProcess(), hStdoutTemp, getCurrentProcess(),
  200. addr(term.hStdout), 0, 1, DUPLICATE_SAME_ACCESS) == 0:
  201. when defined(consoleapp):
  202. raiseOSError(osLastError())
  203. var hStderrTemp = getStdHandle(STD_ERROR_HANDLE)
  204. if duplicateHandle(getCurrentProcess(), hStderrTemp, getCurrentProcess(),
  205. addr(term.hStderr), 0, 1, DUPLICATE_SAME_ACCESS) == 0:
  206. when defined(consoleapp):
  207. raiseOSError(osLastError())
  208. term.oldStdoutAttr = getAttributes(term.hStdout)
  209. term.oldStderrAttr = getAttributes(term.hStderr)
  210. template conHandle(f: File): Handle =
  211. let term = getTerminal()
  212. if f == stderr: term.hStderr else: term.hStdout
  213. else:
  214. import termios, posix, os, parseutils
  215. proc setRaw(fd: FileHandle, time: cint = TCSAFLUSH) =
  216. var mode: Termios
  217. discard fd.tcGetAttr(addr mode)
  218. mode.c_iflag = mode.c_iflag and not Cflag(BRKINT or ICRNL or INPCK or
  219. ISTRIP or IXON)
  220. mode.c_oflag = mode.c_oflag and not Cflag(OPOST)
  221. mode.c_cflag = (mode.c_cflag and not Cflag(CSIZE or PARENB)) or CS8
  222. mode.c_lflag = mode.c_lflag and not Cflag(ECHO or ICANON or IEXTEN or ISIG)
  223. mode.c_cc[VMIN] = 1.cuchar
  224. mode.c_cc[VTIME] = 0.cuchar
  225. discard fd.tcSetAttr(time, addr mode)
  226. proc terminalWidthIoctl*(fds: openArray[int]): int =
  227. ## Returns terminal width from first fd that supports the ioctl.
  228. var win: IOctl_WinSize
  229. for fd in fds:
  230. if ioctl(cint(fd), TIOCGWINSZ, addr win) != -1:
  231. return int(win.ws_col)
  232. return 0
  233. proc terminalHeightIoctl*(fds: openArray[int]): int =
  234. ## Returns terminal height from first fd that supports the ioctl.
  235. var win: IOctl_WinSize
  236. for fd in fds:
  237. if ioctl(cint(fd), TIOCGWINSZ, addr win) != -1:
  238. return int(win.ws_row)
  239. return 0
  240. var L_ctermid{.importc, header: "<stdio.h>".}: cint
  241. proc terminalWidth*(): int =
  242. ## Returns some reasonable terminal width from either standard file
  243. ## descriptors, controlling terminal, environment variables or tradition.
  244. var w = terminalWidthIoctl([0, 1, 2]) #Try standard file descriptors
  245. if w > 0: return w
  246. var cterm = newString(L_ctermid) #Try controlling tty
  247. var fd = open(ctermid(cstring(cterm)), O_RDONLY)
  248. if fd != -1:
  249. w = terminalWidthIoctl([int(fd)])
  250. discard close(fd)
  251. if w > 0: return w
  252. var s = getEnv("COLUMNS") #Try standard env var
  253. if len(s) > 0 and parseInt(s, w) > 0 and w > 0:
  254. return w
  255. return 80 #Finally default to venerable value
  256. proc terminalHeight*(): int =
  257. ## Returns some reasonable terminal height from either standard file
  258. ## descriptors, controlling terminal, environment variables or tradition.
  259. ## Zero is returned if the height could not be determined.
  260. var h = terminalHeightIoctl([0, 1, 2]) # Try standard file descriptors
  261. if h > 0: return h
  262. var cterm = newString(L_ctermid) # Try controlling tty
  263. var fd = open(ctermid(cstring(cterm)), O_RDONLY)
  264. if fd != -1:
  265. h = terminalHeightIoctl([int(fd)])
  266. discard close(fd)
  267. if h > 0: return h
  268. var s = getEnv("LINES") # Try standard env var
  269. if len(s) > 0 and parseInt(s, h) > 0 and h > 0:
  270. return h
  271. return 0 # Could not determine height
  272. proc terminalSize*(): tuple[w, h: int] =
  273. ## Returns the terminal width and height as a tuple. Internally calls
  274. ## `terminalWidth` and `terminalHeight`, so the same assumptions apply.
  275. result = (terminalWidth(), terminalHeight())
  276. when defined(windows):
  277. proc setCursorVisibility(f: File, visible: bool) =
  278. var ccsi: CONSOLE_CURSOR_INFO
  279. let h = conHandle(f)
  280. if getConsoleCursorInfo(h, addr(ccsi)) == 0:
  281. raiseOSError(osLastError())
  282. ccsi.bVisible = if visible: 1 else: 0
  283. if setConsoleCursorInfo(h, addr(ccsi)) == 0:
  284. raiseOSError(osLastError())
  285. proc hideCursor*(f: File) =
  286. ## Hides the cursor.
  287. when defined(windows):
  288. setCursorVisibility(f, false)
  289. else:
  290. f.write("\e[?25l")
  291. proc showCursor*(f: File) =
  292. ## Shows the cursor.
  293. when defined(windows):
  294. setCursorVisibility(f, true)
  295. else:
  296. f.write("\e[?25h")
  297. proc setCursorPos*(f: File, x, y: int) =
  298. ## Sets the terminal's cursor to the (x,y) position.
  299. ## (0,0) is the upper left of the screen.
  300. when defined(windows):
  301. let h = conHandle(f)
  302. setCursorPos(h, x, y)
  303. else:
  304. f.write(fmt"{stylePrefix}{y+1};{x+1}f")
  305. proc setCursorXPos*(f: File, x: int) =
  306. ## Sets the terminal's cursor to the x position.
  307. ## The y position is not changed.
  308. when defined(windows):
  309. let h = conHandle(f)
  310. var scrbuf: CONSOLE_SCREEN_BUFFER_INFO
  311. if getConsoleScreenBufferInfo(h, addr(scrbuf)) == 0:
  312. raiseOSError(osLastError())
  313. var origin = scrbuf.dwCursorPosition
  314. origin.x = int16(x)
  315. if setConsoleCursorPosition(h, origin) == 0:
  316. raiseOSError(osLastError())
  317. else:
  318. f.write(fmt"{stylePrefix}{x+1}G")
  319. when defined(windows):
  320. proc setCursorYPos*(f: File, y: int) =
  321. ## Sets the terminal's cursor to the y position.
  322. ## The x position is not changed.
  323. ## .. warning:: This is not supported on UNIX!
  324. when defined(windows):
  325. let h = conHandle(f)
  326. var scrbuf: CONSOLE_SCREEN_BUFFER_INFO
  327. if getConsoleScreenBufferInfo(h, addr(scrbuf)) == 0:
  328. raiseOSError(osLastError())
  329. var origin = scrbuf.dwCursorPosition
  330. origin.y = int16(y)
  331. if setConsoleCursorPosition(h, origin) == 0:
  332. raiseOSError(osLastError())
  333. else:
  334. discard
  335. proc cursorUp*(f: File, count = 1) =
  336. ## Moves the cursor up by `count` rows.
  337. runnableExamples("-r:off"):
  338. stdout.cursorUp(2)
  339. write(stdout, "Hello World!") # anything written at that location will be erased/replaced with this
  340. when defined(windows):
  341. let h = conHandle(f)
  342. var p = getCursorPos(h)
  343. dec(p.y, count)
  344. setCursorPos(h, p.x, p.y)
  345. else:
  346. f.write("\e[" & $count & 'A')
  347. proc cursorDown*(f: File, count = 1) =
  348. ## Moves the cursor down by `count` rows.
  349. runnableExamples("-r:off"):
  350. stdout.cursorDown(2)
  351. write(stdout, "Hello World!") # anything written at that location will be erased/replaced with this
  352. when defined(windows):
  353. let h = conHandle(f)
  354. var p = getCursorPos(h)
  355. inc(p.y, count)
  356. setCursorPos(h, p.x, p.y)
  357. else:
  358. f.write(fmt"{stylePrefix}{count}B")
  359. proc cursorForward*(f: File, count = 1) =
  360. ## Moves the cursor forward by `count` columns.
  361. runnableExamples("-r:off"):
  362. stdout.cursorForward(2)
  363. write(stdout, "Hello World!") # anything written at that location will be erased/replaced with this
  364. when defined(windows):
  365. let h = conHandle(f)
  366. var p = getCursorPos(h)
  367. inc(p.x, count)
  368. setCursorPos(h, p.x, p.y)
  369. else:
  370. f.write(fmt"{stylePrefix}{count}C")
  371. proc cursorBackward*(f: File, count = 1) =
  372. ## Moves the cursor backward by `count` columns.
  373. runnableExamples("-r:off"):
  374. stdout.cursorBackward(2)
  375. write(stdout, "Hello World!") # anything written at that location will be erased/replaced with this
  376. when defined(windows):
  377. let h = conHandle(f)
  378. var p = getCursorPos(h)
  379. dec(p.x, count)
  380. setCursorPos(h, p.x, p.y)
  381. else:
  382. f.write(fmt"{stylePrefix}{count}D")
  383. when true:
  384. discard
  385. else:
  386. proc eraseLineEnd*(f: File) =
  387. ## Erases from the current cursor position to the end of the current line.
  388. when defined(windows):
  389. discard
  390. else:
  391. f.write("\e[K")
  392. proc eraseLineStart*(f: File) =
  393. ## Erases from the current cursor position to the start of the current line.
  394. when defined(windows):
  395. discard
  396. else:
  397. f.write("\e[1K")
  398. proc eraseDown*(f: File) =
  399. ## Erases the screen from the current line down to the bottom of the screen.
  400. when defined(windows):
  401. discard
  402. else:
  403. f.write("\e[J")
  404. proc eraseUp*(f: File) =
  405. ## Erases the screen from the current line up to the top of the screen.
  406. when defined(windows):
  407. discard
  408. else:
  409. f.write("\e[1J")
  410. proc eraseLine*(f: File) =
  411. ## Erases the entire current line.
  412. runnableExamples("-r:off"):
  413. write(stdout, "never mind")
  414. stdout.eraseLine() # nothing will be printed on the screen
  415. when defined(windows):
  416. let h = conHandle(f)
  417. var scrbuf: CONSOLE_SCREEN_BUFFER_INFO
  418. var numwrote: DWORD
  419. if getConsoleScreenBufferInfo(h, addr(scrbuf)) == 0:
  420. raiseOSError(osLastError())
  421. var origin = scrbuf.dwCursorPosition
  422. origin.x = 0'i16
  423. if setConsoleCursorPosition(h, origin) == 0:
  424. raiseOSError(osLastError())
  425. var wt: DWORD = scrbuf.dwSize.x - origin.x
  426. if fillConsoleOutputCharacter(h, ' ', wt,
  427. origin, addr(numwrote)) == 0:
  428. raiseOSError(osLastError())
  429. if fillConsoleOutputAttribute(h, scrbuf.wAttributes, wt,
  430. scrbuf.dwCursorPosition, addr(numwrote)) == 0:
  431. raiseOSError(osLastError())
  432. else:
  433. f.write("\e[2K")
  434. setCursorXPos(f, 0)
  435. proc eraseScreen*(f: File) =
  436. ## Erases the screen with the background colour and moves the cursor to home.
  437. when defined(windows):
  438. let h = conHandle(f)
  439. var scrbuf: CONSOLE_SCREEN_BUFFER_INFO
  440. var numwrote: DWORD
  441. var origin: COORD # is inititalized to 0, 0
  442. if getConsoleScreenBufferInfo(h, addr(scrbuf)) == 0:
  443. raiseOSError(osLastError())
  444. let numChars = int32(scrbuf.dwSize.x)*int32(scrbuf.dwSize.y)
  445. if fillConsoleOutputCharacter(h, ' ', numChars,
  446. origin, addr(numwrote)) == 0:
  447. raiseOSError(osLastError())
  448. if fillConsoleOutputAttribute(h, scrbuf.wAttributes, numChars,
  449. origin, addr(numwrote)) == 0:
  450. raiseOSError(osLastError())
  451. setCursorXPos(f, 0)
  452. else:
  453. f.write("\e[2J")
  454. when not defined(windows):
  455. var
  456. gFG {.threadvar.}: int
  457. gBG {.threadvar.}: int
  458. proc resetAttributes*(f: File) =
  459. ## Resets all attributes.
  460. when defined(windows):
  461. let term = getTerminal()
  462. if f == stderr:
  463. discard setConsoleTextAttribute(term.hStderr, term.oldStderrAttr)
  464. else:
  465. discard setConsoleTextAttribute(term.hStdout, term.oldStdoutAttr)
  466. else:
  467. f.write(ansiResetCode)
  468. gFG = 0
  469. gBG = 0
  470. type
  471. Style* = enum ## Different styles for text output.
  472. styleBright = 1, ## bright text
  473. styleDim, ## dim text
  474. styleItalic, ## italic (or reverse on terminals not supporting)
  475. styleUnderscore, ## underscored text
  476. styleBlink, ## blinking/bold text
  477. styleBlinkRapid, ## rapid blinking/bold text (not widely supported)
  478. styleReverse, ## reverse
  479. styleHidden, ## hidden text
  480. styleStrikethrough ## strikethrough
  481. proc ansiStyleCode*(style: int): string =
  482. result = fmt"{stylePrefix}{style}m"
  483. template ansiStyleCode*(style: Style): string =
  484. ansiStyleCode(style.int)
  485. # The styleCache can be skipped when `style` is known at compile-time
  486. template ansiStyleCode*(style: static[Style]): string =
  487. (static(stylePrefix & $style.int & "m"))
  488. proc setStyle*(f: File, style: set[Style]) =
  489. ## Sets the terminal style.
  490. when defined(windows):
  491. let h = conHandle(f)
  492. var old = getAttributes(h) and (FOREGROUND_RGB or BACKGROUND_RGB)
  493. var a = 0'i16
  494. if styleBright in style: a = a or int16(FOREGROUND_INTENSITY)
  495. if styleBlink in style: a = a or int16(BACKGROUND_INTENSITY)
  496. if styleReverse in style: a = a or 0x4000'i16 # COMMON_LVB_REVERSE_VIDEO
  497. if styleUnderscore in style: a = a or 0x8000'i16 # COMMON_LVB_UNDERSCORE
  498. discard setConsoleTextAttribute(h, old or a)
  499. else:
  500. for s in items(style):
  501. f.write(ansiStyleCode(s))
  502. proc writeStyled*(txt: string, style: set[Style] = {styleBright}) =
  503. ## Writes the text `txt` in a given `style` to stdout.
  504. when defined(windows):
  505. let term = getTerminal()
  506. var old = getAttributes(term.hStdout)
  507. stdout.setStyle(style)
  508. stdout.write(txt)
  509. discard setConsoleTextAttribute(term.hStdout, old)
  510. else:
  511. stdout.setStyle(style)
  512. stdout.write(txt)
  513. stdout.resetAttributes()
  514. if gFG != 0:
  515. stdout.write(ansiStyleCode(gFG))
  516. if gBG != 0:
  517. stdout.write(ansiStyleCode(gBG))
  518. type
  519. ForegroundColor* = enum ## Terminal's foreground colors.
  520. fgBlack = 30, ## black
  521. fgRed, ## red
  522. fgGreen, ## green
  523. fgYellow, ## yellow
  524. fgBlue, ## blue
  525. fgMagenta, ## magenta
  526. fgCyan, ## cyan
  527. fgWhite, ## white
  528. fg8Bit, ## 256-color (not supported, see `enableTrueColors` instead.)
  529. fgDefault ## default terminal foreground color
  530. BackgroundColor* = enum ## Terminal's background colors.
  531. bgBlack = 40, ## black
  532. bgRed, ## red
  533. bgGreen, ## green
  534. bgYellow, ## yellow
  535. bgBlue, ## blue
  536. bgMagenta, ## magenta
  537. bgCyan, ## cyan
  538. bgWhite, ## white
  539. bg8Bit, ## 256-color (not supported, see `enableTrueColors` instead.)
  540. bgDefault ## default terminal background color
  541. when defined(windows):
  542. var defaultForegroundColor, defaultBackgroundColor: int16 = 0xFFFF'i16 # Default to an invalid value 0xFFFF
  543. proc setForegroundColor*(f: File, fg: ForegroundColor, bright = false) =
  544. ## Sets the terminal's foreground color.
  545. when defined(windows):
  546. let h = conHandle(f)
  547. var old = getAttributes(h) and not FOREGROUND_RGB
  548. if defaultForegroundColor == 0xFFFF'i16:
  549. defaultForegroundColor = old
  550. old = if bright: old or FOREGROUND_INTENSITY
  551. else: old and not(FOREGROUND_INTENSITY)
  552. const lookup: array[ForegroundColor, int] = [
  553. 0, # ForegroundColor enum with ordinal 30
  554. (FOREGROUND_RED),
  555. (FOREGROUND_GREEN),
  556. (FOREGROUND_RED or FOREGROUND_GREEN),
  557. (FOREGROUND_BLUE),
  558. (FOREGROUND_RED or FOREGROUND_BLUE),
  559. (FOREGROUND_BLUE or FOREGROUND_GREEN),
  560. (FOREGROUND_BLUE or FOREGROUND_GREEN or FOREGROUND_RED),
  561. 0, # fg8Bit not supported, see `enableTrueColors` instead.
  562. 0] # unused
  563. if fg == fgDefault:
  564. discard setConsoleTextAttribute(h, cast[int16](cast[uint16](old) or cast[uint16](defaultForegroundColor)))
  565. else:
  566. discard setConsoleTextAttribute(h, cast[int16](cast[uint16](old) or cast[uint16](lookup[fg])))
  567. else:
  568. gFG = ord(fg)
  569. if bright: inc(gFG, 60)
  570. f.write(ansiStyleCode(gFG))
  571. proc setBackgroundColor*(f: File, bg: BackgroundColor, bright = false) =
  572. ## Sets the terminal's background color.
  573. when defined(windows):
  574. let h = conHandle(f)
  575. var old = getAttributes(h) and not BACKGROUND_RGB
  576. if defaultBackgroundColor == 0xFFFF'i16:
  577. defaultBackgroundColor = old
  578. old = if bright: old or BACKGROUND_INTENSITY
  579. else: old and not(BACKGROUND_INTENSITY)
  580. const lookup: array[BackgroundColor, int] = [
  581. 0, # BackgroundColor enum with ordinal 40
  582. (BACKGROUND_RED),
  583. (BACKGROUND_GREEN),
  584. (BACKGROUND_RED or BACKGROUND_GREEN),
  585. (BACKGROUND_BLUE),
  586. (BACKGROUND_RED or BACKGROUND_BLUE),
  587. (BACKGROUND_BLUE or BACKGROUND_GREEN),
  588. (BACKGROUND_BLUE or BACKGROUND_GREEN or BACKGROUND_RED),
  589. 0, # bg8Bit not supported, see `enableTrueColors` instead.
  590. 0] # unused
  591. if bg == bgDefault:
  592. discard setConsoleTextAttribute(h, cast[int16](cast[uint16](old) or cast[uint16](defaultBackgroundColor)))
  593. else:
  594. discard setConsoleTextAttribute(h, cast[int16](cast[uint16](old) or cast[uint16](lookup[bg])))
  595. else:
  596. gBG = ord(bg)
  597. if bright: inc(gBG, 60)
  598. f.write(ansiStyleCode(gBG))
  599. proc ansiForegroundColorCode*(fg: ForegroundColor, bright = false): string =
  600. var style = ord(fg)
  601. if bright: inc(style, 60)
  602. return ansiStyleCode(style)
  603. template ansiForegroundColorCode*(fg: static[ForegroundColor],
  604. bright: static[bool] = false): string =
  605. ansiStyleCode(fg.int + bright.int * 60)
  606. proc ansiForegroundColorCode*(color: Color): string =
  607. let rgb = extractRGB(color)
  608. result = fmt"{fgPrefix}{rgb.r};{rgb.g};{rgb.b}m"
  609. template ansiForegroundColorCode*(color: static[Color]): string =
  610. const rgb = extractRGB(color)
  611. # no usage of `fmt`, see issue #7632
  612. (static("$1$2;$3;$4m" % [$fgPrefix, $(rgb.r), $(rgb.g), $(rgb.b)]))
  613. proc ansiBackgroundColorCode*(color: Color): string =
  614. let rgb = extractRGB(color)
  615. result = fmt"{bgPrefix}{rgb.r};{rgb.g};{rgb.b}m"
  616. template ansiBackgroundColorCode*(color: static[Color]): string =
  617. const rgb = extractRGB(color)
  618. # no usage of `fmt`, see issue #7632
  619. (static("$1$2;$3;$4m" % [$bgPrefix, $(rgb.r), $(rgb.g), $(rgb.b)]))
  620. proc setForegroundColor*(f: File, color: Color) =
  621. ## Sets the terminal's foreground true color.
  622. if getTerminal().trueColorIsEnabled:
  623. f.write(ansiForegroundColorCode(color))
  624. proc setBackgroundColor*(f: File, color: Color) =
  625. ## Sets the terminal's background true color.
  626. if getTerminal().trueColorIsEnabled:
  627. f.write(ansiBackgroundColorCode(color))
  628. proc setTrueColor(f: File, color: Color) =
  629. let term = getTerminal()
  630. if term.fgSetColor:
  631. setForegroundColor(f, color)
  632. else:
  633. setBackgroundColor(f, color)
  634. proc isatty*(f: File): bool =
  635. ## Returns true if `f` is associated with a terminal device.
  636. when defined(posix):
  637. proc isatty(fildes: FileHandle): cint {.
  638. importc: "isatty", header: "<unistd.h>".}
  639. else:
  640. proc isatty(fildes: FileHandle): cint {.
  641. importc: "_isatty", header: "<io.h>".}
  642. result = isatty(getFileHandle(f)) != 0'i32
  643. type
  644. TerminalCmd* = enum ## commands that can be expressed as arguments
  645. resetStyle, ## reset attributes
  646. fgColor, ## set foreground's true color
  647. bgColor ## set background's true color
  648. template styledEchoProcessArg(f: File, s: string) = write f, s
  649. template styledEchoProcessArg(f: File, style: Style) = setStyle(f, {style})
  650. template styledEchoProcessArg(f: File, style: set[Style]) = setStyle f, style
  651. template styledEchoProcessArg(f: File, color: ForegroundColor) =
  652. setForegroundColor f, color
  653. template styledEchoProcessArg(f: File, color: BackgroundColor) =
  654. setBackgroundColor f, color
  655. template styledEchoProcessArg(f: File, color: Color) =
  656. setTrueColor f, color
  657. template styledEchoProcessArg(f: File, cmd: TerminalCmd) =
  658. when cmd == resetStyle:
  659. resetAttributes(f)
  660. elif cmd in {fgColor, bgColor}:
  661. let term = getTerminal()
  662. term.fgSetColor = cmd == fgColor
  663. macro styledWrite*(f: File, m: varargs[typed]): untyped =
  664. ## Similar to `write`, but treating terminal style arguments specially.
  665. ## When some argument is `Style`, `set[Style]`, `ForegroundColor`,
  666. ## `BackgroundColor` or `TerminalCmd` then it is not sent directly to
  667. ## `f`, but instead corresponding terminal style proc is called.
  668. runnableExamples("-r:off"):
  669. stdout.styledWrite(fgRed, "red text ")
  670. stdout.styledWrite(fgGreen, "green text")
  671. var reset = false
  672. result = newNimNode(nnkStmtList)
  673. for i in countup(0, m.len - 1):
  674. let item = m[i]
  675. case item.kind
  676. of nnkStrLit..nnkTripleStrLit:
  677. if i == m.len - 1:
  678. # optimize if string literal is last, just call write
  679. result.add(newCall(bindSym"write", f, item))
  680. if reset: result.add(newCall(bindSym"resetAttributes", f))
  681. return
  682. else:
  683. # if it is string literal just call write, do not enable reset
  684. result.add(newCall(bindSym"write", f, item))
  685. else:
  686. result.add(newCall(bindSym"styledEchoProcessArg", f, item))
  687. reset = true
  688. if reset: result.add(newCall(bindSym"resetAttributes", f))
  689. template styledWriteLine*(f: File, args: varargs[untyped]) =
  690. ## Calls `styledWrite` and appends a newline at the end.
  691. runnableExamples:
  692. proc error(msg: string) =
  693. styledWriteLine(stderr, fgRed, "Error: ", resetStyle, msg)
  694. styledWrite(f, args)
  695. write(f, "\n")
  696. template styledEcho*(args: varargs[untyped]) =
  697. ## Echoes styles arguments to stdout using `styledWriteLine`.
  698. stdout.styledWriteLine(args)
  699. proc getch*(): char =
  700. ## Reads a single character from the terminal, blocking until it is entered.
  701. ## The character is not printed to the terminal.
  702. when defined(windows):
  703. let fd = getStdHandle(STD_INPUT_HANDLE)
  704. var keyEvent = KEY_EVENT_RECORD()
  705. var numRead: cint
  706. while true:
  707. # Block until character is entered
  708. doAssert(waitForSingleObject(fd, INFINITE) == WAIT_OBJECT_0)
  709. doAssert(readConsoleInput(fd, addr(keyEvent), 1, addr(numRead)) != 0)
  710. if numRead == 0 or keyEvent.eventType != 1 or keyEvent.bKeyDown == 0:
  711. continue
  712. return char(keyEvent.uChar)
  713. else:
  714. let fd = getFileHandle(stdin)
  715. var oldMode: Termios
  716. discard fd.tcGetAttr(addr oldMode)
  717. fd.setRaw()
  718. result = stdin.readChar()
  719. discard fd.tcSetAttr(TCSADRAIN, addr oldMode)
  720. when defined(windows):
  721. proc readPasswordFromStdin*(prompt: string, password: var string):
  722. bool {.tags: [ReadIOEffect, WriteIOEffect].} =
  723. ## Reads a `password` from stdin without printing it. `password` must not
  724. ## be `nil`! Returns `false` if the end of the file has been reached,
  725. ## `true` otherwise.
  726. password.setLen(0)
  727. stdout.write(prompt)
  728. let hi = createFileA("CONIN$",
  729. GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, 0, 0)
  730. var mode = DWORD 0
  731. discard getConsoleMode(hi, addr mode)
  732. let origMode = mode
  733. const
  734. ENABLE_PROCESSED_INPUT = 1
  735. ENABLE_ECHO_INPUT = 4
  736. mode = (mode or ENABLE_PROCESSED_INPUT) and not ENABLE_ECHO_INPUT
  737. discard setConsoleMode(hi, mode)
  738. result = readLine(stdin, password)
  739. discard setConsoleMode(hi, origMode)
  740. discard closeHandle(hi)
  741. stdout.write "\n"
  742. else:
  743. import termios
  744. proc readPasswordFromStdin*(prompt: string, password: var string):
  745. bool {.tags: [ReadIOEffect, WriteIOEffect].} =
  746. password.setLen(0)
  747. let fd = stdin.getFileHandle()
  748. var cur, old: Termios
  749. discard fd.tcGetAttr(cur.addr)
  750. old = cur
  751. cur.c_lflag = cur.c_lflag and not Cflag(ECHO)
  752. discard fd.tcSetAttr(TCSADRAIN, cur.addr)
  753. stdout.write prompt
  754. result = stdin.readLine(password)
  755. stdout.write "\n"
  756. discard fd.tcSetAttr(TCSADRAIN, old.addr)
  757. proc readPasswordFromStdin*(prompt = "password: "): string =
  758. ## Reads a password from stdin without printing it.
  759. result = ""
  760. discard readPasswordFromStdin(prompt, result)
  761. # Wrappers assuming output to stdout:
  762. template hideCursor*() = hideCursor(stdout)
  763. template showCursor*() = showCursor(stdout)
  764. template setCursorPos*(x, y: int) = setCursorPos(stdout, x, y)
  765. template setCursorXPos*(x: int) = setCursorXPos(stdout, x)
  766. when defined(windows):
  767. template setCursorYPos*(x: int) = setCursorYPos(stdout, x)
  768. template cursorUp*(count = 1) = cursorUp(stdout, count)
  769. template cursorDown*(count = 1) = cursorDown(stdout, count)
  770. template cursorForward*(count = 1) = cursorForward(stdout, count)
  771. template cursorBackward*(count = 1) = cursorBackward(stdout, count)
  772. template eraseLine*() = eraseLine(stdout)
  773. template eraseScreen*() = eraseScreen(stdout)
  774. template setStyle*(style: set[Style]) =
  775. setStyle(stdout, style)
  776. template setForegroundColor*(fg: ForegroundColor, bright = false) =
  777. setForegroundColor(stdout, fg, bright)
  778. template setBackgroundColor*(bg: BackgroundColor, bright = false) =
  779. setBackgroundColor(stdout, bg, bright)
  780. template setForegroundColor*(color: Color) =
  781. setForegroundColor(stdout, color)
  782. template setBackgroundColor*(color: Color) =
  783. setBackgroundColor(stdout, color)
  784. proc resetAttributes*() {.noconv.} =
  785. ## Resets all attributes on stdout.
  786. ## It is advisable to register this as a quit proc with
  787. ## `exitprocs.addExitProc(resetAttributes)`.
  788. resetAttributes(stdout)
  789. proc isTrueColorSupported*(): bool =
  790. ## Returns true if a terminal supports true color.
  791. return getTerminal().trueColorIsSupported
  792. when defined(windows):
  793. import os
  794. proc enableTrueColors*() =
  795. ## Enables true color.
  796. var term = getTerminal()
  797. when defined(windows):
  798. var
  799. ver: OSVERSIONINFO
  800. ver.dwOSVersionInfoSize = sizeof(ver).DWORD
  801. let res = getVersionExW(addr ver)
  802. if res == 0:
  803. term.trueColorIsSupported = false
  804. else:
  805. term.trueColorIsSupported = ver.dwMajorVersion > 10 or
  806. (ver.dwMajorVersion == 10 and (ver.dwMinorVersion > 0 or
  807. (ver.dwMinorVersion == 0 and ver.dwBuildNumber >= 10586)))
  808. if not term.trueColorIsSupported:
  809. term.trueColorIsSupported = getEnv("ANSICON_DEF").len > 0
  810. if term.trueColorIsSupported:
  811. if getEnv("ANSICON_DEF").len == 0:
  812. var mode: DWORD = 0
  813. if getConsoleMode(getStdHandle(STD_OUTPUT_HANDLE), addr(mode)) != 0:
  814. mode = mode or ENABLE_VIRTUAL_TERMINAL_PROCESSING
  815. if setConsoleMode(getStdHandle(STD_OUTPUT_HANDLE), mode) != 0:
  816. term.trueColorIsEnabled = true
  817. else:
  818. term.trueColorIsEnabled = false
  819. else:
  820. term.trueColorIsEnabled = true
  821. else:
  822. term.trueColorIsSupported = getEnv("COLORTERM").toLowerAscii() in [
  823. "truecolor", "24bit"]
  824. term.trueColorIsEnabled = term.trueColorIsSupported
  825. proc disableTrueColors*() =
  826. ## Disables true color.
  827. var term = getTerminal()
  828. when defined(windows):
  829. if term.trueColorIsSupported:
  830. if getEnv("ANSICON_DEF").len == 0:
  831. var mode: DWORD = 0
  832. if getConsoleMode(getStdHandle(STD_OUTPUT_HANDLE), addr(mode)) != 0:
  833. mode = mode and not ENABLE_VIRTUAL_TERMINAL_PROCESSING
  834. discard setConsoleMode(getStdHandle(STD_OUTPUT_HANDLE), mode)
  835. term.trueColorIsEnabled = false
  836. else:
  837. term.trueColorIsEnabled = false
  838. proc newTerminal(): owned(PTerminal) =
  839. new result
  840. when defined(windows):
  841. initTerminal(result)