Skip to content

Commit 20379f2

Browse files
Add text rendering support (#2)
1 parent b998531 commit 20379f2

40 files changed

+697
-157
lines changed

src/Sandbox/Program.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Text;
21
using Spectre.Tui;
32

43
namespace Sandbox;
@@ -12,26 +11,30 @@ public static void Main(string[] args)
1211
using var terminal = Terminal.Create();
1312
var renderer = new Renderer(terminal);
1413

14+
Console.Title = "Spectre.Tui Sandbox";
15+
1516
while (running)
1617
{
1718
renderer.Draw((ctx, elapsed) =>
1819
{
1920
// Outer box
2021
ctx.Render(new BoxWidget(Color.Red));
21-
ctx.Render(new ClearWidget(
22-
new Rune('.')),
23-
ctx.Viewport.Inflate(-1, -1));
22+
ctx.Render(new ClearWidget('O'), ctx.Viewport.Inflate(-1, -1));
2423

2524
// Inner box
2625
var inner = ctx.Viewport.Inflate(new Size(-10, -5));
2726
ctx.Render(new BoxWidget(Color.Green), inner);
2827
ctx.Render(
29-
new ClearWidget(new Rune('O'), Decoration.Bold),
28+
new ClearWidget('.', Decoration.Bold),
3029
inner.Inflate(-1, -1));
3130

3231
// FPS
3332
ctx.Render(
34-
new TextWidget(((int)(1.0f / elapsed.TotalSeconds)).ToString(), foreground: Color.Green),
33+
new FpsWidget(((int)(1.0f / elapsed.TotalSeconds)).ToString(), foreground: Color.Green),
34+
inner.Inflate(-1, -1));
35+
36+
// Some text
37+
ctx.Render(Text.FromString("नमस्ते Happy Holidays 🎅 Happy Holidays\nHappy Holidays Happy Holidays Happy Holidays Happy Holidays Happy Holidays ", Color.Green),
3538
inner.Inflate(-1, -1));
3639
});
3740

src/Sandbox/Widgets/BoxWidget.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Sandbox;
44

55
public sealed class BoxWidget(Color? color = null) : IWidget
66
{
7-
public void Render(IRenderContext context)
7+
public void Render(RenderContext context)
88
{
99
var area = context.Viewport;
1010

@@ -13,32 +13,32 @@ public void Render(IRenderContext context)
1313
{
1414
if (x == 0)
1515
{
16-
context.SetRune(x, 0, '╭');
16+
context.SetSymbol(x, 0, '╭');
1717
context.SetForeground(x, 0, color);
18-
context.SetRune(x, area.Height - 1, '╰');
18+
context.SetSymbol(x, area.Height - 1, '╰');
1919
context.SetForeground(x, area.Height - 1, color);
2020
}
2121
else if (x == area.Width - 1)
2222
{
23-
context.SetRune(x, 0, '╮');
23+
context.SetSymbol(x, 0, '╮');
2424
context.SetForeground(x, 0, color);
25-
context.SetRune(x, area.Height - 1, '╯');
25+
context.SetSymbol(x, area.Height - 1, '╯');
2626
context.SetForeground(x, area.Height - 1, color);
2727
}
2828
else
2929
{
30-
context.SetRune(x, 0, '─');
30+
context.SetSymbol(x, 0, '─');
3131
context.SetForeground(x, 0, color);
32-
context.SetRune(x, area.Height - 1, '─');
32+
context.SetSymbol(x, area.Height - 1, '─');
3333
context.SetForeground(x, area.Height - 1, color);
3434
}
3535
}
3636

3737
// Sides
3838
for (var y = 1; y < area.Height - 1; y++)
3939
{
40-
context.SetRune(0, y, '│');
41-
context.SetRune(area.Width - 1, y, '│');
40+
context.SetSymbol(0, y, '│');
41+
context.SetSymbol(area.Width - 1, y, '│');
4242
context.SetForeground(0, y, color);
4343
context.SetForeground(area.Width - 1, y, color);
4444
}

src/Sandbox/Widgets/ClearWidget.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
using System.Text;
21
using Spectre.Tui;
32

43
namespace Sandbox;
54

65
public sealed class ClearWidget(
7-
Rune rune,
6+
char? symbol = null,
87
Decoration decoration = Decoration.None,
98
Color? foreground = null,
109
Color? background = null) : IWidget
1110
{
12-
public void Render(IRenderContext context)
11+
public void Render(RenderContext context)
1312
{
1413
for (var x = 0; x < context.Viewport.Width; x++)
1514
{
1615
for (var y = 0; y < context.Viewport.Height; y++)
1716
{
18-
context.GetCell(x, y)
19-
?.SetRune(rune)
17+
context.GetCell(x, y)?
18+
.SetSymbol(symbol ?? ' ')
2019
.SetDecoration(decoration)
2120
.SetForeground(foreground)
2221
.SetBackground(background);

src/Sandbox/Widgets/FpsWidget.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Spectre.Tui;
2+
3+
namespace Sandbox;
4+
5+
public sealed class FpsWidget : IWidget
6+
{
7+
private readonly string _text;
8+
private readonly Style _style;
9+
10+
public FpsWidget(string text,
11+
Color? foreground = null,
12+
Color? background = null)
13+
{
14+
ArgumentNullException.ThrowIfNull(text);
15+
16+
_text = $" FPS: {text} ";
17+
_style = new Style
18+
{
19+
Foreground = foreground ?? Color.Default,
20+
Background = background ?? Color.Default,
21+
};
22+
}
23+
24+
public void Render(RenderContext context)
25+
{
26+
context.Render(
27+
Text.FromString(_text, _style),
28+
new Rectangle(
29+
(context.Viewport.Width - _text.Length) / 2,
30+
context.Viewport.Height / 2,
31+
_text.Length, 1));
32+
}
33+
}

src/Sandbox/Widgets/TextWidget.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/Spectre.Tui.Testing/Spectre.Tui.Testing.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<ProjectReference Include="..\Spectre.Tui\Spectre.Tui.csproj"/>
9+
<ProjectReference Include="..\Spectre.Tui\Spectre.Tui.csproj" />
1010
</ItemGroup>
1111

1212
</Project>

src/Spectre.Tui.Testing/TestTerminal.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Spectre.Tui.Testing;
55
public sealed class TestTerminal : ITerminal
66
{
77
private readonly Size _size;
8-
private readonly char[] _buffer;
8+
private readonly string[] _buffer;
99
private int _position;
1010

1111
public bool SupportsAnsi { get; set; }
@@ -14,10 +14,10 @@ public sealed class TestTerminal : ITerminal
1414
public TestTerminal(Size? size)
1515
{
1616
_size = size ?? new Size(80, 25);
17-
_buffer = new char[_size.Area];
17+
_buffer = new string[_size.Area];
1818
_position = 0;
1919

20-
Array.Fill(_buffer, '•');
20+
Array.Fill(_buffer, "•");
2121
}
2222

2323
public void Dispose()
@@ -26,7 +26,7 @@ public void Dispose()
2626

2727
public void Clear()
2828
{
29-
Array.Fill(_buffer, '•');
29+
Array.Fill(_buffer, "•");
3030
_position = 0;
3131
}
3232

@@ -42,19 +42,10 @@ public void MoveTo(int x, int y)
4242

4343
public void Write(Cell cell)
4444
{
45-
_buffer[_position] = (char)cell.Rune.Value;
45+
_buffer[_position] = cell.Symbol;
4646
_position++;
4747
}
4848

49-
// public void Write(ReadOnlySpan<char> span)
50-
// {
51-
// foreach (var character in span)
52-
// {
53-
// _buffer[_position] = character;
54-
// _position++;
55-
// }
56-
// }
57-
5849
public void Flush()
5950
{
6051
Output = Render();

src/Spectre.Tui.Testing/TuiFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public string Render(IWidget widget)
2121
return _terminal.Output;
2222
}
2323

24-
public string Render(Action<IRenderContext> action)
24+
public string Render(Action<RenderContext> action)
2525
{
2626
_renderer.Draw((frame, _) =>
2727
{

src/Spectre.Tui.Tests/CellTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
using System.Text;
2-
using Shouldly;
3-
41
namespace Spectre.Tui.Tests;
52

63
public sealed class CellTests
@@ -14,7 +11,7 @@ public void Should_Have_Default_Rune()
1411
var cell = new Cell();
1512

1613
// Then
17-
cell.Rune.ShouldBe(new Rune(' '));
14+
cell.Symbol.ShouldBe(" ");
1815
}
1916

2017
[Fact]

0 commit comments

Comments
 (0)