Skip to content

Commit

Permalink
Fix odd REPL issue (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
reubeno authored Mar 23, 2018
1 parent 9ce9fd3 commit 49e964f
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 71 deletions.
4 changes: 2 additions & 2 deletions src/NClap/ConsoleInput/BasicConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ public bool SetCursorPosition(int left, int top)
{
if (left < 0) return false;
if (top < 0) return false;
if (left >= WindowWidth) return false;
if (top >= WindowHeight) return false;
if (left >= BufferWidth) return false;
if (top >= BufferHeight) return false;

try
{
Expand Down
129 changes: 61 additions & 68 deletions src/NClap/ConsoleInput/ConsoleReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
using System;
// #define LOG_KEYS

using System;

#if LOG_KEYS
using System.Collections.Generic;
using System.Text;
using NClap.Utilities;
#endif

namespace NClap.ConsoleInput
{
Expand All @@ -7,6 +15,10 @@ namespace NClap.ConsoleInput
/// </summary>
internal class ConsoleReader : IConsoleReader
{
#if LOG_KEYS
private readonly ConsoleStatusBar _statusBar = new ConsoleStatusBar { Enabled = true };
#endif

private readonly int _defaultCursorSize;

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

#if VERBOSE
#if LOG_KEYS
// Log information about the key press.
LogKey(key);
#endif
Expand Down Expand Up @@ -391,90 +403,71 @@ private void ProcessTabKeyPress(bool previous, bool lastOpWasCompleteToken)
private void UpdateCursorSize() =>
ConsoleOutput.CursorSize = LineInput.InsertMode ? _defaultCursorSize : 100;

#if false
#if LOG_KEYS
private void LogKey(ConsoleKeyInfo key)
{
var x = ConsoleOutput.CursorLeft;
var y = ConsoleOutput.CursorTop;

var fgColor = ConsoleOutput.ForegroundColor;
var bgColor = ConsoleOutput.BackgroundColor;
var builder = new StringBuilder();
builder.AppendFormat("[Key: {{{0, -12}}}] ", key.Key);

try
if (char.IsControl(key.KeyChar))
{
ConsoleOutput.SetCursorPosition(0, 0);
ConsoleOutput.ForegroundColor = ConsoleColor.Yellow;
ConsoleOutput.BackgroundColor = ConsoleColor.DarkBlue;

var builder = new StringBuilder();
builder.AppendFormat("[Key: {{{0, -12}}}] ", key.Key);

if (char.IsControl(key.KeyChar))
{
builder.AppendFormat("[Char: 0x{0:X}] ", (int)key.KeyChar);
}
else if (key.KeyChar != (char)0)
{
builder.AppendFormat("[Char: '{0}' : 0x{1:X}] ", key.KeyChar, (int)key.KeyChar);
}
builder.AppendFormat("[Char: 0x{0:X}] ", (int)key.KeyChar);
}
else if (key.KeyChar != (char)0)
{
builder.AppendFormat("[Char: '{0}' : 0x{1:X}] ", key.KeyChar, (int)key.KeyChar);
}

var modifiers = (ConsoleModifiers)0;
var translationModifiers = (ConsoleModifiers)0;
var modifierNames = new List<string>();
var modifiers = (ConsoleModifiers)0;
var translationModifiers = (ConsoleModifiers)0;
var modifierNames = new List<string>();

if (key.Modifiers.HasFlag(ConsoleModifiers.Control))
{
modifiers |= ConsoleModifiers.Control;
modifierNames.Add("Ctrl");
}
if (key.Modifiers.HasFlag(ConsoleModifiers.Control))
{
modifiers |= ConsoleModifiers.Control;
modifierNames.Add("Ctrl");
}

if (key.Modifiers.HasFlag(ConsoleModifiers.Alt))
{
modifiers |= ConsoleModifiers.Alt;
modifierNames.Add("Alt");
}
if (key.Modifiers.HasFlag(ConsoleModifiers.Alt))
{
modifiers |= ConsoleModifiers.Alt;
modifierNames.Add("Alt");
}

if (key.Modifiers.HasFlag(ConsoleModifiers.Shift))
{
modifiers |= ConsoleModifiers.Shift;
translationModifiers |= ConsoleModifiers.Shift;
}
if (key.Modifiers.HasFlag(ConsoleModifiers.Shift))
{
modifiers |= ConsoleModifiers.Shift;
translationModifiers |= ConsoleModifiers.Shift;
}

var translatedToChars = false;
if (modifiers.HasFlag(ConsoleModifiers.Alt) || modifiers.HasFlag(ConsoleModifiers.Control))
var translatedToChars = false;
if (modifiers.HasFlag(ConsoleModifiers.Alt) || modifiers.HasFlag(ConsoleModifiers.Control))
{
var chars = InputUtilities.GetChars(key.Key, translationModifiers);
if (chars.Length > 0)
{
var chars = InputUtilities.GetChars(key.Key, translationModifiers);
if (chars.Length > 0)
{
var charsAsString = new string(chars);
builder.AppendFormat("[{0}+{1}]", string.Join("+", modifierNames), charsAsString);
translatedToChars = true;
}
var charsAsString = new string(chars);
builder.AppendFormat("[{0}+{1}]", string.Join("+", modifierNames), charsAsString);
translatedToChars = true;
}
}

if (!translatedToChars)
{
if (key.Modifiers.HasFlag(ConsoleModifiers.Shift)) modifierNames.Add("Shift");

if (modifierNames.Count > 0)
{
builder.AppendFormat("[{0}+{1}]", string.Join("+", modifierNames), key.Key);
}
}
if (!translatedToChars)
{
if (key.Modifiers.HasFlag(ConsoleModifiers.Shift)) modifierNames.Add("Shift");

if (builder.Length < ConsoleOutput.BufferWidth)
if (modifierNames.Count > 0)
{
builder.Append(new string(' ', ConsoleOutput.BufferWidth - builder.Length));
builder.AppendFormat("[{0}+{1}]", string.Join("+", modifierNames), key.Key);
}

ConsoleOutput.Write(builder.ToString());
}
finally

if (builder.Length < ConsoleOutput.BufferWidth)
{
ConsoleOutput.ForegroundColor = fgColor;
ConsoleOutput.BackgroundColor = bgColor;
ConsoleOutput.SetCursorPosition(x, y);
builder.Append(new string(' ', ConsoleOutput.BufferWidth - builder.Length));
}

_statusBar.Set(builder.ToString());
}
#endif
}
Expand Down
98 changes: 98 additions & 0 deletions src/NClap/ConsoleInput/ConsoleStatusBar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Text;

namespace NClap.ConsoleInput
{
/// <summary>
/// Console status bar, used only for internal testing.
/// </summary>
internal class ConsoleStatusBar
{
private const ConsoleColor ForegroundColor = ConsoleColor.Yellow;
private const ConsoleColor BackgroundColor = ConsoleColor.Magenta;

private readonly StringBuilder _contents = new StringBuilder();

/// <summary>
/// Constructor.
/// </summary>
public ConsoleStatusBar()
{
Reset();
}

/// <summary>
/// Append the given value to the current contents.
/// </summary>
/// <param name="value">Value to append.</param>
public void Append(string value)
{
_contents.Append(value);
Update();
}

/// <summary>
/// Stores the given value in the bar.
/// </summary>
/// <param name="value">Value to store.</param>
public void Set(string value)
{
_contents.Clear();
Append(value);
}

/// <summary>
/// Resets the contents of the status bar.
/// </summary>
public void Reset()
{
_contents.Clear();
Update();
}

/// <summary>
/// Whether or not the status bar is enabled.
/// </summary>
public bool Enabled { get; set; }

private void Update()
{
if (!Enabled)
{
return;
}

Write(_contents.ToString());
}

private static void Write(string value)
{
var console = BasicConsole.Default;

var x = console.CursorLeft;
var y = console.CursorTop;
var fgColor = console.ForegroundColor;
var bgColor = console.BackgroundColor;

try
{
console.SetCursorPosition(0, 0);
console.ForegroundColor = ForegroundColor;
console.BackgroundColor = BackgroundColor;

if (value.Length < console.BufferWidth)
{
value += new string(' ', console.BufferWidth - value.Length);
}

console.Write(value);
}
finally
{
console.ForegroundColor = fgColor;
console.BackgroundColor = bgColor;
console.SetCursorPosition(x, y);
}
}
}
}
3 changes: 2 additions & 1 deletion src/Tests/TestApp/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"profiles": {
"NClap.TestApp": {
"commandName": "Project"
"commandName": "Project",
"commandLineArgs": "repl"
}
}
}

0 comments on commit 49e964f

Please sign in to comment.