-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathITextUI.cs
More file actions
263 lines (224 loc) · 9.02 KB
/
ITextUI.cs
File metadata and controls
263 lines (224 loc) · 9.02 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Security;
using System.Text;
using Console = Colorful.Console;
namespace ToSTextClient
{
public interface ITextUI
{
TextClient Game { get; }
ViewRegistry Views { get; }
IReadOnlyDictionary<string, Command> Commands { get; }
string StatusLine { get; set; }
CommandContext CommandContext { get; set; }
bool RunInput { get; set; }
void RegisterCommand(Command command, params string[] names);
void RegisterMainView(IMainView view, params string[] names);
void RegisterSideView(IView view, params string[] names);
void SetMainView(IMainView view);
void OpenSideView(IView view);
void CloseSideView(IView view);
void SetInputContext(IInputView view);
void RedrawTimer();
void RedrawCursor();
void AudioAlert();
void Run();
}
public interface IView : IContextual
{
int MinimumWidth { get; }
int MinimumHeight { get; }
IPinnedView PinnedView { get; }
event Action<string, Exception> OnException;
event Action OnTextChange;
IEnumerable<FormattedString> Lines(int width);
void Redraw();
}
public interface IMainView : IView
{
bool AllowGeneralInput { get; }
void GeneralInput(string input);
}
public interface IPinnedView : IView
{
IEnumerable<INamedTimer> Timers { get; }
}
public interface ITextView : IView
{
event Action<int, FormattedString> OnAppend;
event Action<int, FormattedString> OnReplace;
void AppendLine(FormattedString text);
void AppendLine(FormattedString format, params object[] args);
void ReplaceLine(int index, FormattedString text);
void ReplaceLine(int index, FormattedString format, params object[] args);
void Clear();
}
public interface IHomeView : ITextView, IMainView
{
INamedTimer FWotDTimer { get; }
INamedTimer QueueTimer { get; }
}
public interface IGameView : ITextView, IMainView
{
IEditableNamedTimer PhaseTimer { get; }
}
public interface IWillView : IView
{
string Title { get; set; }
string Value { get; set; }
}
public interface IEditableWillView : IView
{
string Title { get; set; }
event Action<string> OnSave;
}
public interface IHelpView : IView
{
(IDocumented cmd, string[] names)? Topic { get; set; }
event Action OnShowHelp;
}
public interface IExceptionView : IMainView
{
Exception Exception { get; set; }
}
public interface IInputView : IView
{
(int x, int y) Cursor { get; }
event Action OnCursorChange;
void KeyPress(ConsoleKeyInfo key);
void Close();
}
public interface IAuthView : IInputView, IMainView
{
FormattedString Status { set; }
event Action<Socket, string, SecureString> OnAuthenticate;
}
public class FormattedString
{
public string RawValue => values.Select(v => v.raw).Aggregate(new StringBuilder(), (sb, raw) => sb.Append(raw)).ToString();
protected (string raw, Color? fg, Color? bg)[] values;
protected FormattedString(params (string raw, Color? fg, Color? bg)[] values) => this.values = values;
public void Render(int width)
{
for (int index = 0; index < values.Length && width > 0; width -= values[index++].raw.Length)
{
Console.ForegroundColor = values[index].fg ?? Color.White;
Console.BackgroundColor = values[index].bg ?? Color.Black;
Console.Write(values[index].raw.Limit(width));
}
if (width > 0) Console.Write("".PadRight(width));
Console.ForegroundColor = Color.White;
Console.BackgroundColor = Color.Black;
//Console.ResetColor(); // Reset colour after padding to allow padding to have last set BG colour
}
public override string ToString() => RawValue;
internal static FormattedString Format(FormattedString format, params object[] args)
{
if (format == null) return null;
return new FormattedString(format.values.SelectMany(v => Format(v, args)).ToArray());
}
private static IEnumerable<(string raw, Color? fg, Color? bg)> Format((string raw, Color? fg, Color? bg) value, params object[] args)
{
StringBuilder sb = new StringBuilder(value.raw.Length);
StringBuilder nb = new StringBuilder();
StringBuilder eb = new StringBuilder();
bool inBlock = false, indexEnd = false;
foreach (char c in value.raw)
{
switch (c)
{
default:
if (inBlock)
{
if (indexEnd) eb.Append(c);
else nb.Append(c);
}
else sb.Append(c);
break;
case '{':
if (inBlock)
{
sb.Append('{');
sb.Append(nb.ToString());
if (indexEnd)
{
sb.Append(':');
sb.Append(eb.ToString());
}
}
nb.Clear();
eb.Clear();
inBlock = true;
indexEnd = false;
break;
case '}':
if (inBlock && uint.TryParse(nb.ToString(), out uint parsed) && parsed < args.Length && args[parsed] is FormattedString formatted)
{
yield return (string.Format(sb.ToString(), args), value.fg, value.bg);
sb.Clear();
foreach ((string raw, Color? fg, Color? bg) rawValue in formatted.values) yield return rawValue;
}
else if (inBlock)
{
sb.Append('{');
sb.Append(nb.ToString());
if (indexEnd)
{
sb.Append(':');
sb.Append(eb.ToString());
}
sb.Append('}');
}
inBlock = false;
break;
case ':':
if (!inBlock || indexEnd) goto default;
indexEnd = true;
break;
}
}
yield return (string.Format(sb.ToString(), args), value.fg, value.bg);
}
public static FormattedString Concat(IEnumerable<FormattedString> values) => new FormattedString(values.SelectMany(sr => sr.values).ToArray());
public static FormattedString Concat(params FormattedString[] values) => Concat((IEnumerable<FormattedString>)values);
public static FormattedString From(params (string raw, Color? fg, Color? bg)[] values) => new FormattedString(values);
public static implicit operator FormattedString(string value) => new FormattedString((value, null, null));
public static implicit operator FormattedString((string value, Color? bg) value) => new FormattedString((value.value, null, value.bg));
public static implicit operator FormattedString((string value, Color? fg, Color? bg) value) => new FormattedString(value);
public static FormattedString operator +(FormattedString a, string b)
{
FormattedString result = new FormattedString(new(string raw, Color? fg, Color? bg)[a.values.Length]);
Array.Copy(a.values, result.values, result.values.Length);
result.values[result.values.Length - 1].raw += b;
return result;
}
public static FormattedString operator +(string a, FormattedString b)
{
FormattedString result = new FormattedString(new (string raw, Color? fg, Color? bg)[b.values.Length]);
Array.Copy(b.values, result.values, result.values.Length);
result.values[0].raw = a + result.values[0].raw;
return result;
}
public static FormattedString operator +(FormattedString a, FormattedString b) => new FormattedString(a.values.Concat(b.values).ToArray());
}
public interface INamedTimer
{
string Name { get; }
int Value { get; }
void Set(int value);
}
public interface IEditableNamedTimer : INamedTimer
{
void Set(string name, int value);
}
public enum RedrawResult
{
SUCCESS,
WIDTH_CHANGED,
HEIGHT_CHANGED
}
}