Skip to content

Commit 49e964f

Browse files
authored
Fix odd REPL issue (#59)
1 parent 9ce9fd3 commit 49e964f

File tree

4 files changed

+163
-71
lines changed

4 files changed

+163
-71
lines changed

src/NClap/ConsoleInput/BasicConsole.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ public bool SetCursorPosition(int left, int top)
225225
{
226226
if (left < 0) return false;
227227
if (top < 0) return false;
228-
if (left >= WindowWidth) return false;
229-
if (top >= WindowHeight) return false;
228+
if (left >= BufferWidth) return false;
229+
if (top >= BufferHeight) return false;
230230

231231
try
232232
{

src/NClap/ConsoleInput/ConsoleReader.cs

Lines changed: 61 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
using System;
1+
// #define LOG_KEYS
2+
3+
using System;
4+
5+
#if LOG_KEYS
6+
using System.Collections.Generic;
7+
using System.Text;
8+
using NClap.Utilities;
9+
#endif
210

311
namespace NClap.ConsoleInput
412
{
@@ -7,6 +15,10 @@ namespace NClap.ConsoleInput
715
/// </summary>
816
internal class ConsoleReader : IConsoleReader
917
{
18+
#if LOG_KEYS
19+
private readonly ConsoleStatusBar _statusBar = new ConsoleStatusBar { Enabled = true };
20+
#endif
21+
1022
private readonly int _defaultCursorSize;
1123

1224
// Operation history
@@ -83,7 +95,7 @@ public string ReadLine()
8395
// Grab a new key press event.
8496
var key = ConsoleInput.ReadKey(true);
8597

86-
#if VERBOSE
98+
#if LOG_KEYS
8799
// Log information about the key press.
88100
LogKey(key);
89101
#endif
@@ -391,90 +403,71 @@ private void ProcessTabKeyPress(bool previous, bool lastOpWasCompleteToken)
391403
private void UpdateCursorSize() =>
392404
ConsoleOutput.CursorSize = LineInput.InsertMode ? _defaultCursorSize : 100;
393405

394-
#if false
406+
#if LOG_KEYS
395407
private void LogKey(ConsoleKeyInfo key)
396408
{
397-
var x = ConsoleOutput.CursorLeft;
398-
var y = ConsoleOutput.CursorTop;
399-
400-
var fgColor = ConsoleOutput.ForegroundColor;
401-
var bgColor = ConsoleOutput.BackgroundColor;
409+
var builder = new StringBuilder();
410+
builder.AppendFormat("[Key: {{{0, -12}}}] ", key.Key);
402411

403-
try
412+
if (char.IsControl(key.KeyChar))
404413
{
405-
ConsoleOutput.SetCursorPosition(0, 0);
406-
ConsoleOutput.ForegroundColor = ConsoleColor.Yellow;
407-
ConsoleOutput.BackgroundColor = ConsoleColor.DarkBlue;
408-
409-
var builder = new StringBuilder();
410-
builder.AppendFormat("[Key: {{{0, -12}}}] ", key.Key);
411-
412-
if (char.IsControl(key.KeyChar))
413-
{
414-
builder.AppendFormat("[Char: 0x{0:X}] ", (int)key.KeyChar);
415-
}
416-
else if (key.KeyChar != (char)0)
417-
{
418-
builder.AppendFormat("[Char: '{0}' : 0x{1:X}] ", key.KeyChar, (int)key.KeyChar);
419-
}
414+
builder.AppendFormat("[Char: 0x{0:X}] ", (int)key.KeyChar);
415+
}
416+
else if (key.KeyChar != (char)0)
417+
{
418+
builder.AppendFormat("[Char: '{0}' : 0x{1:X}] ", key.KeyChar, (int)key.KeyChar);
419+
}
420420

421-
var modifiers = (ConsoleModifiers)0;
422-
var translationModifiers = (ConsoleModifiers)0;
423-
var modifierNames = new List<string>();
421+
var modifiers = (ConsoleModifiers)0;
422+
var translationModifiers = (ConsoleModifiers)0;
423+
var modifierNames = new List<string>();
424424

425-
if (key.Modifiers.HasFlag(ConsoleModifiers.Control))
426-
{
427-
modifiers |= ConsoleModifiers.Control;
428-
modifierNames.Add("Ctrl");
429-
}
425+
if (key.Modifiers.HasFlag(ConsoleModifiers.Control))
426+
{
427+
modifiers |= ConsoleModifiers.Control;
428+
modifierNames.Add("Ctrl");
429+
}
430430

431-
if (key.Modifiers.HasFlag(ConsoleModifiers.Alt))
432-
{
433-
modifiers |= ConsoleModifiers.Alt;
434-
modifierNames.Add("Alt");
435-
}
431+
if (key.Modifiers.HasFlag(ConsoleModifiers.Alt))
432+
{
433+
modifiers |= ConsoleModifiers.Alt;
434+
modifierNames.Add("Alt");
435+
}
436436

437-
if (key.Modifiers.HasFlag(ConsoleModifiers.Shift))
438-
{
439-
modifiers |= ConsoleModifiers.Shift;
440-
translationModifiers |= ConsoleModifiers.Shift;
441-
}
437+
if (key.Modifiers.HasFlag(ConsoleModifiers.Shift))
438+
{
439+
modifiers |= ConsoleModifiers.Shift;
440+
translationModifiers |= ConsoleModifiers.Shift;
441+
}
442442

443-
var translatedToChars = false;
444-
if (modifiers.HasFlag(ConsoleModifiers.Alt) || modifiers.HasFlag(ConsoleModifiers.Control))
443+
var translatedToChars = false;
444+
if (modifiers.HasFlag(ConsoleModifiers.Alt) || modifiers.HasFlag(ConsoleModifiers.Control))
445+
{
446+
var chars = InputUtilities.GetChars(key.Key, translationModifiers);
447+
if (chars.Length > 0)
445448
{
446-
var chars = InputUtilities.GetChars(key.Key, translationModifiers);
447-
if (chars.Length > 0)
448-
{
449-
var charsAsString = new string(chars);
450-
builder.AppendFormat("[{0}+{1}]", string.Join("+", modifierNames), charsAsString);
451-
translatedToChars = true;
452-
}
449+
var charsAsString = new string(chars);
450+
builder.AppendFormat("[{0}+{1}]", string.Join("+", modifierNames), charsAsString);
451+
translatedToChars = true;
453452
}
453+
}
454454

455-
if (!translatedToChars)
456-
{
457-
if (key.Modifiers.HasFlag(ConsoleModifiers.Shift)) modifierNames.Add("Shift");
458-
459-
if (modifierNames.Count > 0)
460-
{
461-
builder.AppendFormat("[{0}+{1}]", string.Join("+", modifierNames), key.Key);
462-
}
463-
}
455+
if (!translatedToChars)
456+
{
457+
if (key.Modifiers.HasFlag(ConsoleModifiers.Shift)) modifierNames.Add("Shift");
464458

465-
if (builder.Length < ConsoleOutput.BufferWidth)
459+
if (modifierNames.Count > 0)
466460
{
467-
builder.Append(new string(' ', ConsoleOutput.BufferWidth - builder.Length));
461+
builder.AppendFormat("[{0}+{1}]", string.Join("+", modifierNames), key.Key);
468462
}
469-
470-
ConsoleOutput.Write(builder.ToString());
471463
}
472-
finally
464+
465+
if (builder.Length < ConsoleOutput.BufferWidth)
473466
{
474-
ConsoleOutput.ForegroundColor = fgColor;
475-
ConsoleOutput.BackgroundColor = bgColor;
476-
ConsoleOutput.SetCursorPosition(x, y);
467+
builder.Append(new string(' ', ConsoleOutput.BufferWidth - builder.Length));
477468
}
469+
470+
_statusBar.Set(builder.ToString());
478471
}
479472
#endif
480473
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace NClap.ConsoleInput
5+
{
6+
/// <summary>
7+
/// Console status bar, used only for internal testing.
8+
/// </summary>
9+
internal class ConsoleStatusBar
10+
{
11+
private const ConsoleColor ForegroundColor = ConsoleColor.Yellow;
12+
private const ConsoleColor BackgroundColor = ConsoleColor.Magenta;
13+
14+
private readonly StringBuilder _contents = new StringBuilder();
15+
16+
/// <summary>
17+
/// Constructor.
18+
/// </summary>
19+
public ConsoleStatusBar()
20+
{
21+
Reset();
22+
}
23+
24+
/// <summary>
25+
/// Append the given value to the current contents.
26+
/// </summary>
27+
/// <param name="value">Value to append.</param>
28+
public void Append(string value)
29+
{
30+
_contents.Append(value);
31+
Update();
32+
}
33+
34+
/// <summary>
35+
/// Stores the given value in the bar.
36+
/// </summary>
37+
/// <param name="value">Value to store.</param>
38+
public void Set(string value)
39+
{
40+
_contents.Clear();
41+
Append(value);
42+
}
43+
44+
/// <summary>
45+
/// Resets the contents of the status bar.
46+
/// </summary>
47+
public void Reset()
48+
{
49+
_contents.Clear();
50+
Update();
51+
}
52+
53+
/// <summary>
54+
/// Whether or not the status bar is enabled.
55+
/// </summary>
56+
public bool Enabled { get; set; }
57+
58+
private void Update()
59+
{
60+
if (!Enabled)
61+
{
62+
return;
63+
}
64+
65+
Write(_contents.ToString());
66+
}
67+
68+
private static void Write(string value)
69+
{
70+
var console = BasicConsole.Default;
71+
72+
var x = console.CursorLeft;
73+
var y = console.CursorTop;
74+
var fgColor = console.ForegroundColor;
75+
var bgColor = console.BackgroundColor;
76+
77+
try
78+
{
79+
console.SetCursorPosition(0, 0);
80+
console.ForegroundColor = ForegroundColor;
81+
console.BackgroundColor = BackgroundColor;
82+
83+
if (value.Length < console.BufferWidth)
84+
{
85+
value += new string(' ', console.BufferWidth - value.Length);
86+
}
87+
88+
console.Write(value);
89+
}
90+
finally
91+
{
92+
console.ForegroundColor = fgColor;
93+
console.BackgroundColor = bgColor;
94+
console.SetCursorPosition(x, y);
95+
}
96+
}
97+
}
98+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"profiles": {
33
"NClap.TestApp": {
4-
"commandName": "Project"
4+
"commandName": "Project",
5+
"commandLineArgs": "repl"
56
}
67
}
78
}

0 commit comments

Comments
 (0)