Skip to content
ConsoleMonster
Class

Terminal

ConsoleMonster

Namespace
ConsoleMonster
Assembly
ConsoleMonster
Modifiers
static

The print-once entry point: Terminal.Write(table). Detects the terminal once (overridable through Terminal.Profile or the CONSOLEMONSTER_* environment variables) and writes widgets in the right mode. Calls are serialized, so concurrent writers never interleave inside a block.

Inheritance

  • object
  • Terminal

Methods

public static void Clear() #

Clears the screen and its scrollback and puts the cursor at the top left, as Console.Clear and Spectre's AnsiConsole.Clear do. A no-op where clearing makes no sense: redirected output, CI logs, plain or stream mode, and while a live session owns the console (its area would be lost).

public static RenderContext CreateContext(TerminalProfile? profile = null, ITheme? theme = null) #

Creates a render context for a profile and theme.

Parameters

profile ConsoleMonster.Capabilities.TerminalProfile? = null optional
The profile, or null for the detected one.
theme ConsoleMonster.Styling.ITheme? = null optional
The theme, or null for Terminal.Theme.

Returns

RenderContext The context.
public static ITerminalOutput CreateInlineOutput(TerminalProfile? profile = null, ITerminalWriter? writer = null) #

Creates the output for an inline live area (InlineOutput) on a VT console, or stream lines where in-place drawing is not possible (CI, redirected output, no VT).

Parameters

profile ConsoleMonster.Capabilities.TerminalProfile? = null optional
The profile, or null for Terminal.Profile.
writer ConsoleMonster.Output.ITerminalWriter? = null optional
The sink, or null for the console (then owned by the output).

Returns

ITerminalOutput The output.
public static ITerminalOutput CreateOutput(TerminalProfile? profile = null, ITerminalWriter? writer = null) #

Creates the output for a profile: VT when live rendering is possible, line-based otherwise.

Parameters

profile ConsoleMonster.Capabilities.TerminalProfile? = null optional
The profile, or null for the detected one.
writer ConsoleMonster.Output.ITerminalWriter? = null optional
The sink, or null for the console (the output then owns it).

Returns

ITerminalOutput The output; dispose it when done.
public static TerminalProfile GetCurrentProfile() #

The cached Terminal.Profile with the current window size: after the user resizes the console, print-once output lays out for the new width while every other detected capability stays cached. An assigned profile, redirected output and non-interactive modes keep their size.

Returns

TerminalProfile The profile for a write happening now.
public static IDisposable HideCursor() #

Hides the cursor until the returned handle is disposed or Terminal.ShowCursor is called. The cursor is also shown again when the process exits or Ctrl+C ends it. A no-op in redirected output, CI logs and plain mode.

Returns

IDisposable A handle that shows the cursor again when disposed.
public static bool IsConsole(ITerminalTarget target) #

Whether a target is the process console (live redraw, keyboard input, console ownership).

Parameters

target ConsoleMonster.Output.ITerminalTarget required
The target.

Returns

bool True for ConsoleTarget.
public static void Refresh() #

Discards the cached profile so the next use detects the terminal again (after a resize or font change).

public static CellBuffer Render(IWidget widget, TerminalProfile profile, ITheme? theme = null) #

Renders a widget to a cell buffer (for tests, snapshots and custom outputs).

Parameters

widget ConsoleMonster.Layout.IWidget required
The widget.
profile ConsoleMonster.Capabilities.TerminalProfile required
The profile.
theme ConsoleMonster.Styling.ITheme? = null optional
The theme, or null for Terminal.Theme.

Returns

CellBuffer The rendered block.
public static IEnumerable<CellBuffer> RenderChunks(IWidget widget, int width, RenderContext context, int chunkLines = 500) #

Renders a widget for print-once output as one or more blocks written one after another: a long table comes as a sequence of blocks with stable column widths (see Terminal.StreamingRowThreshold), anything else as one block.

Parameters

widget ConsoleMonster.Layout.IWidget required
The widget.
width int required
The width in cells.
context ConsoleMonster.Canvas.RenderContext required
The render context.
chunkLines int = 500 optional
Lines per block for long tables (default 500).

Returns

IEnumerable<CellBuffer> The blocks, top to bottom.
public static string RenderEncoded(IWidget widget, TerminalProfile profile, ITheme? theme = null) #

Renders a widget to the exact text the output for profile would write (including VT sequences).

Parameters

widget ConsoleMonster.Layout.IWidget required
The widget.
profile ConsoleMonster.Capabilities.TerminalProfile required
The profile.
theme ConsoleMonster.Styling.ITheme? = null optional
The theme, or null.

Returns

string The encoded text.
public static string RenderText(IWidget widget, TerminalProfile profile, ITheme? theme = null) #

Renders a widget to plain text lines (trailing spaces trimmed).

Parameters

widget ConsoleMonster.Layout.IWidget required
The widget.
profile ConsoleMonster.Capabilities.TerminalProfile required
The profile.
theme ConsoleMonster.Styling.ITheme? = null optional
The theme, or null.

Returns

string The text.
public static ITheme ResolveTheme(TerminalProfile profile) #

Chooses the named light, dark, high contrast or colour-blind-safe palette. Automatic profiles first honor Windows High Contrast, then the detected terminal background; other output uses the dark palette. The colour-blind-safe preference picks its light or dark variant from the detected background in the same way.

Parameters

profile ConsoleMonster.Capabilities.TerminalProfile required
The terminal profile.

Returns

ITheme The theme.
public static int ResolveWidth(int width = 0) #

Resolves an automatic width from the current output target; unknown widths use 100 cells.

Parameters

width int = 0 optional
An explicit positive width, or zero for the current target.

Returns

int A positive width in terminal cells.
public static void ShowCursor() #

Shows the cursor (after Terminal.HideCursor). A no-op in redirected output, CI logs and plain mode.

public static void Status(string markup, Action<StatusContext> action) #

Runs work under a one-line status with a spinner (Spectre's AnsiConsole.Status().Start): Terminal.Status("Resolvingโ€ฆ", ctx => { โ€ฆ ctx.Update("Querying [accent]dc01[/]"); }). The line is a transient inline live area: output written meanwhile scrolls above it, and it is removed when the work ends, even when it throws. Redirected output, CI logs and plain mode get each status text once as a plain line instead (see StatusContext).

Parameters

markup string required
The first status text, as markup.
action System.Action<ConsoleMonster.Output.StatusContext> required
The work.
public static async Task StatusAsync(string markup, Func<StatusContext, Task> action) #

Runs asynchronous work under a status (see Terminal.Status).

Parameters

markup string required
The first status text, as markup.
action System.Func<ConsoleMonster.Output.StatusContext, System.Threading.Tasks.Task> required
The work.

Returns

Task A task that completes when the work has finished and the status is removed.
public static async Task<T?> StatusAsync<T>(string markup, Func<StatusContext, Task<T?>> func) #

Runs asynchronous work that returns a value under a status.

Type Parameters

T
The result type.

Parameters

markup string required
The first status text, as markup.
func System.Func<ConsoleMonster.Output.StatusContext, System.Threading.Tasks.Task<T?>> required
The work.

Returns

Task<T?> The work's result.
public static T? Status<T>(string markup, Func<StatusContext, T?> func) #

Runs work that returns a value under a status (see Terminal.Status).

Type Parameters

T
The result type.

Parameters

markup string required
The first status text, as markup.
func System.Func<ConsoleMonster.Output.StatusContext, T?> required
The work.

Returns

T? The work's result.
public static IDisposable Use(ITerminalTarget target) #

Sends the static methods' output (and prompts, live sessions and status helpers without an explicit target) to target until the returned scope is disposed. The scope flows with async calls and is local to them, so parallel tests and requests do not see each other's target.

Parameters

target ConsoleMonster.Output.ITerminalTarget required
The target.

Returns

IDisposable The scope; dispose it to restore the previous target.
public static IDisposable UseProfilePolicy(Func<TerminalProfile, TerminalProfile> policy) #

Applies a host policy to profiles in the current asynchronous flow without replacing the target or pinning its dimensions. Prompts keep their keyboard and recording surface; console and live output keep ownership.

Parameters

policy System.Func<ConsoleMonster.Capabilities.TerminalProfile, ConsoleMonster.Capabilities.TerminalProfile> required
The profile transformation, applied after an enclosing policy when nested.

Returns

IDisposable A scope that restores the previous policy.
public static IDisposable UseScreenOwner(string owner) #

Declares that another component draws the console screen (a full-screen dashboard, a host's own progress display) until the scope is disposed. Meanwhile console output from every thread uses OutputMode.Stream instead of OutputMode.Interactive: live views, progress and dashboards write append-only lines and prompts that need to redraw do not start, so nothing draws over the owner's frames. A live session already drawing on the console stops drawing: its inline area is cleared (a full-screen session leaves the alternate screen), updates keep being applied, and it draws again, with the current state, when the last owner releases the screen. Plain output stays plain; output sent to another target (Terminal.Use, a recording, a writer) and sessions given their own profile or output are not affected. For a child process, CONSOLEMONSTER_MODE=stream does the same.

Parameters

owner string required
Who owns the screen, for diagnostics (for example Maester dashboard).

Returns

IDisposable A scope that releases the screen; disposing it twice is harmless.

Examples

using (Terminal.UseScreenOwner("Maester dashboard")) {
    RunChecks(); // ConsoleMonster progress inside writes stream lines
}
Write 3 overloads
public static void Write(IWidget widget) #

Writes a widget once, as wide as the terminal.

Parameters

widget ConsoleMonster.Layout.IWidget required
The widget.
public static void Write(IWidget widget, TerminalProfile? profile) #

Writes a widget once with an explicit profile.

Parameters

widget ConsoleMonster.Layout.IWidget required
The widget.
profile ConsoleMonster.Capabilities.TerminalProfile? required
The profile, or null for the detected one.
public static void Write(IWidget widget, TerminalProfile? profile, ITerminalWriter? writer) #

Writes a widget once to an explicit sink: a PowerShell host, a log, a string. With a writer, the output is laid out for the profile but goes to the writer instead of the console.

Parameters

widget ConsoleMonster.Layout.IWidget required
The widget.
profile ConsoleMonster.Capabilities.TerminalProfile? required
The profile, or null for Terminal.GetCurrentProfile.
writer ConsoleMonster.Output.ITerminalWriter? required
The sink, or null for the console. The caller keeps ownership of a given writer.
public static void WriteError(IWidget widget) #

Writes a widget once to standard error (see Terminal.ErrorTarget), for warnings and errors that must not mix with data on standard output.

Parameters

widget ConsoleMonster.Layout.IWidget required
The widget, such as a status list or an exception view.
public static void WriteErrorMarkupLine(string markup) #

Writes one line of markup to standard error (see Terminal.ErrorTarget).

Parameters

markup string required
The markup, for example [warn]Development build[/].
public static void WriteLines(IReadOnlyList<StyledLine> lines, bool newLine = true, TerminalProfile? profile = null, ITerminalWriter? writer = null) #

Writes styled lines; with newLine false the last line is left open.

Parameters

lines System.Collections.Generic.IReadOnlyList<ConsoleMonster.Text.StyledLine> required
The lines.
newLine bool = true optional
Whether to end the last line.
profile ConsoleMonster.Capabilities.TerminalProfile? = null optional
The profile, or null for Terminal.GetCurrentProfile.
writer ConsoleMonster.Output.ITerminalWriter? = null optional
The sink, or null for the console. The caller keeps ownership of a given writer.
WriteMarkup 2 overloads
public static void WriteMarkup(string markup) #

Writes Spectre-compatible markup without ending the line (Spectre's Markup), for example a prompt prefix; line breaks inside the markup still start new lines.

Parameters

markup string required
The markup.
public static void WriteMarkup(string markup, bool newLine, TerminalProfile? profile = null, ITerminalWriter? writer = null) #

Writes markup to an explicit sink; with newLine false the last line is left open. Redirected and CI output gets plain text either way.

Parameters

markup string required
The markup.
newLine bool required
Whether to end the last line.
profile ConsoleMonster.Capabilities.TerminalProfile? = null optional
The profile, or null for Terminal.GetCurrentProfile.
writer ConsoleMonster.Output.ITerminalWriter? = null optional
The sink, or null for the console. The caller keeps ownership of a given writer.
WriteMarkupLine 2 overloads
public static void WriteMarkupLine(string markup) #

Writes one line of Spectre-compatible markup (Spectre's MarkupLine), for example [pass]Done[/] in [muted]3s[/].

Parameters

markup string required
The markup.
public static void WriteMarkupLine(string markup, TerminalProfile? profile, ITerminalWriter? writer) #

Writes one line of markup to an explicit sink.

Parameters

markup string required
The markup.
profile ConsoleMonster.Capabilities.TerminalProfile? required
The profile, or null for Terminal.GetCurrentProfile.
writer ConsoleMonster.Output.ITerminalWriter? required
The sink, or null for the console. The caller keeps ownership of a given writer.
public static void WriteRich(RichText text, bool newLine = true, TerminalProfile? profile = null, ITerminalWriter? writer = null) #

Writes rich text as lines (not as a laid-out block): line breaks inside it start new lines, nothing is wrapped or padded, and with newLine false the last line is left open, as Write-Host -NoNewline does.

Parameters

text ConsoleMonster.Text.RichText required
The rich text.
newLine bool = true optional
Whether to end the last line.
profile ConsoleMonster.Capabilities.TerminalProfile? = null optional
The profile, or null for Terminal.GetCurrentProfile.
writer ConsoleMonster.Output.ITerminalWriter? = null optional
The sink, or null for the console. The caller keeps ownership of a given writer.

Properties

public static ICapabilityProvider CapabilityProvider { get; set; } #

The capability provider used for detection.

public static TerminalProfile Profile { get; set; } #

The active profile; detected on first use and cached (including the size at detection time). Assign to override detection for the process. Print-once writes use Terminal.GetCurrentProfile, which refreshes the size.

public static ITheme Theme { get; set; } #

The active theme: the target's (see Terminal.Target), or the console's.

public static int StreamingRowThreshold { get; set; } #

The rows above which a table is printed in blocks (default 2000): column widths are still computed from every row, but cells are rendered and written a block at a time, so memory stays flat for very long tables.

public static ITerminalTarget ErrorTarget { get; } #

A target for standard error with the same capability decisions as standard output: when standard error is redirected it gets the redirected profile (TerminalProfile.WithRedirectedOutput: no colour, word status markers, whole lines); on a terminal it keeps the console's colours and glyphs and writes append-only lines. Use it with Terminal.Use for a block of output, or call Terminal.WriteError.

public static TerminalScheme? Palette { get; } #

The terminal's RGB palette and defaults, queried once before input starts; null if redirected, unsupported or timed out.

public static bool IsCursorHidden { get; } #

Whether Terminal.HideCursor has hidden the cursor and it has not been shown again.

public static string? ScreenOwner { get; } #

The component that declared it owns the console screen (Terminal.UseScreenOwner), or null when the screen is free. The most recent declaration wins while several are open.

public static ITerminalTarget Target { get; } #

The target the static methods write to: the one set by Terminal.Use for the current asynchronous flow, or Terminal.DefaultTarget. Writes with an explicit writer, live sessions and prompts with an explicit target keep theirs.

public static ITerminalTarget DefaultTarget { get; set; } #

The process-wide target when no scope is active (default ConsoleTarget, or a HeadlessTerminal when CONSOLEMONSTER_HEADLESS is set). Prefer Terminal.Use, which affects only the current asynchronous flow; this is for hosts that own the whole process.