Merge pull request #5060 from johnnovak/terminal-showhide-cursor
Add hideCursor & showCursor
This commit is contained in:
commit
6a83306b5f
2 changed files with 44 additions and 0 deletions
|
|
@ -13,6 +13,8 @@
|
||||||
## Windows API.
|
## Windows API.
|
||||||
## Changing the style is permanent even after program termination! Use the
|
## Changing the style is permanent even after program termination! Use the
|
||||||
## code ``system.addQuitProc(resetAttributes)`` to restore the defaults.
|
## code ``system.addQuitProc(resetAttributes)`` to restore the defaults.
|
||||||
|
## Similarly, if you hide the cursor, make sure to unhide it with
|
||||||
|
## ``showCursor`` before quitting.
|
||||||
|
|
||||||
import macros
|
import macros
|
||||||
|
|
||||||
|
|
@ -49,6 +51,10 @@ when defined(windows):
|
||||||
srWindow: SMALL_RECT
|
srWindow: SMALL_RECT
|
||||||
dwMaximumWindowSize: COORD
|
dwMaximumWindowSize: COORD
|
||||||
|
|
||||||
|
CONSOLE_CURSOR_INFO = object
|
||||||
|
dwSize: DWORD
|
||||||
|
bVisible: WINBOOL
|
||||||
|
|
||||||
proc duplicateHandle(hSourceProcessHandle: HANDLE, hSourceHandle: HANDLE,
|
proc duplicateHandle(hSourceProcessHandle: HANDLE, hSourceHandle: HANDLE,
|
||||||
hTargetProcessHandle: HANDLE, lpTargetHandle: ptr HANDLE,
|
hTargetProcessHandle: HANDLE, lpTargetHandle: ptr HANDLE,
|
||||||
dwDesiredAccess: DWORD, bInheritHandle: WINBOOL,
|
dwDesiredAccess: DWORD, bInheritHandle: WINBOOL,
|
||||||
|
|
@ -60,6 +66,14 @@ when defined(windows):
|
||||||
lpConsoleScreenBufferInfo: ptr CONSOLE_SCREEN_BUFFER_INFO): WINBOOL{.stdcall,
|
lpConsoleScreenBufferInfo: ptr CONSOLE_SCREEN_BUFFER_INFO): WINBOOL{.stdcall,
|
||||||
dynlib: "kernel32", importc: "GetConsoleScreenBufferInfo".}
|
dynlib: "kernel32", importc: "GetConsoleScreenBufferInfo".}
|
||||||
|
|
||||||
|
proc getConsoleCursorInfo(hConsoleOutput: HANDLE,
|
||||||
|
lpConsoleCursorInfo: ptr CONSOLE_CURSOR_INFO): WINBOOL{.
|
||||||
|
stdcall, dynlib: "kernel32", importc: "GetConsoleCursorInfo".}
|
||||||
|
|
||||||
|
proc setConsoleCursorInfo(hConsoleOutput: HANDLE,
|
||||||
|
lpConsoleCursorInfo: ptr CONSOLE_CURSOR_INFO): WINBOOL{.
|
||||||
|
stdcall, dynlib: "kernel32", importc: "SetConsoleCursorInfo".}
|
||||||
|
|
||||||
proc terminalWidthIoctl*(handles: openArray[Handle]): int =
|
proc terminalWidthIoctl*(handles: openArray[Handle]): int =
|
||||||
var csbi: CONSOLE_SCREEN_BUFFER_INFO
|
var csbi: CONSOLE_SCREEN_BUFFER_INFO
|
||||||
for h in handles:
|
for h in handles:
|
||||||
|
|
@ -179,6 +193,30 @@ else:
|
||||||
return w
|
return w
|
||||||
return 80 #Finally default to venerable value
|
return 80 #Finally default to venerable value
|
||||||
|
|
||||||
|
when defined(windows):
|
||||||
|
proc setCursorVisibility(f: File, visible: bool) =
|
||||||
|
var ccsi: CONSOLE_CURSOR_INFO
|
||||||
|
let h = conHandle(f)
|
||||||
|
if getConsoleCursorInfo(h, addr(ccsi)) == 0:
|
||||||
|
raiseOSError(osLastError())
|
||||||
|
ccsi.bVisible = if visible: 1 else: 0
|
||||||
|
if setConsoleCursorInfo(h, addr(ccsi)) == 0:
|
||||||
|
raiseOSError(osLastError())
|
||||||
|
|
||||||
|
proc hideCursor*(f: File) =
|
||||||
|
## Hides the cursor.
|
||||||
|
when defined(windows):
|
||||||
|
setCursorVisibility(f, false)
|
||||||
|
else:
|
||||||
|
f.write("\e[?25l")
|
||||||
|
|
||||||
|
proc showCursor*(f: File) =
|
||||||
|
## Shows the cursor.
|
||||||
|
when defined(windows):
|
||||||
|
setCursorVisibility(f, true)
|
||||||
|
else:
|
||||||
|
f.write("\e[?25h")
|
||||||
|
|
||||||
proc setCursorPos*(f: File, x, y: int) =
|
proc setCursorPos*(f: File, x, y: int) =
|
||||||
## Sets the terminal's cursor to the (x,y) position.
|
## Sets the terminal's cursor to the (x,y) position.
|
||||||
## (0,0) is the upper left of the screen.
|
## (0,0) is the upper left of the screen.
|
||||||
|
|
@ -558,6 +596,8 @@ proc getch*(): char =
|
||||||
discard fd.tcsetattr(TCSADRAIN, addr oldMode)
|
discard fd.tcsetattr(TCSADRAIN, addr oldMode)
|
||||||
|
|
||||||
# Wrappers assuming output to stdout:
|
# Wrappers assuming output to stdout:
|
||||||
|
template hideCursor*() = hideCursor(stdout)
|
||||||
|
template showCursor*() = showCursor(stdout)
|
||||||
template setCursorPos*(x, y: int) = setCursorPos(stdout, x, y)
|
template setCursorPos*(x, y: int) = setCursorPos(stdout, x, y)
|
||||||
template setCursorXPos*(x: int) = setCursorXPos(stdout, x)
|
template setCursorXPos*(x: int) = setCursorXPos(stdout, x)
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,10 @@ Library Additions
|
||||||
``deques`` provides a superset of ``queues`` API with clear naming.
|
``deques`` provides a superset of ``queues`` API with clear naming.
|
||||||
``queues`` module is now deprecated and will be removed in the future.
|
``queues`` module is now deprecated and will be removed in the future.
|
||||||
|
|
||||||
|
- Added ``hideCursor`` and ``showCursor`` to the ``terminal``
|
||||||
|
`(doc) <http://nim-lang.org/docs/terminal.html>`_ module.
|
||||||
|
|
||||||
|
|
||||||
Tool Additions
|
Tool Additions
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue