-
-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathConsoleTextBox.cs
239 lines (223 loc) · 8.15 KB
/
ConsoleTextBox.cs
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
using System;
using System.Collections.Generic;
namespace CKAN.ConsoleUI.Toolkit {
/// <summary>
/// Object displaying a long screen in a big box
/// </summary>
public class ConsoleTextBox : ScreenObject {
/// <summary>
/// Initialize the text box
/// </summary>
/// <param name="l">X coordinate of left edge</param>
/// <param name="t">Y coordinate of top edge</param>
/// <param name="r">X coordinate of right edge</param>
/// <param name="b">Y coordinate of bottom edge</param>
/// <param name="autoScroll">If true, keep the bottommost row visible, else keep the topmost row visible</param>
/// <param name="ta">Alignment of the contents</param>
/// <param name="bgFunc">Function returning the background color for the text</param>
/// <param name="fgFunc">Function returning the foreground color for the text</param>
public ConsoleTextBox(
int l, int t, int r, int b,
bool autoScroll = true,
TextAlign ta = TextAlign.Left,
Func<ConsoleColor> bgFunc = null,
Func<ConsoleColor> fgFunc = null)
: base(l, t, r, b)
{
scrollToBottom = autoScroll;
align = ta;
getFgColor = fgFunc;
getBgColor = bgFunc;
}
/// <summary>
/// Add a line to the text box
/// </summary>
/// <param name="line">String to add</param>
public void AddLine(string line)
{
lines.AddRange(Formatting.WordWrap(line, GetRight() - GetLeft() + 1));
if (scrollToBottom) {
ScrollToBottom();
} else {
// No auto-scrolling
}
}
/// <summary>
/// Scroll the text box to the top
/// </summary>
public void ScrollToTop()
{
topLine = 0;
}
/// <summary>
/// Scroll the text box to the bottom
/// </summary>
public void ScrollToBottom()
{
int h = GetBottom() - GetTop() + 1;
topLine = lines.Count - h;
}
/// <summary>
/// Scroll the text box up one page
/// </summary>
public void ScrollUp(int? howFar = null)
{
topLine -= howFar ?? (GetBottom() - GetTop() + 1);
if (topLine < 0) {
topLine = 0;
}
}
/// <summary>
/// Scroll the text box down one page
/// </summary>
public void ScrollDown(int? howFar = null)
{
int h = GetBottom() - GetTop() + 1;
int diff = howFar ?? h;
if (topLine + diff <= lines.Count - h) {
topLine += diff;
} else {
ScrollToBottom();
}
}
/// <summary>
/// Draw the text box
/// </summary>
/// <param name="focused">Framework parameter not relevant to this object</param>
public override void Draw(bool focused)
{
int l = GetLeft();
int h = GetBottom() - GetTop() + 1;
int index = lines.Count < h ? 0 : topLine;
// Chop one col off the right if we need a scrollbar
int w = GetRight() - l + 1 + (lines.Count > h ? -1 : 0);
if (getBgColor != null) {
Console.BackgroundColor = getBgColor();
} else {
Console.BackgroundColor = ConsoleTheme.Current.TextBoxBg;
}
if (getFgColor != null) {
Console.ForegroundColor = getFgColor();
} else {
Console.ForegroundColor = ConsoleTheme.Current.TextBoxFg;
}
for (int y = GetTop(); y <= GetBottom(); ++y, ++index) {
Console.SetCursorPosition(l, y);
if (index < lines.Count) {
switch (align) {
case TextAlign.Left:
Console.Write(lines[index].PadRight(w));
break;
case TextAlign.Center:
Console.Write(ScreenObject.PadCenter(lines[index], w));
break;
case TextAlign.Right:
Console.Write(lines[index].PadLeft(w));
break;
}
} else {
Console.Write("".PadRight(w));
}
}
// Scrollbar
if (lines.Count > h) {
DrawScrollbar(
GetRight(), GetTop(), GetBottom(),
GetTop() + 1 + (h - 3) * topLine / (lines.Count - h)
);
}
}
/// <summary>
/// Set up key bindings to scroll the box in a given screen container
/// </summary>
/// <param name="cont">Container within which to create the bindings</param>
/// <param name="drawMore">If true, force a redraw of the text box after scrolling, otherwise rely on the main event loop to do it</param>
public void AddScrollBindings(ScreenContainer cont, bool drawMore = false)
{
if (drawMore) {
cont.AddBinding(Keys.Home, (object sender) => {
ScrollToTop();
Draw(false);
return true;
});
cont.AddBinding(Keys.End, (object sender) => {
ScrollToBottom();
Draw(false);
return true;
});
cont.AddBinding(Keys.PageUp, (object sender) => {
ScrollUp();
Draw(false);
return true;
});
cont.AddBinding(Keys.PageDown, (object sender) => {
ScrollDown();
Draw(false);
return true;
});
cont.AddBinding(Keys.UpArrow, (object sender) => {
ScrollUp(1);
Draw(false);
return true;
});
cont.AddBinding(Keys.DownArrow, (object sender) => {
ScrollDown(1);
Draw(false);
return true;
});
} else {
cont.AddBinding(Keys.Home, (object sender) => {
ScrollToTop();
return true;
});
cont.AddBinding(Keys.End, (object sender) => {
ScrollToBottom();
return true;
});
cont.AddBinding(Keys.PageUp, (object sender) => {
ScrollUp();
return true;
});
cont.AddBinding(Keys.PageDown, (object sender) => {
ScrollDown();
return true;
});
cont.AddBinding(Keys.UpArrow, (object sender) => {
ScrollUp(1);
return true;
});
cont.AddBinding(Keys.DownArrow, (object sender) => {
ScrollDown(1);
return true;
});
}
}
/// <summary>
/// Tell the container we can't receive focus
/// </summary>
public override bool Focusable() { return false; }
private bool scrollToBottom;
private int topLine;
private TextAlign align;
private List<string> lines = new List<string>();
private Func<ConsoleColor> getBgColor;
private Func<ConsoleColor> getFgColor;
}
/// <summary>
/// Alignment of text box
/// </summary>
public enum TextAlign {
/// <summary>
/// Left aligned, padding on right
/// </summary>
Left,
/// <summary>
/// Centered, padding on both left and right
/// </summary>
Center,
/// <summary>
/// Right aligned, padding on left
/// </summary>
Right
}
}