SvelteKit
GitHub

Console - ctrl-c

Project: SmallForth

I would like the console to be able to process ctrl-c like this: If the current line is not empty, it abandons the input. If the line is empty, it exits the program.

Currently, ctrl-c is disabled as a signal with the call to SetConsoleCtrlHandler in inputprocessor.cpp. However, the ctrl-c is not interpreted until getline() received a carriage-return.

The handler routine HandlerRouting in inputprocessor.cpp can set a volatile flag all it wants, the routine that is reading a character, a word, or a line from stdin will not return until the carriage-return is received.

All that could really be done is to have the handler routine exit the program, but, this is not what we want.

After trying many different ways, the conclusion is that I have to re-implement the console input functionality using getchar(). This means:

  • Character input - outputing to console and altering the line either by appending or inserting
  • Forward/backward arrows - moving to previous or next line when necessary
  • Home/end
  • Delete/backspace
  • Enter
  • Up/down arrows - console history

All this can be done via conio.h, however, I suspect it makes my current implementation extremely windows-dependent.

This all needs doing whilst ensuring resizing and scrolling of the console window does not affect any of it. The issue with that is there are no scroll/resize events issued via conio.h.

The code is in inputprocessor.cpp. Note, that the standard windows console behaves weirdly at the end of a line, it doesn't move to the next line when it should, instead it places the cursor before the last character of the line. The code here countermands that.

The scrolling and resizing issue meant that we wouldn't necessarily know what console line the input line started on (it may be a lot longer than a single line) after a resize event. It deals with this by asking the console where the cursor is (or has been moved) and it already knows what position in the input-line the cursor is supposed to be on. The call to CalculateLineStartPosition is essential to all the processing, which works out where the current line now starts in x-y terms.

There may have been a better way of achieving this. Rewriting the console input did seem overkill, I may as well have just coded an entire console. Googling/ecosia-ing for an answer did not return anything that could help

Bugs

The history does not seem to work properly, for repeating the same line over and over.

© P Bentley 2023-2025. Created with svelte