-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatusLine.cs
205 lines (170 loc) · 5.41 KB
/
StatusLine.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
using System;
using Chroma.Graphics;
using Chroma.Input;
using Chroma.SabreVGA.TextEditor.DataModel;
namespace Chroma.SabreVGA.TextEditor
{
public class StatusLine
{
private readonly int _messageVisibilitySeconds = 3;
private DateTime? _messageShownAt;
private string _message;
private CodeEditor Owner { get; }
private Action<string> ReadLineCallback { get; set; }
public bool TakingInput { get; private set; }
public Line Input { get; private set; }
public string Prompt { get; private set; }
public string Text { get; set; }
public int CaretIndex => Input.CaretIndex;
public StatusLine(CodeEditor owner)
{
Owner = owner;
}
public void ReadLine(string prompt, Action<string> callback)
{
if (callback == null)
{
throw new ArgumentNullException(
nameof(callback),
"callback cannot be null"
);
}
if (TakingInput)
return;
Prompt = prompt;
Input = new Line();
ReadLineCallback = callback;
Owner.Screen.Cursor.AllowMovementOutOfWindow = true;
TakingInput = true;
}
public void EndInput(bool accept)
{
Owner.Screen.Cursor.AllowMovementOutOfWindow = false;
TakingInput = false;
if (accept)
ReadLineCallback(Input.Text);
Input = null;
ReadLineCallback = null;
}
public void Render()
{
if (TakingInput)
{
Text = $"{Prompt}{Input.Text}";
}
else if (!string.IsNullOrEmpty(_message))
{
Text = _message;
if (_messageShownAt != null
&& (DateTime.Now - _messageShownAt)?.TotalSeconds > _messageVisibilitySeconds)
{
ClearMessage();
}
}
else if (Owner.CurrentBuffer == null)
{
Text = "no buffers opened";
}
else
{
if (Owner.CurrentBuffer.Dirty
|| Owner.FileExistsFunc != null
&& !Owner.FileExistsFunc(Owner.CurrentBuffer.FilePath))
{
Text = " * | ";
}
else
{
Text = " - | ";
}
if (string.IsNullOrEmpty(Owner.CurrentBuffer.FilePath))
Text += "<untitled>";
else
Text += Owner.CurrentBuffer.FilePath;
Text +=
$" | Ln {Owner.CurrentBuffer.CurrentLineIndex + 1} : Col {Owner.CurrentBuffer.CurrentLine.CaretIndex + 1}";
if (!Owner.CurrentBuffer.Selection.IsNone)
{
var abs = Owner.CurrentBuffer.Selection.ToAbsolute();
Text += $" | SEL {abs.StartLine}:{abs.StartColumn} -> {abs.EndLine}:{abs.EndColumn}";
}
}
Text = Text.PadLeft(1, ' ');
Text = Text.PadRight(Owner.Screen.TotalColumns, ' ');
PutString(
Text,
Owner.Screen.Margins.Left,
Owner.Screen.WindowRows - 1,
Color.Black,
Owner.Screen.ActiveForegroundColor
);
}
public void KeyPressed(KeyEventArgs e)
{
if (!TakingInput)
return;
switch (e.KeyCode)
{
case KeyCode.Escape:
EndInput(false);
break;
case KeyCode.Return:
EndInput(true);
break;
case KeyCode.Left:
Input.Left();
break;
case KeyCode.Right:
Input.Right();
break;
case KeyCode.Up:
case KeyCode.Home:
Input.Home();
break;
case KeyCode.Down:
case KeyCode.End:
Input.End();
break;
case KeyCode.Backspace:
Input.Backspace();
break;
case KeyCode.Delete:
Input.Delete();
break;
}
ClearMessage();
}
public void SetMessage(string message)
{
_message = message;
_messageShownAt = DateTime.Now;
}
public void TextInput(char c)
{
if (!TakingInput)
return;
Input.InsertAtCaret(c);
}
private void ClearMessage()
{
_message = string.Empty;
_messageShownAt = null;
}
private void PutString(string s, int x, int y, Color fg, Color bg)
{
for (var i = 0; i < s.Length; i++)
{
if (x + i > Owner.Screen.WindowColumns)
break;
Owner.Screen.PutCharAt(
x + i,
y,
s[i],
fg,
bg,
false
);
}
}
}
}