terminal.nim 33 KB

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