Skip to content

Commit

Permalink
Add support for more SGR decorations (underline color, underline styl…
Browse files Browse the repository at this point in the history
…e, rapid blink).

Fixes #67.
  • Loading branch information
alexrp committed Jan 2, 2022
1 parent 0e4ae9a commit fbf1ec1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
27 changes: 24 additions & 3 deletions src/core/Text/Control/ControlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -572,15 +572,33 @@ public ControlBuilder SetBackgroundColor(byte r, byte g, byte b)
.Print(gSpan[..gLen]).Print(";").Print(bSpan[..bLen]).Print("m");
}

public ControlBuilder SetUnderlineColor(byte r, byte g, byte b)
{
Span<char> rSpan = stackalloc char[StackBufferSize];
Span<char> gSpan = stackalloc char[StackBufferSize];
Span<char> bSpan = stackalloc char[StackBufferSize];

_ = r.TryFormat(rSpan, out var rLen);
_ = g.TryFormat(gSpan, out var gLen);
_ = b.TryFormat(bSpan, out var bLen);

return Print(CSI).Print("58;2;").Print(rSpan[..rLen]).Print(";")
.Print(gSpan[..gLen]).Print(";").Print(bSpan[..bLen]).Print("m");
}

public ControlBuilder SetDecorations(
bool bold = false,
bool faint = false,
bool italic = false,
bool underline = false,
bool curlyUnderline = false,
bool dottedUnderline = false,
bool dashedUnderline = false,
bool blink = false,
bool rapidBlink = false,
bool invert = false,
bool invisible = false,
bool strike = false,
bool strikethrough = false,
bool doubleUnderline = false,
bool overline = false)
{
Expand All @@ -601,15 +619,18 @@ void HandleMode(bool value, ReadOnlySpan<char> code)
_ = Print(code);
}

// TODO: Support more exotic decorations?
HandleMode(bold, "1");
HandleMode(faint, "2");
HandleMode(italic, "3");
HandleMode(underline, "4");
HandleMode(curlyUnderline, "4:3");
HandleMode(dottedUnderline, "4:4");
HandleMode(dashedUnderline, "4:5");
HandleMode(blink, "5");
HandleMode(rapidBlink, "6");
HandleMode(invert, "7");
HandleMode(invisible, "8");
HandleMode(strike, "9");
HandleMode(strikethrough, "9");
HandleMode(doubleUnderline, "21");
HandleMode(overline, "53");

Expand Down
21 changes: 17 additions & 4 deletions src/samples/attributes/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,36 @@ await OutAsync(
.SetDecorations(underline: true)
.PrintLine("This text is underlined.")
.ResetAttributes()
.SetDecorations(curlyUnderline: true)
.PrintLine("This text is underlined (curly).")
.ResetAttributes()
.SetDecorations(dottedUnderline: true)
.PrintLine("This text is underlined (dotted).")
.ResetAttributes()
.SetDecorations(dashedUnderline: true)
.PrintLine("This text is underlined (dashed).")
.ResetAttributes()
.SetDecorations(blink: true)
.PrintLine("This text is blinking.")
.ResetAttributes()
.SetDecorations(rapidBlink: true)
.PrintLine("This text is rapidly blinking.")
.ResetAttributes()
.SetDecorations(invert: true)
.PrintLine("This text is inverted.")
.ResetAttributes()
.SetDecorations(invisible: true)
.PrintLine("This text is invisible.")
.SetDecorations(strike: true)
.PrintLine("This text is struck through.")
.ResetAttributes()
.SetDecorations(overline: true)
.PrintLine("This text is overlined.")
.SetDecorations(strikethrough: true)
.PrintLine("This text is struck through.")
.ResetAttributes()
.SetDecorations(doubleUnderline: true)
.PrintLine("This text is doubly underlined.")
.ResetAttributes()
.SetDecorations(overline: true)
.PrintLine("This text is overlined.")
.ResetAttributes()
.OpenHyperlink(new("https://google.com"))
.PrintLine("This is a Google hyperlink.")
.CloseHyperlink()
Expand Down

0 comments on commit fbf1ec1

Please sign in to comment.