Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions Terminal.Gui/Core/ConsoleDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -681,11 +681,13 @@ public abstract class ConsoleDriver {
/// <param name="col">Column to move the cursor to.</param>
/// <param name="row">Row to move the cursor to.</param>
public abstract void Move (int col, int row);

/// <summary>
/// Adds the specified rune to the display at the current cursor position
/// </summary>
/// <param name="rune">Rune to add.</param>
public abstract void AddRune (Rune rune);

/// <summary>
/// Ensures a Rune is not a control character and can be displayed by translating characters below 0x20
/// to equivalent, printable, Unicode chars.
Expand All @@ -694,21 +696,14 @@ public abstract class ConsoleDriver {
/// <returns></returns>
public static Rune MakePrintable (Rune c)
{
var controlChars = gethexaformat (c, 4);
if (controlChars <= 0x1F || (controlChars >= 0X7F && controlChars <= 0x9F)) {
var controlChars = c & 0xFFFF;
if (controlChars <= 0x1F || controlChars >= 0X7F && controlChars <= 0x9F) {
// ASCII (C0) control characters.
// C1 control characters (https://www.aivosto.com/articles/control-characters.html#c1)
return new Rune (controlChars + 0x2400);
} else {
return c;
}
}

static uint gethexaformat (uint rune, int length)
{
var hex = rune.ToString ($"x{length}");
var hexstr = hex.Substring (hex.Length - length, length);
return (uint)int.Parse (hexstr, System.Globalization.NumberStyles.HexNumber);
return c;
}

/// <summary>
Expand Down
24 changes: 24 additions & 0 deletions UnitTests/ConsoleDriverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,5 +608,29 @@ public void Write_Do_Not_Change_On_ProcessKey ()
Application.Run (win);
Application.Shutdown ();
}

[Theory]
[InlineData(0x0000001F, 0x241F)]
[InlineData(0x0000007F, 0x247F)]
[InlineData(0x0000009F, 0x249F)]
[InlineData(0x0001001A, 0x241A)]
public void MakePrintable_Converts_Control_Chars_To_Proper_Unicode (uint code, uint expected)
{
var actual = ConsoleDriver.MakePrintable(code);

Assert.Equal (expected, actual.Value);
}

[Theory]
[InlineData(0x20)]
[InlineData(0x7E)]
[InlineData(0xA0)]
[InlineData(0x010020)]
public void MakePrintable_Does_Not_Convert_Ansi_Chars_To_Unicode (uint code)
{
var actual = ConsoleDriver.MakePrintable(code);

Assert.Equal (code, actual.Value);
}
}
}