text.console- Text terminal control
This module provides a simple interface for character terminal control. Currently we support vt100 compatible terminals and Windows console.
This module doesn’t depend on external library such as
and works with Gauche alone, but
what it can do is limited; for example, you can’t get an event
when shift key alone is pressed. For finer controls, you need
some extension libraries.
For an example of the features in this module, see snake.scm in the examples directory of Gauche source distribution.
Represents a vt100-compatible terminal. An instance of this class can be passed to the “console” argument of the following generic functions.
Input port connected to the terminal. The default value is the standard input port.
Output port connected to the terminal. The default value is the standard output port.
The terminal send back special keys encoded in an input escape sequence.
In order to distinguish such keys from the actual ESC key, we time the
input—if the subsequent input doesn’t come within
microseconds, we interpret the input as individual keystroke, rather
than a part of an escape sequence. The default value is
Represents Windows console. This class is defined on all platforms, but its useful methods are only available on Windows-native runtime.
It doesn’t have public slots.
The application has to check the runtime to see what kind of console is available. A suggested flow is as follows.
has-windows-console?returns true, create
<windows-console>instance. You don’t need
#fon non-Windows platforms.
TERM. If it is set and satisfies
vt100-compatible?, you can create
<vt100>instance. (Note: It is possible that you end up using
<vt100>console on Windows; e.g.
goshrunning on MSYS shell.)
The following procedure packages this flow.
Determines a suitable console class of the running process and returns its instance.
If no suitable console is available, the behavior depends on the
if-not-available keyword argument. If it is
which is default, an error is signalled. If it is
the procedure returns
Given the string value of the environment variable
#t if the terminal can be handled by
Takes over the control of the console, and calls proc with
console as the only argument. The console is set to
the mode, which must be a symbol
cooked. By default
the console is set to
rare mode, which turn off the echoing
and passes most of keystrokes to the program, but it intercepts
terminal controls (like
Ctrl-C for interrupt and
Ctrl-Z for suspend; the actual key depends on terminal
If proc raises an unhandled error, this generic function resets the terminal mode before returning. It does not clear the screen.
Display a character at the current cursor position, and move the current cursor position.
Display a string from the current cursor position, and move the current cursor position.
Ring the beep, or flash the screen (visible bell) if possible.
Fetch a keypress from the console. This blocks until any key is pressed.
The return value may be one of the following values:
A key for the character is pressed. It may be a control code
if the control key is pressed with the key; that is, if the user
#\x01 will be returned.
Indicates a special key; the following keys are supported:
(Note: DELETE key is usually mapped to
#\x7f, but it depends on
ALTand a character.
Indicates the character key is pressed with Alt key. For example,
if the user presses Alt-a,
(ALT #\a) is returned (assuming
CAPSLOCK is off).
Indicates the input is closed somehow.
Modifier keys except
ALT are not treated separately but
included in the returned keycode. Assuming CAPSLOCK is off,
if the user press
the returned value is
a can’t be distinguished from
a will be
(ALT #\A) and
(ALT #\x01), respectively.
Returns true if there’s a key sequence to be read in the console’s input.
Returns two values, the current cursor’s x and y position. The top-left corner is (0,0).
Move cursor to the specified position. The top-left corner is (0,0).
Reset terminal. Usually this sets the character attributes to the default, clears the screen, and moves the cursor to (0, 0).
Clear entire screen.
Clear characters from the current cursor position to the end of the line.
Clear characters from the current cursor position to the end of the screen.
Hide/show the cursor.
If the cursor is at the bottom line of the screen, scroll up the contents and clear the bottom line; the cursor stays the same position. If the cursor is not at the bottom line of the screen, move the cursor down.
If the cursor is at the top line of the screen, scroll down the contents and clear the top line; the cursor stays the same position. If the cursor is not at the top line of the screen, move the cursor up.
Returns two values, the width and height of the screen.
Note: This may affect what’s shown in the console. It is recommended that you only call this before redrawing the entire screen and save the result.
Set the console so that the subsequent characters will be written with attributes specified by spec.
The character attributes spec is a list in the following format:
(<fgcolor> [<bgcolor> . <option> ...])
<fgcolor> : <color> | #f ; #f means default <bgcolor> : <color> | #f <color> : black | red | green | yellow | blue | magenta | cyan | white <option> : bright | reverse | underscore
For example, you can set characters to be written in red with black background and underscore, you can call:
(set-character-attribute con '(red black underscore))
That the options may seem rather limited in the age of full-color bitmap displays. That’s what it used to be, young lads.
Reset character attributes to the default.
Sets the console’s attributes to attrs and calls thunk, then restores the attributes. Even if thunk throws an error, attributes are restored.
Note: You should be able to nest this, but currently nesting isn’t working.