I'm trying to implement this behavior:

When user types Ctrl-C once, clear input. If the user types Ctrl-C twice then 
exit from the program. What I noticed is my Ctrl-C hook is invoked only the 
first time the user hits Ctrl-C. Am I missing something? Any comments or 
suggestions are welcome!
    
    
    # Based on code from https://rosettacode.org/wiki/Handle_a_signal#Nim
    
    import times, os, strutils
    
    type EKeyboardInterrupt = object of CatchableError
    
    proc handler() {.noconv.} =
      raise newException(EKeyboardInterrupt, "Keyboard Interrupt")
    
    setControlCHook(handler)
    
    let t = epochTime()
    
    var ctrl_c_caught = false
    for n in 1..<int64.high:
      try:
        sleep 500
        echo n
      except EKeyboardInterrupt:
        if ctrl_c_caught:
          break
        else:
          ctrl_c_caught = true
          echo "Program has run for ", formatFloat(epochTime() - t, precision = 
0), " seconds."
    
    
    Run

Reply via email to