johnnovak / illwill

A curses inspired simple cross-platform console library for Nim
Do What The F*ck You Want To Public License
398 stars 27 forks source link

Does this work with stdin streams? #17

Closed srcrip closed 2 years ago

srcrip commented 3 years ago

I figured the following should work:

import streams, os
import illwill

proc exitProc() {.noconv.} =
  illwillDeinit()
  showCursor()
  quit(0)

illwillInit(fullscreen=true)
setControlCHook(exitProc)
hideCursor()

var tb = newTerminalBuffer(terminalWidth(), terminalHeight())
tb.setForegroundColor(fgBlack, true)

var stdinStream = newFileStream(stdin)
var line = ""

if not isNil(stdinStream):
  var lineNum = 0
  while stdinStream.readLine(line):
    tb.write(1, lineNum, line)
    tb.display()
    sleep(20)
    lineNum = lineNum + 1
  stdinStream.close()

I'd just like to read from stdin as a stream, but the moment illwillInit gets called it ends up setting something on the terminal thats messing with stdinput I think.

srcrip commented 3 years ago

I believe the reason is the termios stuff (setting non blocking io). I'm not quite sure how you'd work around that though.

johnnovak commented 3 years ago

I believe the reason is the termios stuff (setting non blocking io). I'm not quite sure how you'd work around that though.

That's correct; probably the best is to implement your own readLine() equivalent using getKey().