Screen scraping

Saving screen into various formats 

Both TerminalControl and VirtualTerminal objects make it possible to easily save the screen content info an image, plain text or HTML text, either with or without the history buffer.

// save the contents of the terminal screen as a text file
terminal.Save("screen.txt", TerminalCaptureFormat.Text);

// save the contents of the terminal screen as a HTML file and include the history buffer as well
terminal.Save("screen.html", TerminalCaptureFormat.Html, TerminalCaptureOptions.SaveHistory);

// save the contents of the terminal screen as a JPEG image
terminal.Save("screen.jpg", TerminalCaptureFormat.Jpeg);

// save the contents of the terminal screen as a PNG image into a stream
terminal.Save(output, TerminalCaptureFormat.Png);
' save the contents of the terminal screen as a text file
terminal.Save("screen.txt", TerminalCaptureFormat.Text)

' save the contents of the terminal screen as a HTML file and include the history buffer as well
terminal.Save("screen.html", TerminalCaptureFormat.Html, TerminalCaptureOptions.SaveHistory)

' save the contents of the terminal screen as a JPEG image
terminal.Save("screen.jpg", TerminalCaptureFormat.Jpeg)

' save the contents of the terminal screen as a PNG image into a stream
terminal.Save(output, TerminalCaptureFormat.Png)

Tip: It's also easily possible to record a complete terminal session and view it later.

Accessing screen content 

To directly access the actual content of the terminal screen, use TerminalScreen object, available as Screen property in both TerminalControl and VirtualTerminal.

// get number of terminal screen columns (=width)
int width = terminal.Screen.Columns;

// get number of terminal screen rows (=height)
int height = terminal.Screen.Rows;

// get cursor position
int cursorX = terminal.Screen.CursorLeft;
int cursorY = terminal.Screen.CursorTop;
' get number of terminal screen columns (=width)
Dim width As Integer = terminal.Screen.Columns

' get number of terminal screen rows (=height)
Dim height As Integer = terminal.Screen.Rows

' get cursor position
Dim cursorX As Integer = terminal.Screen.CursorLeft
Dim cursorY As Integer = terminal.Screen.CursorTop

Note: TerminalScreen provides additional methods and properties, some of which are described below.

Writing to terminal screen 

To write custom content to TerminalControl's or VirtualTerminal's screen, use Write or WriteLine methods:

// clear screen
terminal.Screen.Clear();

// set cursor position to upper left corner
terminal.Screen.SetCursorPosition(0, 0);

// write to the screen
terminal.Screen.Write("Hello world!");
' clear screen
terminal.Screen.Clear()

' set cursor position to upper left corner
terminal.Screen.SetCursorPosition(0, 0)

' write to the screen
terminal.Screen.Write("Hello world!")

Tip: Write and WriteLine methods accept terminal control sequences and interpret them automatically.

Tip: It's possible to use TerminalControl simply to display custom content without actually being connected to any server.

Reading and writing cells 

To access or modify the content of individual cells, use TerminalScreen's GetCell and SetCell methods. To retrieve subregions of the screen, use GetRegion or GetRegionText methods.

// get the contents of the character cell at position (4, 8)
TerminalCell cell = terminal.Screen.GetCell(4, 8);

// get the cell's character
char c = cell.Character;

// get contents of the rectangle with an upper-left corner of (5, 10)
// and the size of (60, 2)
string[] contents = terminal.Screen.GetRegionText(5, 10, 60, 2);
' get the contents of the character cell at position (4, 8)
Dim cell As TerminalCell = terminal.Screen.GetCell(4, 8)

' get the cell's character
Dim c As Char = cell.Character

' get contents of the rectangle with an upper-left corner of (5, 10)
' and the size of (60, 2)
Dim contents As String() = terminal.Screen.GetRegionText(5, 10, 60, 2)

Reading and writing history 

To access or modify the content of history cells, use TerminalScreen's methods with negative index. So, the most recent history line has index -1, the previous line has index -2, the oldest history line has index -terminal.HistoryLength (terminal is instance of TerminalControl or VirtualTerminal).

// get contents of the first character cell on the most recent history line
TerminalCell cell = terminal.Screen.GetCell(0, -1);

// get contents of the first 4 lines of history (the oldest history lines)
string[] contents = terminal.Screen.GetRegionText(
    0,                       // first column
    -terminal.HistoryLength, // the oldest history line
    terminal.Screen.Columns, // whole width of screen
    4                        // 4 lines
);
' get contents of the first character cell on the most recent history line
Dim cell As TerminalCell = terminal.Screen.GetCell(0, -1)

' get contents of the first 4 lines of history (the oldest history lines)
Dim contents As String() = terminal.Screen.GetRegionText(
    0,                       ' first column
    -terminal.HistoryLength, ' the oldest history line
    terminal.Screen.Columns, ' whole width of screen
    4                        ' 4 lines
 )