How nested styles work in Kokuban
main+ operator), the styles from both levels are combined in the output. This allows for complex, layered terminal UI elements.repository·main·Indexed 41 days ago
https://github.com/cysharp/kokubanA .NET library for terminal string styling inspired by Chalk, providing an expressive API for applying ANSI colors and decorations to console output with support for automatic style stacking and RGB colors.
+ operator), the styles from both levels are combined in the output. This allows for complex, layered terminal UI elements.To use Kokuban in your .NET project, add the NuGet package using the dotnet CLI.
dotnet package add KokubanKokuban provides several ways to apply ANSI styles to strings. You can use the + operator, the indexer on Chalk style objects, the ToStyledString extension method, or the Render method for RGB colors.
When nesting styled strings, styles are automatically stacked.
using Kokuban;
// Use `+` operator.
Console.WriteLine(Chalk.Red + "Hello");
Console.WriteLine(Chalk.Red + ("Hello " + (Chalk.Underline.BgBlue + "World")) + "!");
// Use indexer.
Console.WriteLine(Chalk.Red.Underline["Hello"]);
Console.WriteLine(Chalk.Red.Underline["Hello " + Chalk.Underline.BgBlue["World"]]);
// Use `ToStyledString` extension method.
Console.WriteLine("Foo Bar Baz".ToStyledString(Chalk.White.Blue));
// Use `Render` method.
Console.WriteLine(Chalk.Rgb(255, 128, 128).Render("Hello Konnichiwa!"));
// Nested styles stack automatically
Console.WriteLine(
Chalk.Bold.Gray.BgYellow[
"\" +
Chalk.White.BgRed["Hello"] +
" " +
Chalk.White.BgBlue["コンニチハ"] +
"!!/"
]
);You can customize the behavior of the styling engine (such as forcing plain text output regardless of terminal support) by using Chalk.Create with a KokubanOptions object.
var chalk = Chalk.Create(new KokubanOptions
{
// The output will be forced to plain text, whether the terminal supports colors or not.
Mode = KokubanColorMode.None,
});
Console.WriteLine(chalk.Red.Underline["Hello"]); // => "Hello" without escape sequences.The KokubanColorValue struct allows you to represent colors using different ANSI methods: basic 4-bit, 8-bit (index), or 24-bit (RGB). You can then apply these to a foreground or background using Chalk.Foreground or Chalk.Background.
var byColor = KokubanColorValue.FromColor(KokubanColor.Blue);
var byCode = KokubanColorValue.FromBasic(31); // 4-bit
var byIndex = KokubanColorValue.FromIndex(196); // 8-bit (Index)
var byRgb = KokubanColorValue.FromRgb(128, 0, 0); // 24-bit
Console.WriteLine(Chalk.Foreground(byColor) + "Foo Bar");Kokuban provides a wide range of decorations and colors for both foreground and background styling.
### Decorations
- Bold
- Dim
- Italic
- Underline
- Inverse
- Overline
### Foreground colors
- Black, Red, Green, Yellow, Blue, Magenta, Cyan, White, Gray, Grey
- BrightBlack, BrightRed, BrightGreen, BrightYellow, BrightBlue, BrightMagenta, BrightCyan, BrightWhite
- `Foreground` (for 3 or 4-bit)
- `Ansi256` (for 8-bit)
- `Rgb` (for 24-bit)
### Background colors
- BgBlack, BgRed, BgGreen, BgYellow, BgBlue, BgMagenta, BgCyan, BgWhite, BgGray, BgGrey
- BgBrightBlack, BgBrightRed, BgBrightGreen, BgBrightYellow, BgBrightBlue, BgBrightMagenta, BgBrightCyan, BgBrightWhite
- `Background` (for 3 or 4-bit)
- `BgAnsi256` (for 8-bit)
- `BgRgb` (for 24-bit)