all repos — minline @ a049ece45f3ceee22c6dc0a31ae9bef5691026d3

A minimalist but highly-customizable line editing library.

Using native getch.
h3rald h3rald@h3rald.com
Sun, 04 Jun 2017 10:23:32 +0200
commit

a049ece45f3ceee22c6dc0a31ae9bef5691026d3

parent

fef435658bd0578e24aa33686836cd5a29a8bb22

1 files changed, 3 insertions(+), 69 deletions(-)

jump to
M nimline.nimnimline.nim

@@ -6,79 +6,13 @@ sequtils,

strutils, os - -### Start getCh implementation -### Modified from https://github.com/6A/getch/blob/master/getch.nim -when hostOS == "windows": - proc wgetch(): char {. header: "<conio.h>", importc: "getch" .} - proc wgetche(): char {. header: "<conio.h>", importc: "getche" .} - proc putchr(c: char): cint {.discardable, header: "<conio.h>", importc: "putch" .} - - proc getCh*(e = false): char = - ## Immediately get the next character from stdin. - ## If `e` is true, the character will be echoed. - if e: wgetche() else: wgetch() - -else: - const - NCCS = 20 - TCSANOW = 0 - ISIG: uint = 1 - ICANON: uint = 2 - ECHO: uint = 10 - IEXTEN: uint = 0o000000100000 - - type termios = object - c_iflag*: uint - c_oflag*: uint - c_cflag*: uint - c_lflag*: uint - c_line*: char - c_cc*: array[NCCS, char] - - proc tcgetattr(f: int, t: ptr termios): void {. header: "<termios.h>", importc: "tcgetattr" .} - proc tcsetattr(f: int, s: int, t: ptr termios): void {. header: "<termios.h>", importc: "tcsetattr" .} - proc getchar(): char {.header: "<stdio.h>", importc: "getchar" .} - proc putchar(c: char): cint {.discardable, header: "<stdio.h>", importc: "putchar" .} - - proc getCh*(e = false): char = - ## Immediately get the next character from stdin. - ## If `e` is true, the character will be echoed. - var - oldt: ptr termios = cast[ptr termios](alloc0(sizeof termios)) - newt: ptr termios = cast[ptr termios](alloc0(sizeof termios)) - - # Change terminal settings - tcgetattr(0, oldt) - - newt.c_iflag = oldt.c_iflag - newt.c_oflag = oldt.c_oflag - newt.c_cflag = oldt.c_cflag - newt.c_lflag = oldt.c_lflag - newt.c_line = oldt.c_line - newt.c_cc = oldt.c_cc - - newt.c_lflag = newt.c_lflag and (not ICANON) and (IEXTEN) and (not ISIG) - newt.c_lflag = newt.c_lflag and (if e: ECHO else: not ECHO) - - # Disable buffered IO - tcsetattr(0, TCSANOW, newt) - dealloc(newt) - - # Read next char - result = getchar() - - # Restore terminal settings - tcsetattr(0, TCSANOW, oldt) - dealloc(oldt) - -### End getCh implementation +system.addQuitProc(resetAttributes) proc putchr*(c: cint) = - putchar(c.chr) + stdout.write(c.chr) proc getchr*(): cint = - return getCh().ord + return getch().ord # Types