# The MIT License (MIT)
#
# Copyright (C) 2026 Scott Duensing
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

.section Widgets
.topic widget.ansiterm
.title AnsiTerm
.toc 0 AnsiTerm
.index AnsiTerm
.index wgtAnsiTerm
.index Terminal
.index VT100

.h2 AnsiTerm

A VT100/ANSI-compatible terminal emulator widget designed for connecting to BBS systems over the serial link. Uses a traditional text-mode cell buffer (character + attribute byte pairs) with the CP437 character set and 16-color CGA palette. Supports cursor movement, screen/line erase, scrolling regions, SGR colors, DEC private modes, and a configurable scrollback buffer. Communication is abstracted through read/write function pointers, allowing the terminal to work with raw serial ports, the secLink encrypted channel, or any other byte-oriented transport.

The widget renders through two paint paths: a full paint used by the normal widget pipeline, and a fast incremental repaint (wgtAnsiTermRepaint) that pushes dirty rows directly to the window content buffer for low-latency serial echo.

Header: widgets/ansiTerm.h

.h3 Creation

.code
WidgetT *term = wgtAnsiTerm(parent, 80, 25);
.endcode

.h3 API Functions

.index wgtAnsiTermWrite
.index wgtAnsiTermClear
.index wgtAnsiTermSetComm
.index wgtAnsiTermSetScrollback
.index wgtAnsiTermPoll
.index wgtAnsiTermRepaint

.table
  Function                                                      Description
  --------                                                      -----------
  WidgetT *wgtAnsiTerm(parent, cols, rows)                      Create an ANSI terminal widget with the given column/row dimensions.
  void wgtAnsiTermWrite(w, data, len)                           Write raw bytes into the terminal's ANSI parser. data is a const uint8_t * buffer.
  void wgtAnsiTermClear(w)                                      Clear the terminal screen and reset the cursor to the home position.
  void wgtAnsiTermSetComm(w, ctx, readFn, writeFn)              Attach a communication channel. readFn and writeFn are I/O callbacks; ctx is passed as their first argument.
  void wgtAnsiTermSetScrollback(w, maxLines)                    Set the maximum number of scrollback lines.
  int32_t wgtAnsiTermPoll(w)                                    Poll the communication channel for incoming data and feed it into the parser. Returns number of bytes consumed.
  int32_t wgtAnsiTermRepaint(w, outY, outH)                     Fast repaint path that renders dirty rows directly into the window's content buffer, bypassing the widget pipeline. Returns the number of rows repainted and the dirty region via outY/outH.
.endtable

.h3 API Struct (wgtRegisterApi "ansiterm")

The sApi struct exposes these function pointers:

.table
  Slot             Function
  ----             --------
  create           wgtAnsiTerm
  write            wgtAnsiTermWrite
  clear            wgtAnsiTermClear
  setComm          wgtAnsiTermSetComm
  setScrollback    wgtAnsiTermSetScrollback
  poll             wgtAnsiTermPoll
  repaint          wgtAnsiTermRepaint
.endtable

.h3 Properties (BASIC Interface)

.table
  Property     Type       Access       Description
  --------     ----       ------       -----------
  Cols         Integer    Read-only    Number of columns.
  Rows         Integer    Read-only    Number of rows.
  Scrollback   Integer    Write-only   Maximum number of scrollback lines.
.endtable

.h3 Methods (BASIC Interface)

.table
  Method       Description
  ------       -----------
  Clear        Clear the terminal screen.
  Poll         Process any pending bytes on the attached communication channel.
  Write        Write a string into the terminal (with ANSI escape processing).
.endtable

.h3 Events

AnsiTerm uses the common events only. No widget-specific events are defined.

.h3 Keyboard Shortcuts

Within the terminal widget:

.table
  Key              Action
  ---              ------
  Ctrl+C           Copy selection to the clipboard (when a selection exists).
  Ctrl+V           Send the clipboard contents to the attached write function.
  Arrows           Send VT100 cursor escape sequences.
  Home/End         Send VT100 Home/End sequences.
  PgUp/PgDn/Del    Send the corresponding VT100 escape sequences.
.endtable
