Add terminalHeight, terminalHeightIoctl & terminalSize
This commit is contained in:
parent
b1b2dd606b
commit
3fcb8a3246
2 changed files with 50 additions and 1 deletions
|
|
@ -83,6 +83,13 @@ when defined(windows):
|
||||||
return int(csbi.srWindow.Right - csbi.srWindow.Left + 1)
|
return int(csbi.srWindow.Right - csbi.srWindow.Left + 1)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
proc terminalHeightIoctl*(handles: openArray[Handle]): int =
|
||||||
|
var csbi: CONSOLE_SCREEN_BUFFER_INFO
|
||||||
|
for h in handles:
|
||||||
|
if getConsoleScreenBufferInfo(h, addr csbi) != 0:
|
||||||
|
return int(csbi.srWindow.Bottom - csbi.srWindow.Top + 1)
|
||||||
|
return 0
|
||||||
|
|
||||||
proc terminalWidth*(): int =
|
proc terminalWidth*(): int =
|
||||||
var w: int = 0
|
var w: int = 0
|
||||||
w = terminalWidthIoctl([ getStdHandle(STD_INPUT_HANDLE),
|
w = terminalWidthIoctl([ getStdHandle(STD_INPUT_HANDLE),
|
||||||
|
|
@ -91,6 +98,14 @@ when defined(windows):
|
||||||
if w > 0: return w
|
if w > 0: return w
|
||||||
return 80
|
return 80
|
||||||
|
|
||||||
|
proc terminalHeight*(): int =
|
||||||
|
var h: int = 0
|
||||||
|
h = terminalHeightIoctl([ getStdHandle(STD_INPUT_HANDLE),
|
||||||
|
getStdHandle(STD_OUTPUT_HANDLE),
|
||||||
|
getStdHandle(STD_ERROR_HANDLE) ] )
|
||||||
|
if h > 0: return h
|
||||||
|
return 0
|
||||||
|
|
||||||
proc setConsoleCursorPosition(hConsoleOutput: HANDLE,
|
proc setConsoleCursorPosition(hConsoleOutput: HANDLE,
|
||||||
dwCursorPosition: COORD): WINBOOL{.
|
dwCursorPosition: COORD): WINBOOL{.
|
||||||
stdcall, dynlib: "kernel32", importc: "SetConsoleCursorPosition".}
|
stdcall, dynlib: "kernel32", importc: "SetConsoleCursorPosition".}
|
||||||
|
|
@ -177,7 +192,17 @@ else:
|
||||||
return int(win.ws_col)
|
return int(win.ws_col)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
proc terminalHeightIoctl*(fds: openArray[int]): int =
|
||||||
|
## Returns terminal height from first fd that supports the ioctl.
|
||||||
|
|
||||||
|
var win: IOctl_WinSize
|
||||||
|
for fd in fds:
|
||||||
|
if ioctl(cint(fd), TIOCGWINSZ, addr win) != -1:
|
||||||
|
return int(win.ws_row)
|
||||||
|
return 0
|
||||||
|
|
||||||
var L_ctermid{.importc, header: "<stdio.h>".}: cint
|
var L_ctermid{.importc, header: "<stdio.h>".}: cint
|
||||||
|
|
||||||
proc terminalWidth*(): int =
|
proc terminalWidth*(): int =
|
||||||
## Returns some reasonable terminal width from either standard file
|
## Returns some reasonable terminal width from either standard file
|
||||||
## descriptors, controlling terminal, environment variables or tradition.
|
## descriptors, controlling terminal, environment variables or tradition.
|
||||||
|
|
@ -195,6 +220,29 @@ else:
|
||||||
return w
|
return w
|
||||||
return 80 #Finally default to venerable value
|
return 80 #Finally default to venerable value
|
||||||
|
|
||||||
|
proc terminalHeight*(): int =
|
||||||
|
## Returns some reasonable terminal height from either standard file
|
||||||
|
## descriptors, controlling terminal, environment variables or tradition.
|
||||||
|
## Zero is returned if the height could not be determined.
|
||||||
|
|
||||||
|
var h = terminalHeightIoctl([0, 1, 2]) # Try standard file descriptors
|
||||||
|
if h > 0: return h
|
||||||
|
var cterm = newString(L_ctermid) # Try controlling tty
|
||||||
|
var fd = open(ctermid(cstring(cterm)), O_RDONLY)
|
||||||
|
if fd != -1:
|
||||||
|
h = terminalHeightIoctl([ int(fd) ])
|
||||||
|
discard close(fd)
|
||||||
|
if h > 0: return h
|
||||||
|
var s = getEnv("LINES") # Try standard env var
|
||||||
|
if len(s) > 0 and parseInt(string(s), h) > 0 and h > 0:
|
||||||
|
return h
|
||||||
|
return 0 # Could not determine height
|
||||||
|
|
||||||
|
proc terminalSize*(): tuple[w, h: int] =
|
||||||
|
## Returns the terminal width and height as a tuple. Internally calls
|
||||||
|
## `terminalWidth` and `terminalHeight`, so the same assumptions apply.
|
||||||
|
result = (terminalWidth(), terminalHeight())
|
||||||
|
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
proc setCursorVisibility(f: File, visible: bool) =
|
proc setCursorVisibility(f: File, visible: bool) =
|
||||||
var ccsi: CONSOLE_CURSOR_INFO
|
var ccsi: CONSOLE_CURSOR_INFO
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,8 @@ 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``
|
- Added ``hideCursor``, ``showCursor``, ``terminalWidth``,
|
||||||
|
``terminalWidthIoctl`` and ``terminalSize`` to the ``terminal``
|
||||||
`(doc) <http://nim-lang.org/docs/terminal.html>`_ module.
|
`(doc) <http://nim-lang.org/docs/terminal.html>`_ module.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue