Aptivi - Manual
ProjectsWebsiteBlog
Terminaux - Manual
Terminaux - Manual
  • Welcome!
  • Breaking changes
    • API v1.0
    • API v2.0
    • API v3.0
    • API v4.0
    • API v5.0
    • API v6.0
    • API v7.0
  • Usage
    • Preface
    • Console Tools
      • Console Checker
        • Console Size Requirements
      • Image Rendering
        • Icons
      • Console Writers
        • Individual Writers
        • Cyclic Writers
          • Geometric Shapes
          • Charts
          • Text
          • Artistic
          • Progress Bars
          • Lists and Calendars
          • Miscellaneous
        • Informational Boxes
      • Textual UI
        • Interactive TUI
        • Console Screen
        • Console Resize Listener
        • VT Sequences
      • Console Wrapper
      • Console Colors
      • Color Templates
      • Presentation System
      • Console Extensions
      • Nerd Fonts
      • Terminal Info
      • Test Fixtures
      • Terminal Structures
      • Console Logging
    • Input Reader
      • Shells
        • Shell Structure
          • Help System
          • Command Parsing
          • Command Information
          • Command Switches
          • Shell Presets
          • Command Aliasing
      • Other Input
        • Keybindings
        • Choice-based inputs
        • Editors and Viewers
        • Figlet Font Selector
        • Color Wheel
        • Spinner Selector
      • Reader State
      • Reader Settings
      • Syntax Highlighting
      • Pointer Events
    • Color Sequences
      • Color Model Conversions
      • Color Model Parsing
      • Interop with System.Drawing.Color
  • Report an issue
  • Source code
  • API Reference
Powered by GitBook
On this page
Edit on GitHub
  1. Usage
  2. Input Reader

Syntax Highlighting

We need to highlight things to make them easier to find.

Last updated 7 months ago

Syntax highlighting is commonly found in code editors, code viewers, and some shells, such as zsh. We believe that syntax highlighting makes it easier to distinguish between parts of text, making it easier to read and understand the whole text, especially if you're reading a codebase, such as Terminaux's source code.

Syntax highlighting is also available on vim for code files and for configuration files for the same reason. As a result, Terminaux also provides syntax highlighting in your prompt to make the text that you've written easier to read and understand.

To try this feature out, you can execute the console demo of Terminaux, passing the PromptHighlighted argument.

The syntax highlighting tools provide you with functions to manage your highlighters, including registering them after getting an instance of the highlighter from its JSON representation that looks like the following:

Minimum JSON
{
    "Name": "custom",
    "Components": {
        "FirstWord": {
            "ComponentMatch": "/(?:^|(?:[.!?]\\s))(\\w+)/",
            "ComponentForegroundColor": "#00FF00",
            "ComponentBackgroundColor": "#000000",
        }
    }
}

All of your highlighters must follow this convention above to make a useful highlighter for your syntax. If done correctly, you must be able to get a registerable instance of a SyntaxHighlighting instance.

Registering this instance require making either a JSON file that contains the above syntax highlighter properties, or a JSON string variable, such as in , require getting the instance using the GetHighlighterFromJson() function. Afterwards, you can register it using RegisterHighlighter().

This registration process is necessary for your syntax highlighter to be usable in the terminal reader.

You need to keep a handy copy of the instance of your highlighter so that you can use it in the terminal reader settings like this:

Demo source code
var settingsCustom = new TermReaderSettings()
{
    SyntaxHighlighterEnabled = true,
    SyntaxHighlighter = template,
};

After that, you must pass the settings instance to the Read() function to be able to recognize your highlighter. You can also use the global settings to do the same thing. You can read more about how the reader settings works here:

If you need to get your highlighter in another function, or if you start the reader in another function, you can use the GetHighlighter() function, passing it the name of your highlighter defined in the name part of its JSON contents, as long as it's registered.

If you want to unregister a highlighter, you can use the UnregisterHighlighter() function. Be sure that you're really done with the highlighter before trying to call this function.

You can also save your highlighter to a JSON string which you can then save to a file using GetHighlighterToJson(). You can later read it using the GetHighlighterFromJson() function.

this demo's source code
Reader Settings