From f3124e6de43fd98800c32cdfc956134ab00cb1a4 Mon Sep 17 00:00:00 2001 From: def Date: Tue, 3 Feb 2015 17:27:59 +0100 Subject: Add terminal.getch to get a single character --- lib/pure/terminal.nim | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/pure/terminal.nim b/lib/pure/terminal.nim index 8607066f3..b0c822c57 100644 --- a/lib/pure/terminal.nim +++ b/lib/pure/terminal.nim @@ -45,6 +45,21 @@ when defined(windows): var oldAttr = getAttributes() + proc winGetch(): cint {.header: "", importc: "_getch".} +else: + import termios, unsigned + + proc setRaw(fd: FileHandle, time: cint = TCSAFLUSH) = + var mode: Termios + discard fd.tcgetattr(addr mode) + mode.iflag = mode.iflag and not Tcflag(BRKINT or ICRNL or INPCK or ISTRIP or IXON) + mode.oflag = mode.oflag and not Tcflag(OPOST) + mode.cflag = (mode.cflag and not Tcflag(CSIZE or PARENB)) or CS8 + mode.lflag = mode.lflag and not Tcflag(ECHO or ICANON or IEXTEN or ISIG) + mode.cc[VMIN] = 1.cuchar + mode.cc[VTIME] = 0.cuchar + discard fd.tcsetattr(time, addr mode) + proc setCursorPos*(x, y: int) = ## sets the terminal's cursor to the (x,y) position. (0,0) is the ## upper left of the screen. @@ -349,6 +364,17 @@ macro styledEcho*(m: varargs[expr]): stmt = result.add(newCall(bindSym"write", bindSym"stdout", newStrLitNode("\n"))) result.add(newCall(bindSym"resetAttributes")) +proc getch*(): char = + when defined(windows): + result = winGetch().char + else: + let fd = getFileHandle(stdin) + var oldMode: Termios + discard fd.tcgetattr(addr oldMode) + fd.setRaw() + result = stdin.readChar() + discard fd.tcsetattr(TCSADRAIN, addr oldMode) + when isMainModule: system.addQuitProc(resetAttributes) write(stdout, "never mind") -- cgit 1.4.1-2-gfad0 From e7eab49e14e4764cecaf6e415d71556238f5642b Mon Sep 17 00:00:00 2001 From: def Date: Tue, 3 Feb 2015 17:29:09 +0100 Subject: Add copyright header --- lib/posix/termios.nim | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/posix/termios.nim b/lib/posix/termios.nim index 492a1456f..88aed73c8 100644 --- a/lib/posix/termios.nim +++ b/lib/posix/termios.nim @@ -1,3 +1,12 @@ +# +# +# Nim's Runtime Library +# (c) Copyright 2015 Andreas Rumpf +# +# See the file "copying.txt", included in this +# distribution, for details about the copyright. +# + {.deadCodeElim: on.} import posix -- cgit 1.4.1-2-gfad0 From 8640efcd4053353fe97da1a1950ad1c370565a4b Mon Sep 17 00:00:00 2001 From: def Date: Wed, 4 Feb 2015 14:10:58 +0100 Subject: Document terminal.getch --- lib/pure/terminal.nim | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/pure/terminal.nim b/lib/pure/terminal.nim index b0c822c57..e0e2aa247 100644 --- a/lib/pure/terminal.nim +++ b/lib/pure/terminal.nim @@ -365,6 +365,8 @@ macro styledEcho*(m: varargs[expr]): stmt = result.add(newCall(bindSym"resetAttributes")) proc getch*(): char = + ## Read a single character from the terminal, blocking until it is entered. + ## The character is not printed to the terminal. when defined(windows): result = winGetch().char else: -- cgit 1.4.1-2-gfad0