Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

Programming 1 (BI)
  ▸ exercises
  ▸ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C#
* D lang.
* Java
* Nim
* Nim2
  + exercises
* XC=BASIC
* old
  ✦C++, ✦Clojure, ✦Scala


[ edit | logout ]
[ sandbox | passwd ]

Read from stdin -- advanced solution

When I wanted to use a colored prompt and/or when I wanted to type in some Unicode text, then the previous (intermediate) solution was not perfect.

The solution that I show here worked for me under Linux. We'll need the 3rd-party library noise:

$ nimble install noise
import pkg/noise
import std/random

proc input(noise: var Noise): string {.raises: [EOFError, IOError, Exception].} =
  let ok = noise.readLine()
  if not ok:
    raise newException(EOFError, "abort")
  noise.getLine()

proc input(noise: var Noise, prompt: Styler): string {.raises: [EOFError, IOError, Exception].} =
  noise.setPrompt(prompt)
  noise.input()

proc input(noise: var Noise, prompt: string): string {.raises: [EOFError, IOError, Exception].} =
  noise.setPrompt(Styler.init(prompt))
  noise.input()

proc main() =
  randomize()
  #
  var noise = Noise.init()
  let prompt = Styler.init(bgCyan, "JKB", resetStyle, "> ")
  noise.setPrompt(prompt)

  echo "'", noise.input(), "'"      # v1
  echo "'", noise.input("> "), "'"  # v2
  #
  while true:
    try:
      let s = block:
        let which = rand(1..2)
        if which == 1:
          noise.input(Styler.init(bgRed, fgWhite, styleBright, "Error", resetStyle, "> "))  # v3
        else:
          noise.input(Styler.init(bgGreen, fgWhite, styleBright, "OK", resetStyle, "> "))   # v4
      echo "'" & s & "'"
    except EOFError:
      break

main()

The library implicitly imports the std/terminal module. The styles and colors (bgRed, fgWhite, styleBright, etc.) come from this module.

What do we get?

  • Bash-like keybindings: left arrow, right arrow, DEL, etc. The current line can be freely edited.
  • The prompt can be colored and the input can be a Unicode text. When you edit the text, no glitches.
  • I added some conveniance input() functions.
  • v1: you set the prompt beforehand and then you call noise.input()
  • v2: for noise.input() you can specify the prompt as a string. No style is applied.
  • v3 and v4: or, you can set a styled (colored) prompt
  • Ctrl+C and Ctrl+D raise an exception (EOFError in both cases) that you can catch

Note:

  • If you want a colored prompt (like above), then use the colors and styles from std/terminal. When I tried to use my own escape sequences, it resulted in some glitches. At least that was my experience.

History support

noise supports history too (browse it with the up and down arrows):

proc main() =
  var noise = Noise.init()
  #
  while true:
    try:
      let s = noise.input("> ")
      echo "'", s, "'"
      if s.len > 0:            # this
        noise.historyAdd(s)    # and this
    except EOFError:
      break
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 June 01, 22:30