summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorCharles Blake <cb@cblake.net>2016-09-01 07:34:42 -0400
committerCharles Blake <cb@cblake.net>2016-09-01 07:34:42 -0400
commit19f83ccd0bf03dea93698a5611c881f57499ca12 (patch)
treece07b15040f963135cc2fcc39dcd4f6cd16f8cc9 /lib/pure
parent0cb90f3eaf482415662091a62b06aceb53d4b1f5 (diff)
downloadNim-19f83ccd0bf03dea93698a5611c881f57499ca12.tar.gz
Add terminal.terminalWidth and supporting types/calls.
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/terminal.nim45
1 files changed, 44 insertions, 1 deletions
diff --git a/lib/pure/terminal.nim b/lib/pure/terminal.nim
index 1f34ec07e..329c50f3e 100644
--- a/lib/pure/terminal.nim
+++ b/lib/pure/terminal.nim
@@ -60,6 +60,22 @@ when defined(windows):
     lpConsoleScreenBufferInfo: ptr CONSOLE_SCREEN_BUFFER_INFO): WINBOOL{.stdcall,
     dynlib: "kernel32", importc: "GetConsoleScreenBufferInfo".}
 
+  proc terminalWidth*(h: Handle): int =
+    var csbi: CONSOLE_SCREEN_BUFFER_INFO
+    if getConsoleScreenBufferInfo(h, addr csbi) != 0:
+      return int(csbi.srWindow.Right - csbi.srWindow.Left + 1)
+    return 0
+
+  proc terminalWidth*(): int =
+    var w: int = 0
+    w = terminalWidth(getStdHandle(STD_INPUT_HANDLE))
+    if w > 0: return w
+    w = terminalWidth(getStdHandle(STD_OUTPUT_HANDLE))
+    if w > 0: return w
+    w = terminalWidth(getStdHandle(STD_ERROR_HANDLE))
+    if w > 0: return w
+    return 80
+
   proc setConsoleCursorPosition(hConsoleOutput: HANDLE,
                                 dwCursorPosition: COORD): WINBOOL{.
       stdcall, dynlib: "kernel32", importc: "SetConsoleCursorPosition".}
@@ -123,7 +139,7 @@ when defined(windows):
     if f == stderr: hStderr else: hStdout
 
 else:
-  import termios
+  import termios, posix, os, parseutils
 
   proc setRaw(fd: FileHandle, time: cint = TCSAFLUSH) =
     var mode: Termios
@@ -137,6 +153,33 @@ else:
     mode.c_cc[VTIME] = 0.cuchar
     discard fd.tcsetattr(time, addr mode)
 
+  proc terminalWidthIoctl*(fds: openArray[int]): int =
+    ## Returns terminal width 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_col)
+    return 0
+
+  proc terminalWidth*(): int =
+    ## Returns **some** reasonable terminal width from either standard file
+    ## descriptors, controlling terminal, environment variables or tradition.
+
+    var L_ctermid{.importc, header: "<stdio.h>".}: cint
+    var w = terminalWidthIoctl([0, 1, 2])   #Try standard file descriptors
+    if w > 0: return w
+    var cterm = newString(L_ctermid)        #Try controlling tty
+    var fd = open(ctermid(cstring(cterm)), O_RDONLY)
+    if fd != -1:
+      w = terminalWidthIoctl([ int(fd) ])
+    discard close(fd)
+    if w > 0: return w
+    var s = getEnv("COLUMNS")               #Try standard env var
+    if len(s) > 0 and parseInt(s, w) > 0 and w > 0:
+      return w
+    return 80                               #Finally default to venerable value
+
 proc setCursorPos*(f: File, x, y: int) =
   ## Sets the terminal's cursor to the (x,y) position.
   ## (0,0) is the upper left of the screen.