Skip to content
ConsoleMonster
Class

LiveSession

ConsoleMonster.Live

Namespace
ConsoleMonster.Live
Assembly
ConsoleMonster
Modifiers
sealed

Single-use live renderer. Producers only enqueue; one background thread owns widget updates. Stop closes admission, joins that thread, drains accepted events and restores the terminal.

Inheritance

  • object
  • LiveSession

Usage

Explore 19 API references to this type

This type appears in these public API surfaces even when no hand-authored example is attached directly to the page.

Returned or exposed by 6

Accepted by parameters 13

Constructors

public LiveSession(IWidget root, LiveSessionOptions? options = null) #

Discovers targets in the widget tree. Definitions must remain stable after construction.

Parameters

root ConsoleMonster.Layout.IWidget required
Dashboard root.
options ConsoleMonster.Live.LiveSessionOptions? = null optional
Optional configuration, fixed when Start is called.

Methods

public void Cancel() #

Signals cooperative cancellation, stopping immediately only when StopOnCancellation is true. Call Stop after the producer has drained.

public void Delay(TimeSpan delay) #

Pauses work cancellably. With RecordingTarget.SkipDelays, first applies earlier queued updates and captures selected compact dashboard frames on the dispatcher instead of waiting. Selection and call counts belong to the recording target; checkpoint order assumes one producer. Checkpoints use a fixed animation tick and the elapsed time before the delay. Unless an explicit session clock is supplied, skipped recording delays advance the session's logical elapsed time by the requested interval after each checkpoint. Setting CONSOLEMONSTER_SNAPSHOT to 1, true or yes (case-insensitive) also skips the wait, without capturing a frame unless the session's recording target has SkipDelays enabled.

Parameters

delay System.TimeSpan required
A nonnegative finite interval, no greater than Int32.MaxValue milliseconds.

Exceptions

  • ArgumentOutOfRangeException – The interval is outside the WaitHandle timeout range.
  • InvalidOperationException – The session is not running, or the dispatcher calls this method.
  • OperationCanceledException – The session was cancelled or stopped.
public void Dispose() #

Stops the session and waits for final output and terminal restoration. A dispatcher callback requests its own loop to finish.

public bool Flush(TimeSpan timeout) #

Waits until earlier accepted events have reached output. Useful for a host's pipeline-thread output pump; it does not force a frame redraw.

Parameters

timeout System.TimeSpan required
Maximum wait. The render thread cannot synchronously wait for itself.

Returns

bool False if the session is closed or the timeout expired.
public LiveSessionCounters GetCounters() #

A snapshot of the session's counters, the target names that received updates but do not exist, and the errors, so hosts can report problems that would otherwise disappear behind the live screen.

Returns

LiveSessionCounters The snapshot.
public void Invalidate() #

Forces the next live tick to redraw fully.

public void Mark(string label) #

Adds a marker to the recording (a named point such as "Deploy started"), which Show-ConsoleReplay can jump to. Markers change nothing on screen; without a recording they are ignored.

Parameters

label string required
The marker text.
public LivePause Pause() #

Pauses drawing so a prompt can take the screen (for example "Continue on errors?" during a run). Updates keep being applied while paused, so the dashboard is current when it comes back. Dispose the pause to resume; the next frame redraws everything.

Returns

LivePause The pause, with the last frame as the backdrop for an overlay.
public void Post(LiveEvent liveEvent) #

Enqueues without per-event signaling. Topology and cell-capacity operations share the update gate. Events may be queued before Start.

Parameters

liveEvent ConsoleMonster.Live.LiveEvent required
Event; zero timestamps are stamped relative to Start.
public RegionHandle Region(string widgetId) #

A handle to the RegionWidget named widgetId: session.Region("main").Set(widget) replaces its content between frames, from any thread.

Parameters

widgetId string required
The slot name.

Returns

RegionHandle The handle.

Exceptions

  • ArgumentException – No slot has that name.
public void Register(ILiveTarget target) #

Registers an additional target, rejecting duplicate names and the reserved log alias.

Parameters

target ConsoleMonster.Live.ILiveTarget required
Target to register.
Replay 2 overloads
public static void Replay(IWidget root, TextReader jsonLines, LiveSessionOptions? options = null, double speed = 1, CancellationToken ct = default) #

Replays events in order with scaled original timing, then drains and restores the session.

Parameters

root ConsoleMonster.Layout.IWidget required
Target widget tree.
jsonLines System.IO.TextReader required
Borrowed recording input.
options ConsoleMonster.Live.LiveSessionOptions? = null optional
Session options; ReplayDelay can supply a deterministic scheduler.
speed double = 1 optional
Playback multiplier; zero or negative disables waits.
ct System.Threading.CancellationToken = default optional
Playback cancellation.
public static void Replay(IWidget root, IEnumerable<LiveEvent> events, LiveSessionOptions? options = null, double speed = 1, CancellationToken ct = default) #

Replays already decoded events with the same timing, cancellation and drain policy as a recording reader.

Parameters

root ConsoleMonster.Layout.IWidget required
Target widget tree.
events System.Collections.Generic.IEnumerable<ConsoleMonster.Live.LiveEvent> required
Events in recording order. A recording target with SkipDelays captures logical time groups without real waits, unless an explicit ReplayDelay scheduler is supplied.
options ConsoleMonster.Live.LiveSessionOptions? = null optional
Session configuration.
speed double = 1 optional
Playback multiplier; zero or negative disables waits.
ct System.Threading.CancellationToken = default optional
Playback cancellation.
public void Run(Action<LiveSession> work) #

Runs work inside a session, restoring the terminal even when work throws.

Parameters

work System.Action<ConsoleMonster.Live.LiveSession> required
Work to execute.
public async Task RunAsync(Func<LiveSession, CancellationToken, Task> work, CancellationToken ct = default) #

Runs asynchronous work with linked caller and session cancellation.

Parameters

work System.Func<ConsoleMonster.Live.LiveSession, System.Threading.CancellationToken, System.Threading.Tasks.Task> required
Work to execute.
ct System.Threading.CancellationToken = default optional
Caller cancellation.

Returns

Task Completion, preserving work exceptions after restoration.
public IDisposable Scope(string widgetId, string? key = null) #

Begins an ambient progress scope for this session.

Parameters

widgetId string required
Target name.
key string? = null optional
Optional item key.

Returns

IDisposable The scope lifetime.
public CellBuffer? Snapshot() #

A copy of the last dashboard frame drawn or captured as a skipped-delay recording checkpoint (for exports and screenshots), or null before the first frame and for line-based outputs without checkpoints. Safe to call from any thread.

Returns

CellBuffer? The frame copy, or null.
public void Start() #

Starts the dedicated background render thread. Repeated calls while running are harmless.

public void Stop() #

Stops admission, cancels work and joins for up to StopTimeout (two seconds by default). Accepted events are drained on the stopping caller after joining, before restoration. If user code blocks the render thread, console capture is restored immediately and final cleanup is deferred until that callback returns. Dispose, Run and RunAsync wait for that final cleanup before their lifetime ends. A call from the render thread also defers final cleanup to its loop exit.

public static IDisposable SuspendCapture() #

Until the returned scope is disposed, what the calling thread writes to Console.Out and Console.Error goes to the original streams instead of a live session's log. A host that hands a session's own output lines on through Console.Out (as PowerShell's console host does with redirected output) writes them inside this scope, so they are not captured, drawn and written again.

Returns

IDisposable The scope; dispose it on the same thread.
public bool TryGetTarget(string widgetId, out ILiveTarget? target) #

Looks up a registered target, including the reserved log alias.

Parameters

widgetId string required
Target name.
target ConsoleMonster.Live.ILiveTarget? required
Matching target, or null.

Returns

bool Whether the target exists.
public void Update(string widgetId, WidgetUpdate update) #

Applies one presence-aware update, rejecting unsupported fields before any event is published.

Parameters

widgetId string required
The target identifier.
update ConsoleMonster.Live.WidgetUpdate required
The explicitly supplied fields.
public void Write(string line, string level = "out") #

Enqueues a line for the session log target.

Parameters

line string required
Text.
level string = "out" optional
Stream or severity, default out.
public void WriteOutside(Action write) #

Runs write synchronously with the live area out of the way, for output the session does not draw: a PowerShell host writing a warning or verbose line, or a prompt such as ShouldContinue or -WarningAction Inquire that waits for an answer. Inline sessions write pending captured lines, clear their area and show the cursor; full-screen sessions leave the alternate screen. Then the action runs on the calling thread, for as long as it needs. Afterwards the area starts again below whatever the action wrote, so later frames never draw over it; a full-screen session returns to the alternate screen and redraws. No frame is drawn while the action runs: updates are applied and the next frame after it shows the current state. Calls from several threads run one at a time; a call made inside the action, or from a callback on the render thread, runs directly. What the thread writes to during the action goes to the console, not to the session log. Append-only (stream) sessions and stopped sessions just run the action.

Parameters

write System.Action required
The output to write.

Properties

public IWidget Root { get; } #

The root widget the session renders.

public static LiveSession? Current { get; } #

The most recently started session that is still running, across all runspaces.

public bool IsRunning { get; } #

Whether the session is accepting live work.

public CancellationToken Cancellation { get; } #

Cancelled by Stop or the first intercepted Ctrl+C.

public long Resizes { get; } #

The number of size changes the session redrew for (coalesced: rapid drags count once per redraw).

public long RenderErrors { get; } #

Frames where the layout threw and a one-line note was shown instead (the session keeps running).

public string? WidthDecision { get; private set; } #

The terminal width probe result for this session ("width probe (cursor report, 12 ms): ..."), the reason it was skipped, or null when probing was not applicable (Stream/Plain output, a borrowed output, or explicit width settings).

public TimeSpan Elapsed { get; } #

Time since Start, frozen after shutdown. During LiveSession.Replay it follows the recorded timeline (real time multiplied by the replay speed), so the header clock agrees with recorded timestamps. With a recording target's SkipDelays enabled and no explicit clock, it advances only as LiveSession.Delay completes.

public long EventsPosted { get; } #

Number of events accepted before shutdown closed admission.

public long EventsApplied { get; } #

Number of events successfully applied.

public long EventsDropped { get; } #

Number of events ignored because the target was unknown, or admission had closed.

public long FramesRendered { get; } #

Number of frames rendered, including the final frame.

public long ApplyErrors { get; } #

Number of target Apply calls that threw.

public Exception? LastError { get; } #

Most recent target, rendering, recording or cleanup exception.

public Exception? Failure { get; } #

The first fatal rendering, required-recording or output exception, including failed shutdown draining, flushing or summary output, or null.

public IReadOnlyCollection<string> TargetNames { get; } #

A snapshot of registered target names.

public bool IsPaused { get; } #

Whether drawing is paused for a prompt.

public string? RecordingPath { get; private set; } #

The automatic recording file of this session, or null.

Fields

public const string MarkerTarget #

The reserved target of marker events (LiveSession.Mark): recorded for replay, shown nowhere.

Value: $marker

Extension Methods

public static string ToHtml(LiveSession session, ExportOptions? options = null) #

Exports what a live session shows now as an HTML page.

Parameters

session ConsoleMonster.Live.LiveSession required
The session.
options ConsoleMonster.Export.ExportOptions? = null optional
Theme, font and window frame.

Returns

string The HTML.
public static string ToMarkdown(LiveSession session) #

Exports the current live-session snapshot as a fenced text block.

Parameters

session ConsoleMonster.Live.LiveSession required
A session that has drawn a frame.

Returns

string The Markdown code block.
public static byte[] ToPng(LiveSession session, GraphicsExportOptions? options = null) #

Encodes the latest live-session snapshot as PNG.

Parameters

session ConsoleMonster.Live.LiveSession required
options ConsoleMonster.Graphics.GraphicsExportOptions? = null optional

Returns

byte[]
public static string ToSvg(LiveSession session, ExportOptions? options = null) #

Exports what a live session shows now as an SVG image.

Parameters

session ConsoleMonster.Live.LiveSession required
The session.
options ConsoleMonster.Export.ExportOptions? = null optional
Theme, font and window frame.

Returns

string The SVG.
public static string ToText(LiveSession session, ExportOptions? options = null) #

Exports what a live session shows now as plain text.

Parameters

session ConsoleMonster.Live.LiveSession required
The session.
options ConsoleMonster.Export.ExportOptions? = null optional
Unused; for symmetry with the other formats.

Returns

string The text.