Nest.Text 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Nest.Text --version 1.0.0
                    
NuGet\Install-Package Nest.Text -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Nest.Text" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Nest.Text" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Nest.Text" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Nest.Text --version 1.0.0
                    
#r "nuget: Nest.Text, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Nest.Text@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Nest.Text&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Nest.Text&version=1.0.0
                    
Install as a Cake Tool

Nest.Text

Nest.Text is a zero-dependency, fluent text generation library that helps you build structured content β€” from C#, Python, and YAML to HTML, XML, and more. It lets you describe what to generate β€” Nest takes care of how it's formatted.


πŸ“¦ Installation

dotnet add package Nest.Text

πŸš€ What Is It?

Nest.Text provides a builder-style API to generate code, markup, or any structured text using:

  • .L(...) – Write one or more lines
  • .B(b => { }) – Begin a block with nested content
  • .L(...).B(b => { }) – Write a logical block of lines

Nest adds or avoids line breaks based on chaining and structure awareness.


Line Break Behavior

Understanding how line breaks work with different usage patterns:

Usage Patterns

Lines

_.L("one");
_.L("two");
_.L("one");
_.L("two");
  • ❌ No line break will be added between these lines

Multiple Lines

βœ… Line break will be added here
_.L(
    "one",
    "two"
);
βœ… Line break will be added here
_.L("one");
_.L("two");

Multi-Line raw string literal

βœ… Line break will be added here
_.L(
    """
    one
    two
    """
);
βœ… Line break will be added here
_.L(
    """
    one
    two
    """
);
βœ… Line break will be added here

Strings having line breaks

βœ… Line break will be added here
_.L("one \n two");
_.L(); ⚠️ First explicit line break will override implicit line break
_.L(); βœ… Explicit line break will be added here
_.L("one \n two");
βœ… Line break will be added here

Method Chaining

βœ… Line break will be added here
_.L("one")
 .L("two").B(_ => 
 {
    _.L("three");
 });
βœ… Line break will be added here
  • ❌ No line break between chained calls

βš™οΈ Options

_.Options.BlockStyle = BlockStyle.Braces; // or IndentOnly (Default)
_.Options.IndentSize = 4;                 // spaces per indent level

πŸ”„ Smart Quote Replacement

You can use backticks (`) instead of escaped quotes in your strings:

_.L("Console.WriteLine(`Hello World!`);"); 
// Outputs: Console.WriteLine("Hello World!");

To customize or disable:

_.Options.RegisterCharReplacement('`', '"');
_.Options.RemoveCharReplacement('`');

βš™οΈ Options (Live Behavior & Inheritance)

Options in Nest.Text are live β€” changes apply immediately and affect all content added after the change.

πŸ”§ Live Indent Example

_.L("Console.WriteLine(`A`)"); // uses current indent size (e.g. 4)

_.Options.IndentSize = 0;
_.L("Console.WriteLine(`B`)"); // will have no indentation

_.Options.IndentSize = 4;
_.L("Console.WriteLine(`C`)"); // will again be indented

Each line reflects the indent size active at the time it's written.


πŸ“¦ BlockStyle Inheritance

When you change BlockStyle, it affects the current block and all nested blocks by default:

_.L("namespace Demo").B(_ =>
{
    _.Options.BlockStyle = BlockStyle.Braces;

    _.L("class A").B(_ =>
    {
        _.L("void Print()").B(_ =>
        {
            _.L("print(`Hello`)");
        });
    });
});

βœ… namespace Demo block itself & all blocks inside it will use now Braces.

βœ‹ Prevent Inheritance (Limit Scope)

If you want to apply a BlockStyle to just the current block without affecting nested blocks, reset it at the end:

_.L("namespace Demo").B(_ =>
{
    _.L("class A").B(_ =>
    {
        _.L("void Print()").B(_ =>
        {
            _.L("print(`Hello`)");
        });
    });

    _.Options.BlockStyle = BlockStyle.Braces;
});

πŸ” Inherited Automatically

Any block you enter inherits the parent’s Options by default β€” this includes indent size, block style, and character replacements. You can override or reset them at any level as needed.

This behavior makes it easy to dynamically shape output without needing multiple builders or deep config trees.


Reusing Common Builder Patterns

Since .B() accepts a lambda, you can pass a method that takes an ITextBuilder parameter β€” this lets you keep the structure clean by moving code blocks into reusable methods.


βœ… Example: Extracting an If-Else Block

Instead of writing everything inline:

_.L("if (count > 6)").B(_ =>
{
    _.L("Console.WriteLine(`Hello World!`);");
    _.L("Console.WriteLine(`Hello Again!`);");
})
.L("else").B(_ =>
{
    _.L("Console.WriteLine(`Hello World!`);");
});

You can extract it into a separate method:

void AddIfElse(ITextBuilder _)
{
    _.L("if (count > 6)").B(_ =>
    {
        _.L("Console.WriteLine(`Hello World!`);");
        _.L("Console.WriteLine(`Hello Again!`);");
    })
    .L("else").B(_ =>
    {
        _.L("Console.WriteLine(`Hello World!`);");
    });
}

And call it like this:

_.L("public static void Main(string[] args)").B(_ => AddIfElse(_));

βœ… This keeps the Main method block clean and readable.


🧩 Using .Append()

.Append() allows you to insert multiple external methods cleanly:

_.L("public static void Main(string[] args)").B(_ => 
    _.Append(_ => AddIfElse(_)).Append(_ => AddLoop(_))
);

Each method AddIfElse(), AddLoop() still receives the same builder instance, so indentation and formatting stay consistent.


πŸ” Using .Chain()

If you're inside a chain, use .Chain() and pass a method that takes IChainBuilder:

void AddMainBody(IChainBuilder _)
{
    _.B(_ =>
    {
        _.L("if (count > 6)").B(_ =>
        {
            _.L("Console.WriteLine(`Hello World!`);");
        })
        .L("else").B(_ =>
        {
            _.L("Console.WriteLine(`Goodbye!`);");
        });
    });
}

Then call it like:

_.L("public static void Main(string[] args)")
    .Chain(_ => AddMainBody(_));

Why?

  • βœ… Keeps your generation logic modular.
  • βœ… Makes complex structures easier to read and maintain.
  • βœ… Allows mixing and matching reusable blocks.

This approach is especially helpful when generating large sections like methods, conditionals, loops, or class structures.


πŸ§ͺ C# Example (Braces Block Style with Chaining)

var _ = TextBuilder.Create();
_.Options.BlockStyle = BlockStyle.Braces;
_.Options.IndentSize = 4;

_.L("using System.Text;");

_.L("namespace MyProgram").B(_ =>
{
    _.L("public class MyProgram").B(_ =>
    {
        _.L("public static void Main(string[] args)").B(_ =>
        {
            _.L("if (count > 6)").B(_ =>
            {
                _.L("Console.WriteLine(`Hello World!`);");
                _.L("Console.WriteLine(`Hello Again!`);");
            })
            .L("else").B(_ =>
            {
                _.L("Console.WriteLine(`Hello World!`);");
            });
        });
    });
});

Console.WriteLine(_.ToString());

🐍 Python Example (IndentOnly)

var _ = TextBuilder.Create();
_.Options.BlockStyle = BlockStyle.IndentOnly;

_.L("def greet():").B(_ =>
{
    _.L("print(`Hello World!`)");
    _.L("print(`Hello Again!`)");
});

Console.WriteLine(_.ToString());

🌐 HTML Example

var _ = TextBuilder.Create();
_.Options.BlockStyle = BlockStyle.IndentOnly;
_.Options.IndentSize = 2;

_.L("<div>").B(_ =>
{
    _.L("<span>Hello World!</span>");
    _.L("<span>Hello Again!</span>");
}
).L("</div>");

Console.WriteLine(_.ToString());

πŸ“„ XML Example

var _ = TextBuilder.Create();
_.Options.BlockStyle = BlockStyle.IndentOnly;

_.L("<config>").B(_ =>
{
    _.L("<entry key=`theme`>dark</entry>");
    _.L("<entry key=`lang`>en</entry>");
}
).L("</config>");

Console.WriteLine(_.ToString());

πŸ“˜ YAML Example

var _ = TextBuilder.Create();
_.Options.BlockStyle = BlockStyle.IndentOnly;

_.L("library:").B(_ =>
{
    _.L("name: `Nest`");
    _.L("use: `Structured Text Generation`");

    _.L("features:").B(_ =>
    {
        _.L("- Automated Indentation");
        _.L("- Easy To Use");
        _.L("- Zero Dependency");
    });
});

Console.WriteLine(_.ToString());

πŸͺ› Debugging

You can inspect the builder at any point during generation:

_.L("if (count > 0)").B(_ =>
{
    _.L("Console.WriteLine(`Positive`);");

    Console.WriteLine("--- Debug Snapshot ---");
    Console.WriteLine(_.ToString()); // View current generated output
});

Alternatively, set a breakpoint anywhere and inspect the builder in your debugger. This is especially useful when checking structure, indentation, or formatting mid-flow.

Since everything is standard C#, you can step through and verify behavior interactively β€” no custom tooling needed.


πŸ“š Summary

  • Fluent and chainable API
  • Smart formatting β€” line breaks where needed, not where not
  • Custom indentation and block styles
  • Backtick-friendly string writing
  • Debuggable at every step
  • No dependencies, works anywhere .NET runs

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 98 7/31/2025
1.0.2 100 7/30/2025
1.0.1 374 7/25/2025
1.0.0 280 7/20/2025