Skip to content

About

Native Delphi VCL terminal emulator component (ConPTY + VT parser + custom rendering)

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

11 Commits

Folders and files

Repository files navigation

Terminal4D

Terminal4D is a terminal emulator for Delphi VCL applications. It runs a console program, such as a shell or a text-mode application, behind the Windows Pseudo Console (ConPTY), parses the VT/ANSI output into a screen buffer and paints that buffer in a VCL control. It is written in Object Pascal and needs no WebView and no external DLL.

Requirements

  • Delphi 13 (Florence). Older versions are not tested.
  • Windows 10 version 1809 (build 17763) or later, where CreatePseudoConsole is available. TTermConPty.IsAvailable returns False on older systems and Start raises ETermConPtyUnavailable.
  • Win32 or Win64, VCL.
  • DUnitX, which ships with Delphi, for the test project.

Installation

Pick one of these:

  • Add the src folder to the search path of your project.
  • Build the runtime package packages\Terminal4D.dproj and add Terminal4D to the runtime packages of your project.
  • Install the package GDK.Terminal4D with DPM, using the spec in GDK.Terminal4D.dspec.

The control is not registered on the component palette. Create it at runtime, as in the example below.

Example

A form with a terminal that fills the client area and runs PowerShell:

uses
  Terminal4D.Vcl.TerminalControl;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  FTerminal := TTerminal4DControl.Create(Self);
  FTerminal.Parent := Self;
  FTerminal.Align := alClient;
  FTerminal.OnTerminated := TerminalTerminated;
end;

procedure TMainForm.FormShow(Sender: TObject);
begin
  FTerminal.SetFocus;
  FTerminal.Start('powershell.exe');
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  FTerminal.Stop;
end;

demo\Terminal4DDemo.dproj is a complete VCL application that does this. Pass a command on its command line to run something else, for example Terminal4DDemo.exe cmd.exe.

Public API

Member Purpose
Start(Command, WorkingDir) Starts Command in a new pseudo console sized to the control
Stop Closes the pseudo console, ends the process if it has not exited after two seconds, and releases all handles and threads
Running True while the child process runs
SendToChild(Text) Sends text to the child as if it were typed, encoded as UTF-8
FontSize Font size in points, limited to 6 to 48
RefreshPalette Repaints after TTermPalette.Scheme changed and reports the new scheme to the child
OnTerminated Raised on the main thread when the child process exits. Not raised after Stop

TTermPalette.Scheme (Terminal4D.Vcl.Palette) selects the dark or the light color table. The core units (Terminal4D.Core.*) have no VCL dependency: feed TTermParser.Process a string and read the cells from TTermEmulator.ActiveBuffer. TTermConPty (Terminal4D.ConPty) hosts a process without the control.

The command line

Start passes Command unchanged to CreateProcess as the command line, with no application name. Windows then takes the first token as the program and looks for it in the folder of your executable, the current folder, the system folders and the PATH. Quote a path that contains spaces, and use a full path when the current folder may hold files you do not trust. Terminal4D does not quote or escape arguments for you.

What is supported

  • Cursor movement, erase, insert and delete of lines and characters, scroll regions, save and restore of the cursor, the alternate screen (modes 47, 1047, 1048, 1049).
  • SGR attributes: bold, faint, italic, underline, inverse, hidden, strike-through, 16 colors, 256 colors and 24-bit color.
  • Wide (East Asian) characters and combining marks.
  • A scrollback of 5000 lines on the primary screen, mouse-wheel scrolling, text selection, copy and paste, bracketed paste (mode 2004).
  • Mouse reporting: clicks (mode 1000), drag (mode 1002; mode 1003 is treated the same) and the SGR encoding (mode 1006).
  • Device status and device attribute reports, OSC 10 and 11 color queries, and color scheme reports (DSR 996, mode 2031).
  • Keyboard input with xterm modifier encoding and Alt as meta. AltGr combinations still type characters.

Limitations

  • OSC sequences other than the color queries are read and ignored: the window title (OSC 0 and 2), hyperlinks (OSC 8) and clipboard access (OSC 52) do nothing.
  • Character set designations (ESC ( 0 and the like) are ignored, so DEC line-drawing characters show as plain ASCII letters. Applications that print Unicode box-drawing characters display correctly.
  • DCS, sixel and other image protocols are not supported.
  • Tab stops are fixed at every eighth column.
  • Lines are not reflowed when the control is resized.
  • The legacy (non-SGR) mouse encoding can report columns and rows up to 223 only.
  • Input is written to the child on the main thread. When the child stops reading its input, a large paste blocks the UI until the child reads again.
  • A child attaches to the console of its parent when the parent has one. Host the control in a GUI application; in a console application the child bypasses the pseudo console.
  • Make the host application per-monitor DPI aware, or Windows scales the window bitmap and the text gets blurry.
  • The default font is Cascadia Mono 12 pt. Cells with glyphs that font lacks are drawn with Segoe UI Symbol.

Output from the child is untrusted

The child process, and anything it prints (a file you type, the output of ssh), controls the byte stream the parser reads. Terminal4D limits the work that stream can cause: CSI sequences longer than 256 characters are dropped, numeric parameters are capped at 65535, scroll counts at the height of the scroll region, OSC strings at 4096 characters, and combining marks at 32 code units per cell. Replies to queries contain only numbers, never text taken from the stream. In bracketed-paste mode the escape character is removed from pasted text, so the clipboard cannot end the paste early. See SECURITY.md to report a problem.

Architecture

src/
  Terminal4D.Core.Defines.pas         Cell, color, style and position types
  Terminal4D.Core.CharWidth.pas       East Asian width and combining marks
  Terminal4D.Core.ScreenBuffer.pas    Cell grid, cursor, scroll regions, scrollback
  Terminal4D.Core.Emulator.pas        Primary and alternate buffer, modes, saved cursor
  Terminal4D.Core.Parser.pas          VT/ANSI escape sequence state machine
  Terminal4D.Core.Utf8Decoder.pas     Incremental UTF-8 decoder for split reads
  Terminal4D.Core.SessionToken.pas    Marks callbacks of an ended session as stale
  Terminal4D.ConPty.pas               Pseudo console host: start, read, write, resize, stop
  Terminal4D.Vcl.Palette.pas          Logical colors to COLORREF, dark and light scheme
  Terminal4D.Vcl.KeyMap.pas           Virtual keys and pasted text to VT input
  Terminal4D.Vcl.TerminalControl.pas  The VCL control
packages/                             Runtime package
demo/                                 VCL demo application
tests/                                DUnitX tests

The parser follows the semantics of libvterm, which is MIT licensed. No code is copied from it or from any other terminal emulator.

Running the tests

From a RAD Studio command prompt in the repository root:

msbuild tests\Terminal4D.Tests.dproj /t:Build /p:Config=Debug /p:Platform=Win32
tests\Win32\Debug\Terminal4D.Tests.exe --exitbehavior:Continue

The tests in the category Integration start real processes through ConPTY. Skip them with --exclude:Integration.

License

MIT. Copyright (c) 2026 GDK Software.

Contributing and security

See CONTRIBUTING.md. Report a vulnerability as described in SECURITY.md, not in a public issue.

About

Native Delphi VCL terminal emulator component (ConPTY + VT parser + custom rendering)

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages