Class TConsoleReaderThread

Unit

Declaration

type TConsoleReaderThread = class(TThread)

Description

A thread class dedicated to creating and managing a separate Windows console for input/output. The thread continuously monitors the console's standard input (stdin) and delivers received lines to the Owner using the IConsoleInputHandler interface.

Hierarchy

Overview

Fields

Protected StdInHandle: THandle;

Methods

Protected procedure Execute; override;
Public procedure CSInit;
Public procedure CSBlockWrite(const ABlock: Boolean);
Public function CSIsWriteBlocked: Boolean;
Public procedure CSWrite(const S: string);
Public procedure CSWriteLn(const S: string);
Public procedure CSInput(const S: String; const pre: Boolean; const post: Boolean); overload;
Public procedure CSInput(const S: String); overload;
Public procedure CSWriteLogEntryUTC(level: TLevelLog; const MessageString: String);
Public procedure CSShow; overload;
Public procedure CSShow(MainForm: TForm); overload;
Public procedure CSShow(Mode: TConsolePosMode; MainForm: TForm; MonitorIndex: Integer = 0); overload;
Public procedure CSHide;
Public procedure CSDisableCloseButton;
Public procedure CSEnableCloseButton;
Public procedure CSSetForegroundWindow;
Public procedure MessageInfoLog(level: TLevelLog; Message: String); overload;
Public procedure MessageInfoLog(level: TLevelLog; Message: String; Info: String); overload;
Public procedure NoticeLog(Message: String); overload;
Public procedure NoticeLog(Message: String; Info: String); overload;
Public procedure WarningLog(Message: String); overload;
Public procedure WarningLog(Message: String; Info: String); overload;
Public procedure ErrorLog(Message: String); overload;
Public procedure ErrorLog(Message: String; Info: String); overload;
Public procedure InfoLog(Message: String); overload;
Public procedure InfoLog(Message: String; Info: String); overload;
Public procedure DebugLog(Message: String); overload;
Public procedure DebugLog(Message: String; Info: String); overload;
Public procedure Log(Message: String); overload;
Public procedure Log(Message: String; Info: String); overload;
Public constructor Create(AOwner: IConsoleInputHandler); overload;
Public constructor Create(AOwner: IConsoleInputHandler; Preface: String); overload;
Public constructor Create(AOwner: IConsoleInputHandler; Preface: String; AStoreText: Boolean); overload;
Public destructor Destroy; override;

Properties

Public property Owner: IConsoleInputHandler read FOwner;

Description

Fields

Protected StdInHandle: THandle;

The handle to the console's standard input. Used internally during initialization.

Methods

Protected procedure Execute; override;

The main execution loop of the thread. Allocates the console, sets up standard I/O handles, and continuously calls ReadConsole() to capture user input.

Public procedure CSInit;

Initializes the console environment. Calls AllocConsole and sets up the standard input, output, and error handles. Platform: Windows

Public procedure CSBlockWrite(const ABlock: Boolean);

Sets whether console output is blocked.

Parameters
ABlock
If True, subsequent console write operations are ignored.
Public function CSIsWriteBlocked: Boolean;

Checks if console output is currently blocked.

Returns

True if console writing is blocked; otherwise, False.

Public procedure CSWrite(const S: string);

Writes the specified string to the console's standard output without a line break.

Parameters
S
The string to write.
Public procedure CSWriteLn(const S: string);

Writes the specified string to the console's standard output followed by a line break.

Parameters
S
The string to write.
Public procedure CSInput(const S: String; const pre: Boolean; const post: Boolean); overload;

Writes a string to the console and optionally displays an input prompt. This is the detailed overload, allowing control over prefix and prompt display.

Parameters
S
The message string to display before the input prompt.
pre
If True, prefixes the message with ': '.
post
If True, displays the input prompt '> ' after the message.
Public procedure CSInput(const S: String); overload;

Writes a string to the console, prefixes it with ': ', and displays the input prompt '> '. Default overload for standard console interaction.

Parameters
S
The message string to display before the input prompt.
Public procedure CSWriteLogEntryUTC(level: TLevelLog; const MessageString: String);

Writes a timestamped log entry to the console using UTC time.

Parameters
level
The log level to use TLevelLog.
MessageString
The log message content.
Public procedure CSShow; overload;

Shows the console window. Platform: Windows

See also
CSHide
Hides the console window.
Public procedure CSShow(MainForm: TForm); overload;

Shows the console window. Platform: Windows

Parameters
MainForm
The main form to associate with the console window.
See also
CSHide
Hides the console window.
Public procedure CSShow(Mode: TConsolePosMode; MainForm: TForm; MonitorIndex: Integer = 0); overload;

Shows the console window. Platform: Windows

Parameters
Mode
The position mode for the console window.
MainForm
The main form to associate with the console window.
MonitorIndex
The index of the monitor to use for positioning the console window.
See also
CSHide
Hides the console window.
Public procedure CSHide;

Hides the console window. Platform: Windows

See also
CSShow
Shows the console window.
Public procedure CSDisableCloseButton;

Disables the close button (X) on the console window's title bar. This prevents the user from closing the console using the standard window controls, allowing the managing application to control the console's lifecycle. Platform: Windows

Note
TODO: Keyboard shortcuts for closing the console window are not yet implemented.

See also
CSEnableCloseButton
Enables the close button (X) on the console window's title bar.
Public procedure CSEnableCloseButton;

Enables the close button (X) on the console window's title bar. Platform: Windows

See also
CSDisableCloseButton
Disables the close button (X) on the console window's title bar.
Public procedure CSSetForegroundWindow;

Brings the console window to the foreground. Platform: Windows

Public procedure MessageInfoLog(level: TLevelLog; Message: String); overload;

Logs a message with the specified log level.

Parameters
level
The log level to use TLevelLog.
Message
The main log message.
Public procedure MessageInfoLog(level: TLevelLog; Message: String; Info: String); overload;

Logs a message with the specified log level and additional info.

Parameters
level
The log level to use TLevelLog.
Message
The main log message.
Info
Additional information to include in the log entry.
Public procedure NoticeLog(Message: String); overload;

Logs a notice-level message.

Parameters
Message
The log message.
Public procedure NoticeLog(Message: String; Info: String); overload;

Logs a notice-level message with additional info.

Parameters
Message
The main log message.
Info
Additional information to include in the log entry.
Public procedure WarningLog(Message: String); overload;

Logs a warning-level message.

Parameters
Message
The log message.
Public procedure WarningLog(Message: String; Info: String); overload;

Logs a warning-level message with additional info.

Parameters
Message
The main log message.
Info
Additional information to include in the log entry.
Public procedure ErrorLog(Message: String); overload;

Logs an error-level message.

Parameters
Message
The log message.
Public procedure ErrorLog(Message: String; Info: String); overload;

Logs an error-level message with additional info.

Parameters
Message
The main log message.
Info
Additional information to include in the log entry.
Public procedure InfoLog(Message: String); overload;

Logs an info-level message.

Parameters
Message
The log message.
Public procedure InfoLog(Message: String; Info: String); overload;

Logs an info-level message with additional info.

Parameters
Message
The main log message.
Info
Additional information to include in the log entry.
Public procedure DebugLog(Message: String); overload;

Logs a debug-level message.

Parameters
Message
The log message.
Public procedure DebugLog(Message: String; Info: String); overload;

Logs a debug-level message with additional info.

Parameters
Message
The main log message.
Info
Additional information to include in the log entry.
Public procedure Log(Message: String); overload;

Logs a general message.

Parameters
Message
The log message.
Public procedure Log(Message: String; Info: String); overload;

Logs a general message with additional info.

Parameters
Message
The main log message.
Info
Additional information to include in the log entry.
Public constructor Create(AOwner: IConsoleInputHandler); overload;

Creates and initializes the console reader thread without a preface message.

Parameters
AOwner
The object that implements IConsoleInputHandler to receive input.
Public constructor Create(AOwner: IConsoleInputHandler; Preface: String); overload;

Creates and initializes the console reader thread with an initial preface message.

Parameters
AOwner
The object that implements IConsoleInputHandler to receive input.
Preface
A string displayed at console startup.
Public constructor Create(AOwner: IConsoleInputHandler; Preface: String; AStoreText: Boolean); overload;

Creates and initializes the console reader thread with a preface and option to store output.

Parameters
AOwner
The object that implements IConsoleInputHandler to receive input.
Preface
A string displayed at console startup.
AStoreText
If True, all console output is stored internally in FStoredText.
Public destructor Destroy; override;

Destroys the thread and frees the console resource. Calls FreeConsole before inherited destruction.

Properties

Public property Owner: IConsoleInputHandler read FOwner;

Reference to the component implementing IConsoleInputHandler.