An open API service indexing awesome lists of open source software.

https://github.com/devlooped/spectre.yaml

Display YAML in Spectre.Console
https://github.com/devlooped/spectre.yaml

dotnet spectre-console yaml

Last synced: 2 months ago
JSON representation

Display YAML in Spectre.Console

Awesome Lists containing this project

README

          

# Spectre.Yaml

[![Version](https://img.shields.io/nuget/vpre/Devlooped.Spectre.Yaml.svg?color=royalblue)](https://www.nuget.org/packages/Devlooped.Spectre.Yaml)
[![Downloads](https://img.shields.io/nuget/dt/Devlooped.Spectre.Yaml.svg?color=darkmagenta)](https://www.nuget.org/packages/Devlooped.Spectre.Yaml)
[![EULA](https://img.shields.io/badge/EULA-OSMF-blue?labelColor=black&color=C9FF30)](https://github.com/devlooped/oss/blob/main/osmfeula.txt)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/devlooped/oss/blob/main/license.txt)

## Open Source Maintenance Fee

To ensure the long-term sustainability of this project, users of this package who generate
revenue must pay an [Open Source Maintenance Fee](https://opensourcemaintenancefee.org).
While the source code is freely available under the terms of the [License](license.txt),
this package and other aspects of the project require [adherence to the Maintenance Fee](osmfeula.txt).

To pay the Maintenance Fee, [become a Sponsor](https://github.com/sponsors/devlooped) at the proper
OSMF tier. A single fee covers all of [Devlooped packages](https://www.nuget.org/profiles/Devlooped).

## Overview

**Devlooped.Spectre.Yaml** adds a `YamlText` renderable to [Spectre.Console](https://spectreconsole.net/)
that displays YAML with syntax-highlighted tokens (keys, strings, numbers, booleans, nulls, and
comments). It also accepts JSON and arbitrary .NET objects, automatically converting them to YAML
before rendering.

![](https://raw.githubusercontent.com/devlooped/Spectre.Yaml/main/assets/img/order.png)

## Usage

### From a YAML string

```csharp
using Spectre.Console;

AnsiConsole.Write(new YamlText("""
server:
host: localhost
port: 8080
tls: true
"""));
```

### From a .NET object

```csharp
using Spectre.Console;

var config = new
{
Server = new { Host = "localhost", Port = 8080, Tls = true },
Retries = 3,
Tags = new[] { "web", "api" },
};

AnsiConsole.Write(new YamlText(config));
```

`System.Text.Json.JsonSerializer` serializes the object; the resulting JSON is then converted
to YAML. `JsonNode` and `JsonElement` overloads are also available.

### Inside a Panel

```csharp
using Spectre.Console;

AnsiConsole.Write(
new Panel(new YamlText(myObject))
.Header("Configuration")
.BorderColor(Color.Yellow)
.Padding(1, 1));
```

## Customizing colors

Each token type has a configurable `Style`. Use the fluent extension methods for the most
concise syntax:

```csharp
var text = new YamlText(yaml)
.KeyColor(Color.Yellow)
.StringColor(Color.Cyan1)
.NumberColor(Color.Blue)
.BooleanColor(Color.Green)
.NullColor(Color.Grey)
.CommentColor(Color.DarkSlateGray1);

AnsiConsole.Write(text);
```

Or assign `Style` objects directly when you need full control (foreground, background,
decorations):

```csharp
var text = new YamlText(yaml)
{
KeyStyle = new Style(Color.Yellow, decoration: Decoration.Bold),
StringStyle = new Style(Color.Cyan1),
};
```

### Default colors

| Token | Default |
|----------|---------------------|
| Key | `Color.Grey` |
| String | `Color.Red` |
| Number | `Color.Blue` |
| Boolean | `Color.Green` |
| Null | `Color.Grey` |
| Comment | `Color.Grey` + Dim |

---

# Sponsors

[![Clarius Org](https://avatars.githubusercontent.com/u/71888636?v=4&s=39 "Clarius Org")](https://github.com/clarius)
[![MFB Technologies, Inc.](https://avatars.githubusercontent.com/u/87181630?v=4&s=39 "MFB Technologies, Inc.")](https://github.com/MFB-Technologies-Inc)
[![Khamza Davletov](https://avatars.githubusercontent.com/u/13615108?u=11b0038e255cdf9d1940fbb9ae9d1d57115697ab&v=4&s=39 "Khamza Davletov")](https://github.com/khamza85)
[![SandRock](https://avatars.githubusercontent.com/u/321868?u=99e50a714276c43ae820632f1da88cb71632ec97&v=4&s=39 "SandRock")](https://github.com/sandrock)
[![DRIVE.NET, Inc.](https://avatars.githubusercontent.com/u/15047123?v=4&s=39 "DRIVE.NET, Inc.")](https://github.com/drivenet)
[![Keith Pickford](https://avatars.githubusercontent.com/u/16598898?u=64416b80caf7092a885f60bb31612270bffc9598&v=4&s=39 "Keith Pickford")](https://github.com/Keflon)
[![Thomas Bolon](https://avatars.githubusercontent.com/u/127185?u=7f50babfc888675e37feb80851a4e9708f573386&v=4&s=39 "Thomas Bolon")](https://github.com/tbolon)
[![Kori Francis](https://avatars.githubusercontent.com/u/67574?u=3991fb983e1c399edf39aebc00a9f9cd425703bd&v=4&s=39 "Kori Francis")](https://github.com/kfrancis)
[![Reuben Swartz](https://avatars.githubusercontent.com/u/724704?u=2076fe336f9f6ad678009f1595cbea434b0c5a41&v=4&s=39 "Reuben Swartz")](https://github.com/rbnswartz)
[![Jacob Foshee](https://avatars.githubusercontent.com/u/480334?v=4&s=39 "Jacob Foshee")](https://github.com/jfoshee)
[![](https://avatars.githubusercontent.com/u/33566379?u=bf62e2b46435a267fa246a64537870fd2449410f&v=4&s=39 "")](https://github.com/Mrxx99)
[![Eric Johnson](https://avatars.githubusercontent.com/u/26369281?u=41b560c2bc493149b32d384b960e0948c78767ab&v=4&s=39 "Eric Johnson")](https://github.com/eajhnsn1)
[![Jonathan ](https://avatars.githubusercontent.com/u/5510103?u=98dcfbef3f32de629d30f1f418a095bf09e14891&v=4&s=39 "Jonathan ")](https://github.com/Jonathan-Hickey)
[![Ken Bonny](https://avatars.githubusercontent.com/u/6417376?u=569af445b6f387917029ffb5129e9cf9f6f68421&v=4&s=39 "Ken Bonny")](https://github.com/KenBonny)
[![Simon Cropp](https://avatars.githubusercontent.com/u/122666?v=4&s=39 "Simon Cropp")](https://github.com/SimonCropp)
[![agileworks-eu](https://avatars.githubusercontent.com/u/5989304?v=4&s=39 "agileworks-eu")](https://github.com/agileworks-eu)
[![Zheyu Shen](https://avatars.githubusercontent.com/u/4067473?v=4&s=39 "Zheyu Shen")](https://github.com/arsdragonfly)
[![Vezel](https://avatars.githubusercontent.com/u/87844133?v=4&s=39 "Vezel")](https://github.com/vezel-dev)
[![ChilliCream](https://avatars.githubusercontent.com/u/16239022?v=4&s=39 "ChilliCream")](https://github.com/ChilliCream)
[![4OTC](https://avatars.githubusercontent.com/u/68428092?v=4&s=39 "4OTC")](https://github.com/4OTC)
[![domischell](https://avatars.githubusercontent.com/u/66068846?u=0a5c5e2e7d90f15ea657bc660f175605935c5bea&v=4&s=39 "domischell")](https://github.com/DominicSchell)
[![Adrian Alonso](https://avatars.githubusercontent.com/u/2027083?u=129cf516d99f5cb2fd0f4a0787a069f3446b7522&v=4&s=39 "Adrian Alonso")](https://github.com/adalon)
[![torutek](https://avatars.githubusercontent.com/u/33917059?v=4&s=39 "torutek")](https://github.com/torutek)
[![mccaffers](https://avatars.githubusercontent.com/u/16667079?u=110034edf51097a5ee82cb6a94ae5483568e3469&v=4&s=39 "mccaffers")](https://github.com/mccaffers)
[![Seika Logiciel](https://avatars.githubusercontent.com/u/2564602?v=4&s=39 "Seika Logiciel")](https://github.com/SeikaLogiciel)
[![Andrew Grant](https://avatars.githubusercontent.com/devlooped-user?s=39 "Andrew Grant")](https://github.com/wizardness)
[![Lars](https://avatars.githubusercontent.com/u/1727124?v=4&s=39 "Lars")](https://github.com/latonz)
[![prime167](https://avatars.githubusercontent.com/u/3722845?v=4&s=39 "prime167")](https://github.com/prime167)

[![Sponsor this project](https://avatars.githubusercontent.com/devlooped-sponsor?s=118 "Sponsor this project")](https://github.com/sponsors/devlooped)

[Learn more about GitHub Sponsors](https://github.com/sponsors)