Skip to content

jingxi0109/XtermBlazor

 
 

Repository files navigation

XtermBlazor

Brings xterm.js to Blazor

Live Demo: https://xtermblazor.project.tatlead.com

Demo

Prerequisites

Installation

1. Install the package

Find the package through NuGet Package Manager or install it with following command.

dotnet add package XtermBlazor

2. Add Imports

After the package is added, you need to add the following in your _Imports.razor

@using XtermBlazor

3. Add CSS & Font references

Add the following to your HTML head section, it's either index.html or _Host.cshtml depending on whether you're running WebAssembly or Server.

<link href="_content/XtermBlazor/XtermBlazor.css" rel="stylesheet" />

In the HTML body section of either index.html or _Host.cshtml add this:

<script src="_content/XtermBlazor/XtermBlazor.min.js"></script>

Basic Usage

<Xterm @ref="_terminal" Options="_options" OnFirstRender="@OnFirstRender" />

@code {
    private Xterm _terminal;

    private TerminalOptions _options = new TerminalOptions
    {
        CursorBlink = true,
        CursorStyle = CursorStyle.Bar,
        Theme =
        {
            Background = "#17615e",
        },
    };
    
    private async Task OnFirstRender()
    {
        await _terminal.WriteLine("Hello World");
    }
}

Addons

Xterm supports Addons

To use xterm-addon-fit addon, you need to add the following to your HTML body section either index.html or _Host.cshtml.

<!-- xterm-addon-fit CDN -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/xterm-addon-fit.min.js"></script>

<!-- Register addon to XtermBlazor -->
<script>window.XtermBlazor.registerAddon("xterm-addon-fit", new window.FitAddon.FitAddon());</script>

Usage with addons

<Xterm @ref="_terminal" Options="_options" AddonIds="_addonIds" OnFirstRender="@OnFirstRender" />

@code {
    private Xterm _terminal;

    private TerminalOptions _options = new TerminalOptions
    {
        CursorBlink = true,
        CursorStyle = CursorStyle.Bar,
    };
    
    private string[] _addonIds = new string[]
    {
        "xterm-addon-fit",
    };
    
    private async Task OnFirstRender()
    {
        // Invoke fit() function
        await _terminal.InvokeAddonFunctionVoidAsync("xterm-addon-fit", "fit");
        
        await _terminal.WriteLine("Hello World");
    }
}

Packages

No packages published

Languages

  • C# 50.9%
  • HTML 29.9%
  • CSS 10.4%
  • TypeScript 5.5%
  • JavaScript 3.3%