To interact with the terminal, you need to read from `/dev/tty` on Unix 
systems, as stdin may be redirected to a non-tty input source. Here's a quick 
and dirty adaptation of the `readPasswordFromStdin` code for `/dev/tty` 
(warning, I did only minimal testing):
    
    
    import termios
    
    proc readPasswordfromTty*(prompt: string): string =
      var tty = open("/dev/tty", fmReadWrite)
      let fd = tty.getFileHandle()
      var cur, old: Termios
      discard fd.tcgetattr(addr cur)
      old = cur
      cur.c_lflag = cur.c_lflag and not Cflag(ECHO)
      discard fd.tcsetattr(TCSADRAIN, addr cur)
      tty.write prompt
      tty.flushFile
      result = tty.readLine
      tty.close
      discard fd.tcsetattr(TCSADRAIN, addr old)
    
    import strutils
    
    proc main =
      let s = readPasswordfromTty("> ")
      echo repeatChar(len(s), '*')
      echo s
    
    main()
    

Reply via email to