terminal

Module for supporting cursor and color manipulation on the console.

The main interface for this module is the Terminal struct, which encapsulates the functions of the terminal. Creating an instance of this struct will perform console initialization; when the struct goes out of scope, any changes in console settings will be automatically reverted.

Note:
on Posix, it traps SIGINT and translates it into an input event. You should keep your event loop moving and keep an eye open for this to exit cleanly; simply break your event loop upon receiving a UserInterruptionEvent. (Without the signal handler, ctrl+c can leave your terminal in a bizarre state.)

As a user, if you have to forcibly kill your program and the event doesn't work, there's still ctrl+\

enum Color: ushort;
Defines the list of standard colors understood by Terminal.

black
.

red
.

green
.

yellow
.

blue
.

magenta
.

cyan
.

white
.

enum ConsoleInputFlags: int;
When capturing input, what events are you interested in?

Note:
these flags can be OR'd together to select more than one option at a time.

Ctrl+C and other keyboard input is always captured, though it may be line buffered if you don't use raw.

raw
raw input returns keystrokes immediately, without line buffering

echo
do you want to automatically echo input back to the user?

mouse
capture mouse events

paste
capture paste events (note: without this, paste can come through as keystrokes)

size
window resize events

allInputEvents
subscribe to all input events.

enum ConsoleOutputType: int;
Defines how terminal output should be handled.

linear
do you want output to work one line at a time?

cellular
or do you want access to the terminal screen as a grid of characters?

minimalProcessing
do the least possible work, skips most construction and desturction tasks. Only use if you know what you're doing here

enum ForceOption: int;
Some methods will try not to send unnecessary commands to the screen. You can override their judgement using a ForceOption parameter, if present

automatic
automatically decide what to do (best, unless you know for sure it isn't right)

neverSend
never send the data. This will only update Terminal's internal state. Use with caution because this can

alwaysSend
always send the data, even if it doesn't seem necessary

struct Terminal;
Encapsulates the I/O capabilities of a terminal.

Warning:
do not write out escape sequences to the terminal. This won't work on Windows and will confuse Terminal's internal state on Posix.

this(ConsoleOutputType type, int fdIn = 0, int fdOut = 1, int[] delegate() getSizeOverride = null);
Constructs an instance of Terminal representing the capabilities of the current terminal.

While it is possible to override the stdin+stdout file descriptors, remember that is not portable across platforms and be sure you know what you're doing.

ditto on getSizeOverride. That's there so you can do something instead of ioctl.

void color(int foreground, int background, ForceOption force = ForceOption.automatic, bool reverseVideo = false);
Changes the current color. See enum Color for the values.

void reset();
Returns the terminal to normal output colors

@property int cursorX();
The current x position of the output cursor. 0 == leftmost column

@property int cursorY();
The current y position of the output cursor. 0 == topmost row

void moveTo(int x, int y, ForceOption force = ForceOption.automatic);
Moves the output cursor to the given position. (0, 0) is the upper left corner of the screen. The force parameter can be used to force an update, even if Terminal doesn't think it is necessary

void showCursor();
shows the cursor

void hideCursor();
hides the cursor

void setTitle(string t);
Changes the terminal's title

void flush();
Flushes your updates to the terminal. It is important to call this when you are finished writing for now if you are using the version=with_eventloop

@property int width();
The current width of the terminal (the number of columns)

@property int height();
The current height of the terminal (the number of rows)

void writef(T...)(string f, T t);
void writefln(T...)(string f, T t);
void write(T...)(T t);
void writeln(T...)(T t);
Writes to the terminal at the current cursor position.

deprecated alias writeString = writePrintableString;
use write() or writePrintableString instead

void clear();
Clears the screen.

struct RealTimeConsoleInput;
Encapsulates the stream of input events received from the terminal input.

this(Terminal* terminal, ConsoleInputFlags flags);
To capture input, you need to provide a terminal and some flags.

bool kbhit();
Returns true if there is input available now

bool timedCheckForInput(int milliseconds);
Check for input, waiting no longer than the number of milliseconds

dchar getch();
Get one character from the terminal, discarding other events in the process.

InputEvent nextEvent();
Returns the next event.

Experimental:
It is also possible to integrate this into a generic event loop, currently under -version=with_eventloop and it will require the module arsd.eventloop (Linux only at this point)

struct CharacterEvent;
Input event for characters

enum Type: int;
.

Released
.

Pressed
.

Type eventType;
.

dchar character;
.

uint modifierState;
Don't depend on this to be available for character events

struct PasteEvent;
.

string pastedText;
.

struct MouseEvent;
.

enum Type: int;
.

Moved
.

Pressed
.

Released
.

Clicked
.

Type eventType;
.

enum Button: uint;
.

None
.

Left
.

Middle
.

Right
.

ScrollUp
.

ScrollDown
.

uint buttons;
A mask of Button

int x;
0 == left side

int y;
0 == top

uint modifierState;
shift, ctrl, alt, meta, altgr. Not always available. Always check by using modifierState & ModifierState.something

struct SizeChangedEvent;
.

struct UserInterruptionEvent;
the user hitting ctrl+c will send this

struct InputEvent;
GetNextEvent returns this. Check the type, then use get to get the more detailed input

enum Type: int;
.

CharacterEvent
.

NonCharacterKeyEvent
.

PasteEvent
.

MouseEvent
only sent if you subscribed to mouse events

SizeChangedEvent
only sent if you subscribed to size events

UserInterruptionEvent
the user hit ctrl+c

CustomEvent
.

@property Type type();
.

@property auto get(Type T)();
.


Page generated by Ddoc.