• Stars
    star
    123
  • Rank 290,145 (Top 6 %)
  • Language
    C#
  • License
    Other
  • Created about 3 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simplifies styling strings in the terminal for .NET application.

🖍 Kokuban

Kokuban simplifies styling strings in the terminal for .NET applications. (Kokuban is "blackboard" in Japanese.)

Kokuban is based on JavaScript library Chalk for many of its concepts and some of its code.

Features

  • Expressive API
  • Auto detects color support
  • Auto enables escape sequence support on Windows 10 20H1 or later
  • 256 (8-bit) / TrueColor (24-bit) colors support

Requirements

  • .NET Standard 2.0 or higher
  • Linux, macOS, Windows ...
    • Windows 10 Anniversary Update or later

Install

dotnet package add Kokuban

Usage

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

If you nest styled strings, the styles will be automatically stacked.

Console.WriteLine(
    Chalk.Bold.Gray.BgYellow[
        "" + 
        Chalk.White.BgRed["Hello"] +
        " " +
        Chalk.White.BgBlue["コンニチハ"] +
        "!!/"
    ]
);
// or
Console.WriteLine(
    Chalk.Bold.Gray.BgYellow + (
        "" + (Chalk.White.BgRed + "Hello") + " " + (Chalk.White.BgBlue + "コンニチハ") + "!!/"
    )
);

API

Chalk.<style>{.<style>...}[string] (indexer)

Chalk.Red.Underline["Hello"] // => "Hello" (Red + Underlined)

Chalk.<style>{.<style>...} + string (+ operator)

(Chalk.Red.Underline + "Hello") // => "Hello" (Red + Underlined)

ToStyledString extension method

Console.WriteLine("Foo Bar Baz".ToStyledString(Chalk.White.Blue));

Chalk.Create method

You can customize the behavior by explicitly instancing the AnsiStyle and use it.

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.

KokubanColorValue struct

KokubanColorValue struct represents a color of AnsiStyle.

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");

Styles

  • 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)

256 colors (8-bit) and TrueColor (24-bit) support

Kokuban also supports 256 colors (8-bit) and TrueColors (24-bit) on supported terminals, same as Chalk.

The automatic detection of support states and color downsampling are derived from Chalk.

Kokuban supports ANSI 256-palette index (Ansi256 method) and RGB format (Rgb method). However, Hex format (e.g. #AABBCC) is not yet supported.

License

MIT License

Copyright (c) Cysharp, Inc.
Copyright (c) Mayuki Sawatari <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

---- 
Part of the source code of this library is derived from the following libraries.
The original source codes are licensed under the MIT License.

chalk/supports-color
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)

chalk/ansi-styles
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)

Qix-/color-convert
Copyright © 2011-2016, Heather Arthur. Copyright © 2016-2021, Josh Junon.

More Repositories

1

UniTask

Provides an efficient allocation free async/await integration for Unity.
C#
8,201
star
2

MagicOnion

Unified Realtime/API framework for .NET platform and Unity.
C#
3,838
star
3

MemoryPack

Zero encoding extreme performance binary serializer for C# and Unity.
C#
3,288
star
4

R3

The new future of dotnet/reactive and UniRx.
C#
2,177
star
5

ZString

Zero Allocation StringBuilder for .NET and Unity.
C#
2,060
star
6

ConsoleAppFramework

Zero Dependency, Zero Overhead, Zero Reflection, Zero Allocation, AOT Safe CLI Framework powered by C# Source Generator.
C#
1,635
star
7

MasterMemory

Embedded Typed Readonly In-Memory Document Database for .NET and Unity.
C#
1,521
star
8

MessagePipe

High performance in-memory/distributed messaging pipeline for .NET and Unity.
C#
1,406
star
9

Ulid

Fast .NET C# Implementation of ULID for .NET and Unity.
C#
1,314
star
10

ZLogger

Zero Allocation Text/Structured Logger for .NET with StringInterpolation and Source Generator, built on top of a Microsoft.Extensions.Logging.
C#
1,262
star
11

SimdLinq

Drop-in replacement of LINQ aggregation operations extremely faster with SIMD.
C#
775
star
12

csbindgen

Generate C# FFI from Rust for automatically brings native code and C native library to .NET and Unity.
Rust
688
star
13

ObservableCollections

High performance observable collections and synchronized views, for WPF, Blazor, Unity.
C#
559
star
14

ProcessX

Simplify call an external process with the async streams in C# 8.0.
C#
453
star
15

YetAnotherHttpHandler

YetAnotherHttpHandler brings the power of HTTP/2 (and gRPC) to Unity and .NET Standard.
C#
354
star
16

UnitGenerator

C# Source Generator to create value-object, inspired by units of measure.
C#
330
star
17

RuntimeUnitTestToolkit

CLI/GUI Frontend of Unity Test Runner to test on any platform.
C#
300
star
18

AlterNats

An alternative high performance NATS client for .NET.
C#
284
star
19

NativeMemoryArray

Utilized native-memory backed array for .NET and Unity - over the 2GB limitation and support the modern API(IBufferWriter, ReadOnlySequence, scatter/gather I/O, etc...).
C#
276
star
20

StructureOfArraysGenerator

Structure of arrays source generator to make CPU Cache and SIMD friendly data structure for high-performance code in .NET and Unity.
C#
262
star
21

MagicPhysX

.NET PhysX 5 binding to all platforms(win, osx, linux) for 3D engine, deep learning, dedicated server of gaming.
Rust
258
star
22

PrivateProxy

Source Generator and .NET 8 UnsafeAccessor based high-performance strongly-typed private accessor for unit testing and runtime.
C#
239
star
23

KcpTransport

KcpTransport is a Pure C# implementation of RUDP for high-performance real-time network communication
C#
237
star
24

LogicLooper

A library for building server application using loop-action programming model on .NET.
C#
237
star
25

DFrame

Distributed load testing framework for .NET and Unity.
C#
223
star
26

Utf8StreamReader

Utf8 based StreamReader for high performance text processing.
C#
208
star
27

LitJWT

Lightweight, Fast JWT(JSON Web Token) implementation for .NET.
C#
199
star
28

Claudia

Unofficial Anthropic Claude API client for .NET.
C#
162
star
29

CsprojModifier

CsprojModifier performs additional processing when Unity Editor generates the .csproj.
C#
154
star
30

Utf8StringInterpolation

Successor of ZString; UTF8 based zero allocation high-peformance String Interpolation and StringBuilder.
C#
153
star
31

ValueTaskSupplement

Append supplemental methods(WhenAny, WhenAll, Lazy) to ValueTask.
C#
135
star
32

SlnMerge

SlnMerge merges the solution files when generating solution file by Unity Editor.
C#
114
star
33

GrpcWebSocketBridge

Yet Another gRPC over HTTP/1 using WebSocket implementation, primarily targets .NET platform.
C#
76
star
34

WebSerializer

Convert Object into QueryString/FormUrlEncodedContent for C# HttpClient REST Request.
C#
65
star
35

RandomFixtureKit

Fill random/edge-case value to target type for unit testing, supports both .NET Standard and Unity.
C#
46
star
36

Actions

41
star
37

DocfxTemplate

Patchworked DocFX v2 template for Cysharp
JavaScript
7
star
38

Multicaster

A framework for transparently invoking multiple instances or clients.
C#
5
star
39

com.unity.ide.visualstudio-backport

Backport of com.unity.ide.visualstudio to before Unity 2019.4.21
C#
1
star