-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFooter.cs
More file actions
332 lines (296 loc) · 11.9 KB
/
Footer.cs
File metadata and controls
332 lines (296 loc) · 11.9 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
namespace maple
{
public abstract class FooterBlock
{
[XmlIgnore]
public short ColorAttribute { get; private set; } = Printer.ConsoleColorToAttributeTable[ConsoleColor.Gray];
private string _color = "Gray";
public string Color
{
get { return _color; }
set
{
_color = value;
ColorAttribute = Printer.ConsoleColorToAttributeTable[Settings.StringToConsoleColor(value)];
}
}
private string _backgroundColor = "Black";
public string BackgroundColor
{
get { return _backgroundColor; }
set
{
_backgroundColor = value;
ColorAttribute = Printer.GetAttributeFromColor(
Settings.StringToConsoleColor(_color),
Settings.StringToConsoleColor(_backgroundColor)
);
}
}
public abstract string GetText();
}
public class FooterBlockText : FooterBlock
{
public string Content { get; set; } = "";
public override string GetText()
{
return Content;
}
}
public class FooterBlockSeparator : FooterBlock
{
public override string GetText()
{
return " ";
}
}
public class FooterBlockFilepath : FooterBlock
{
public override string GetText()
{
return Editor.CurrentDoc.Filepath.Trim();
}
}
public class FooterBlockFilename : FooterBlock
{
public override string GetText()
{
return System.IO.Path.GetFileName(Editor.CurrentDoc.Filepath.Trim());
}
}
public class FooterBlockLnCol : FooterBlock
{
public override string GetText()
{
return String.Format("ln {0} col {1}", Editor.DocCursor.DY + 1, Editor.DocCursor.DX + 1);
}
}
public class FooterBlockSelection : FooterBlock
{
public override string GetText()
{
if (Editor.CurrentDoc.HasSelection)
{
return String.Format("{0},{1} - {2},{3}",
Editor.CurrentDoc.SelectInY + 1,
Editor.CurrentDoc.SelectInX + 1,
Editor.CurrentDoc.SelectOutY + 1,
Editor.CurrentDoc.SelectOutX + 1);
}
else if (Editor.CurrentDoc.HasSelectionStart)
{
return String.Format("{0},{1} …",
Editor.CurrentDoc.SelectInY + 1,
Editor.CurrentDoc.SelectInX + 1);
}
return "";
}
}
public class FooterBlockShortcut : FooterBlock
{
public string Key { get; set; }
public override string GetText()
{
string output = String.Format("[{0}] ", Key);
if (Settings.Properties.ShortcutsTable.ContainsKey(Settings.StringToConsoleKeyTable[Key]))
{
output += Settings.Properties.ShortcutsTable[Settings.StringToConsoleKeyTable[Key]].Command.Trim();
}
else
{
output += "None";
}
return output;
}
}
public class FooterBlockReadOnlyIndicator : FooterBlock
{
public string TrueValue { get; set; } = "[readonly]";
public string FalseValue { get; set; } = "";
public override string GetText()
{
return (Input.ReadOnly) ? TrueValue : FalseValue;
}
}
public class FooterBlockDirtyIndicator : FooterBlock
{
public string TrueValue { get; set; } = "*";
public string FalseValue { get; set; } = "";
public override string GetText()
{
return Editor.CurrentDoc.Dirty ? TrueValue : FalseValue;
}
}
public class FooterLayout
{
public string SeparatorText { get; set; } = " ";
[XmlArrayItem(ElementName = "Text", Type = typeof(FooterBlockText)),
XmlArrayItem(ElementName = "Separator", Type = typeof(FooterBlockSeparator)),
XmlArrayItem(ElementName = "Filepath", Type = typeof(FooterBlockFilepath)),
XmlArrayItem(ElementName = "Filename", Type = typeof(FooterBlockFilename)),
XmlArrayItem(ElementName = "LnCol", Type = typeof(FooterBlockLnCol)),
XmlArrayItem(ElementName = "Selection", Type = typeof(FooterBlockSelection)),
XmlArrayItem(ElementName = "Shortcut", Type = typeof(FooterBlockShortcut)),
XmlArrayItem(ElementName = "ReadOnlyIndicator", Type = typeof(FooterBlockReadOnlyIndicator)),
XmlArrayItem(ElementName = "DirtyIndicator", Type = typeof(FooterBlockDirtyIndicator))
]
public List<FooterBlock> BlockGroup { get; set; } = new();
}
static class Footer
{
public static int FooterHeight { get; private set; }= 1;
static bool refreshOutputNext = false;
public static string CommandPromptText { get; } = "maple $ ";
static FooterLayout layout = new FooterLayout();
public static void LoadFooterLayout()
{
FooterLayout loaded = Settings.ReadSettingsFile<FooterLayout>(Settings.Properties.FooterLayoutFile);
if (loaded != null)
{
layout = loaded;
Log.Write("Loaded " + layout.BlockGroup.Count + " footer layout blocks", "footer");
}
else
{
// null layout, load default block group
layout.BlockGroup = new() {
new FooterBlockText() { Content = "maple", Color = "Yellow"},
new FooterBlockSeparator(),
new FooterBlockFilepath() { Color = "Gray" },
new FooterBlockSeparator(),
new FooterBlockLnCol() { Color = "Yellow" },
new FooterBlockSeparator(),
new FooterBlockSelection() { Color = "Blue" },
new FooterBlockSeparator(),
new FooterBlockReadOnlyIndicator() { Color = "DarkGray" },
};
Log.Write("Failed to load footer layout", "footer", important: true);
Log.Write("Loaded " + layout.BlockGroup.Count + " default footer layout blocks", "footer");
}
}
/// <summary>
/// Print the footer (prints command line input if user is accessing cli).
/// </summary>
public static void PrintFooter()
{
Printer.ClearLine(Printer.MaxScreenY);
// DRAW DOCUMENT FOOTER
if (Input.CurrentTarget == Input.InputTarget.Document)
{
// Get content from footer tokens
Printer.MoveCursor(0, Printer.MaxScreenY); // to manually set initial X
for (int i = 0; i < layout.BlockGroup.Count; i++)
{
FooterBlock b = layout.BlockGroup[i];
string text = b.GetText();
// separators are special, check if they should display
if (b.GetType() == typeof(FooterBlockSeparator))
{
// never render a separator if it is first or last
if (i == 0 || i == layout.BlockGroup.Count - 1)
{
text = "";
}
else
{
// search backwards and only render if there is a non-empty block
// stop searching if a separator is seen
int lookI = i - 1;
text = "";
while (lookI >= 0)
{
if (layout.BlockGroup[lookI].GetType() == typeof(FooterBlockSeparator))
break;
if (layout.BlockGroup[lookI].GetText().Length != 0)
{
text = b.GetText();
break;
}
lookI--;
}
}
}
Printer.WriteToFooter(text, attribute: b.ColorAttribute);
}
Printer.ClearRight();
}
// DRAW COMMAND FOOTER
else if (Input.CurrentTarget == Input.InputTarget.Command)
{
Printer.WriteToFooter(CommandPromptText, x: 0, foregroundColor: Settings.Theme.AccentColor);
if (Settings.Properties.CliNoHighlight || (CommandLine.HasOutput && CommandLine.OPrompt != null))
{
Printer.WriteToFooter(CommandLine.InputText, x: CommandPromptText.Length, Settings.Theme.CliInputDefaultColor);
}
else
{
List<Token> cliTokens = Lexer.TokenizeCommandLine(CommandLine.InputText);
Printer.MoveCursor(CommandPromptText.Length, Printer.MaxScreenY);
for (int i = 0; i < cliTokens.Count; i++)
{
Printer.WriteToFooter(cliTokens[i].Text, foregroundColor: cliTokens[i].Color);
}
}
}
if (refreshOutputNext)
{
PrintOutputLine();
refreshOutputNext = false;
}
}
public static void RefreshOutputLine()
{
FooterHeight = (CommandLine.HasOutput) ? 2 : 1;
refreshOutputNext = true;
}
public static void PrintOutputLine()
{
// Draw output text
if (CommandLine.HasOutput)
{
Printer.ClearLine(Printer.MaxScreenY - 1);
ConsoleColor outputColor = Settings.Theme.CliOutputInfoColor;
switch (CommandLine.OType)
{
case OutputType.Error:
outputColor = Settings.Theme.CliOutputErrorColor;
break;
case OutputType.Success:
outputColor = Settings.Theme.CliOutputSuccessColor;
break;
case OutputType.Prompt:
outputColor = Settings.Theme.CliOutputPromptColor;
break;
}
// build prompt string
string outputText = CommandLine.OutputText;
if (CommandLine.OType == OutputType.Prompt &&
CommandLine.OPrompt.InstantActionTable != null &&
CommandLine.OPrompt.InstantActionTable.Keys.Count > 0
)
{
outputText += " [";
foreach (ConsoleKey k in CommandLine.OPrompt.InstantActionTable.Keys)
{
// capitalize default, lowercase otherwise
outputText += ((k == CommandLine.OPrompt.DefaultInstantAction) ? k.ToString().ToUpper() : k.ToString().ToLower()) + " / ";
}
outputText = outputText.Remove(outputText.Length - 3);
outputText += "]";
}
if (Settings.Properties.ColorOutputBackground)
{
Printer.DrawFooter(outputText, foregroundColor: ConsoleColor.Black, backgroundColor: outputColor, yOffset: 1);
}
else
{
Printer.DrawFooter(outputText, foregroundColor: outputColor, backgroundColor: ConsoleColor.Black, yOffset: 1);
}
}
}
}
}