Consolonia

repository·main·Indexed 21 days ago

https://github.com/consolonia/consolonia

A framework that integrates Avalonia UI into Terminal User Interface (TUI) applications. It provides a unified control library, XAML support, data binding, and styling for console environments across Windows, macOS, and Linux. Key components include Consolonia.Controls for specialized brushes and controls, Consolonia.Controls.DataGrid for text-mode optimized grids, Consolonia.Fonts for AsciiArt fonts, and Consolonia.ManagedWindows for managed file and folder pickers via IStorageProvider.

Tokens
5.9K
Snippets
27
Records
35
Agent score
73%

What's inside Consolonia

  1. Overview of Consolonia.Controls components

    main

    The Consolonia.Controls package provides several specialized brushes and controls designed for console-based UI manipulation:

    • Brushes:
      • LineBrush: Draws a line.
      • BrightenBrush: Brightens the background color.
      • ShadeBrush: Shades the background color.
      • MoveConsoleCaretToPositionBrush: Moves the console caret to the position where the brush is drawn.
    • Controls:
      • ConsoleCaret: A control representing the console caret.
    • Platform Extensions:
      • OnPlatform: Extends Console as a target platform for Avalonia.
  2. Overview of Consolonia.ManagedWindows components

    main

    Consolonia.ManagedWindows provides managed windowing capabilities, drawing primitives, and platform infrastructure. The project is built upon several core architectural pillars:

    • Drawing Primitives: Handled via DrawingContextImpl for low-level graphics.
    • Pixel and Buffer Management: Managed through PixelBuffer for handling pixels, buffers, shading, and blending.
    • Windowing Infrastructure: Rooted in ConsoleWindow for native window management.
    • Platform Abstraction: The ConsoloniaPlatform serves as the fundamental root for platform-specific implementations.
    • Text Rendering: FormattedText is used for drawing text, managing carets, and handling selections in components like TextBox.

    Note: This project uses portions adapted from GUI.cs under the MIT license.

  3. Core concepts of Consolonia

    main

    Consolonia is a framework that injects the benefits of the Avalonia UI framework directly into Terminal User Interface (TUI) applications.

    Key architectural benefits include:

    • Layout & Positioning: Uses Avalonia's flexible layout system (responsive panels and alignment) instead of manual coordinates or brittle grids.
    • Styling & Theming: Leverages Avalonia's styling engine for rich color schemes, gradients, and reusable style resources.
    • Data Binding: Supports the full Avalonia data binding system, allowing you to bind to properties, collections, and commands for automatic UI updates.
    • Control Set: Provides a unified control library modeled on Avalonia UI elements (buttons, sliders, tree views, tab controls, etc.).
    • Event Handling: Uses an async-friendly event system consistent with Avalonia GUI applications.
    • Templating: Uses Avalonia's control templates to allow swapping visual structures without changing underlying logic.
    • Cross-Platform: Provides consistent rendering and key handling across Windows, macOS, and Linux via Avalonia's abstraction layer.
  4. Install and configure Consolonia.Fonts

    main

    To use AsciiArt fonts in your Consolonia application, you must register them during the application startup process. Update your Program.cs (or your application builder method) to include the .WithConsoleFonts() extension method on the AppBuilder instance.

    This makes the embedded AsciiArt fonts available to the system for use in your UI components.

    public static AppBuilder BuildAvaloniaApp()
    {
        return AppBuilder.Configure<App>()
            .UseConsolonia()
            .UseAutoDetectedConsole()
            .WithConsoleFonts(); // <---- Add this to add these fonts to your system.
    }
  5. Install Consolonia.Controls.DataGrid

    main

    Install the Consolonia.Controls.DataGrid package via NuGet to add text-mode optimized DataGrid templates, styles, and helpers to your Avalonia application.

    Requirements:

    • Target framework: net8.0
    • Avalonia version 12.0.3
    • Avalonia.Controls.DataGrid version 12.0.0
    • Consolonia.Core and Consolonia.Controls
    dotnet add package Consolonia.Controls.DataGrid
  6. Configure GPM via /etc/gpm.conf

    main

    GPM is configured by editing the /etc/gpm.conf file.

    Important Safety Note: This file is sourced by /etc/init.d/gpm. If you edit this file manually, you must protect all shell meta-characters from evaluation by quoting them.

    Common configuration settings include:

    • device: The input device path (e.g., /dev/input/mice).
    • responsiveness: Adjust this value (e.g., 15) if the mouse response feels too slow.
    • type: The mouse type (e.g., imps2).
    • append: Additional arguments to be appended to the command line. Use quotes if containing spaces or special characters.

    After modifying the configuration, you must restart the service for changes to take effect.

    # Edit the configuration
    sudo nano /etc/gpm.conf
    
    # Restart the service to apply changes
    sudo systemctl restart gpm
  7. Use AsciiArt fonts in Avalonia XAML

    main

    Once configured, you can apply AsciiArt fonts to any Avalonia control that supports the FontFamily property (such as TextBlock) using the fonts: prefix followed by the font name.

    Note that different fonts have specific supported font sizes to render correctly. Always check the supported font sizes for the specific font you are using.

    <TextBlock FontFamily="fonts:Consolonia#Standard" FontSize="8">...</TextBlock>
  8. Use Consolonia.Controls in XAML

    main

    To use the controls in your XAML files, first declare the console XML namespace using the URI https://github.com/jinek/consolonia. You can then instantiate controls like LineBrush directly in your XAML markup.

    <!-- Declare the namespace -->
    xmlns:console="https://github.com/jinek/consolonia"
    
    <!-- Use a control -->
    <console:LineBrush Brush="Red" LineStyle="Edge"/>
  9. Enable Consolonia storage provider

    main

    To use managed file and folder pickers in your Avalonia application, you must enable the ConsoloniaStorageProvider during the app startup sequence using the UseConsoloniaStorage() extension method on the AppBuilder.

    using Avalonia;
    using Consolonia.ManagedWindows.Storage;
    
    public static class Program
    {
        public static void Main(string[] args)
        {
            BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
        }
    
        public static AppBuilder BuildAvaloniaApp() =>
            AppBuilder.Configure<App>()
                      .UsePlatformDetect()
                      .UseConsoloniaStorage(); // <= enables Consolonia storage dialogs
    }
  10. Include Managed Windows styles

    main

    Managed windows require theme resources to render correctly. You can include these styles in your App.axaml using one of two methods:

    Use AutoManagedWindowStyles to automatically include styles based on your current Consolonia theme family.

    2. Explicit Inclusion

    Manually include specific theme files using StyleInclude.

    • Modern base styles: avares://Consolonia.ManagedWindows/Themes/Base.axaml
    • TurboVision theme: avares://Consolonia.ManagedWindows/Themes/TurboVision/TurboVision.axaml
    <!-- Automatic inclusion in App.axaml -->
    <Application xmlns="https://github.com/avaloniaui" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                 xmlns:themes="clr-namespace:Consolonia.Themes;assembly=Consolonia.ManagedWindows">
      <Application.Styles>
        <themes:AutoManagedWindowStyles/>
      </Application.Styles>
    </Application>