-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminalText.cs
More file actions
52 lines (46 loc) · 1.43 KB
/
TerminalText.cs
File metadata and controls
52 lines (46 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.Drawing;
using System.Numerics;
using System.Text.RegularExpressions;
namespace TerminalRenderer;
public class TerminalText : ITerminalRenderable
{
public Point Position;
public string Text;
public Alignment TextAlignment;
public TerminalPixel PixelType;
public TerminalText(Point pos, string text, TerminalPixel pixels, Alignment textAlignment = Alignment.Left)
{
Position = pos;
Text = text;
TextAlignment = textAlignment;
PixelType = pixels;
}
public enum Alignment
{
Left, Center, Right
}
public IEnumerable<TerminalPixel> Render()
{
var chunks = Text.Chunk(2).Select(x => new string(x).PadRight(2, ' '));
if (TextAlignment == Alignment.Right)
chunks = chunks.Reverse();
int length = chunks.Count();
int i = 0;
foreach (string chunk in chunks)
{
yield return PixelType with
{
Position = Position with
{
X = TextAlignment switch
{
Alignment.Left => Position.X + i++,
Alignment.Center => Position.X + i++ - length / 2,
Alignment.Right => Position.X - i++,
_ => throw new ArgumentOutOfRangeException()
}
}, PixelContent = chunk
};
}
}
}