Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #219. Adds Underline and Blink #1275

Closed
wants to merge 7 commits into from
Closed
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
24 changes: 19 additions & 5 deletions Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,28 @@ public override void End ()
//Console.Out.Flush ();

//Set cursor key to cursor.
Console.Out.Write("\x1b[?1l");
Console.Out.Flush();
Console.Out.Write ("\x1b[?1l");
Console.Out.Flush ();
}

public override void UpdateScreen () => window.redrawwin ();

int currentAttribute;
bool currentUnderlineAttribute;

static int A_UNDERLINE = 0x20000;

public override void SetAttribute (Attribute c)
{
currentAttribute = c.Value;
currentUnderlineAttribute = c.UnderLine;
Curses.attrset (currentAttribute);

if (c.UnderLine) {
Curses.attron (A_UNDERLINE);
} else {
Curses.attroff (A_UNDERLINE);
}
}

public Curses.Window window;
Expand Down Expand Up @@ -865,10 +875,12 @@ static int MapColor (Color color)
throw new ArgumentException ("Invalid color code");
}

public override Attribute MakeAttribute (Color fore, Color back)
public override Attribute MakeAttribute (Color fore, Color back, bool underline = false)
{
var f = MapColor (fore);
return MakeColor ((short)(f & 0xffff), (short)MapColor (back)) | ((f & Curses.A_BOLD) != 0 ? Curses.A_BOLD : 0);
Attribute attr = MakeColor ((short)(f & 0xffff), (short)MapColor (back)) | ((f & Curses.A_BOLD) != 0 ? Curses.A_BOLD : 0);
attr.UnderLine = underline;
return attr;
}

public override void Suspend ()
Expand Down Expand Up @@ -913,7 +925,9 @@ public override void CookMouse ()

public override Attribute GetAttribute ()
{
return currentAttribute;
Attribute attr = currentAttribute;
attr.UnderLine = currentUnderlineAttribute;
return attr;
}

/// <inheritdoc/>
Expand Down
6 changes: 4 additions & 2 deletions Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ public override void Init (Action terminalResized)
//MockConsole.Clear ();
}

public override Attribute MakeAttribute (Color fore, Color back)
public override Attribute MakeAttribute (Color fore, Color back, bool underline = false)
{
return MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
var attr = MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
attr.UnderLine = underline;
return attr;
}

int redrawColor = -1;
Expand Down
75 changes: 67 additions & 8 deletions Terminal.Gui/ConsoleDrivers/NetDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void Cleanup ()
}

const int STD_INPUT_HANDLE = -10;
const int STD_OUTPUT_HANDLE = -11;
internal const int STD_OUTPUT_HANDLE = -11;
const int STD_ERROR_HANDLE = -12;

// Input modes.
Expand All @@ -93,7 +93,7 @@ public void Cleanup ()
const uint ENABLE_LVB_GRID_WORLDWIDE = 10;

[DllImport ("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle (int nStdHandle);
internal static extern IntPtr GetStdHandle (int nStdHandle);

[DllImport ("kernel32.dll")]
static extern bool GetConsoleMode (IntPtr hConsoleHandle, out uint lpMode);
Expand All @@ -103,6 +103,33 @@ public void Cleanup ()

[DllImport ("kernel32.dll")]
static extern uint GetLastError ();

[DllImport ("kernel32.dll")]
internal static extern bool SetConsoleTextAttribute (IntPtr hConsoleOutput, ushort attributes);

[DllImport ("kernel32.dll")]
internal static extern bool GetConsoleScreenBufferInfo (IntPtr hConsoleOutput, out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
}

struct COORD {
internal short X;
internal short Y;
}

struct SMALL_RECT {
internal short Left;
internal short Top;
internal short Right;
internal short Bottom;
}

[StructLayoutAttribute (LayoutKind.Sequential)]
struct CONSOLE_SCREEN_BUFFER_INFO {
internal COORD dwSize;
internal COORD dwCursorPosition;
internal ushort wAttributes;
internal SMALL_RECT srWindow;
internal COORD dwMaximumWindowSize;
}

internal class NetEvents {
Expand Down Expand Up @@ -1111,7 +1138,7 @@ public override void Move (int col, int row)

public override void AddRune (Rune rune)
{
if (contents.Length != Rows * Cols * 3) {
if (contents.Length != Rows * Cols * 4) {
return;
}
rune = MakePrintable (rune);
Expand All @@ -1120,6 +1147,7 @@ public override void AddRune (Rune rune)
contents [crow, ccol, 0] = (int)(uint)rune;
contents [crow, ccol, 1] = currentAttribute;
contents [crow, ccol, 2] = 1;
contents [crow, ccol, 3] = currentUnderlineAttribute;
dirtyLine [crow] = true;

ccol++;
Expand Down Expand Up @@ -1281,7 +1309,7 @@ void ResizeScreen ()

Clip = new Rect (0, 0, Cols, Rows);

contents = new int [Rows, Cols, 3];
contents = new int [Rows, Cols, 4];
dirtyLine = new bool [Rows];
}

Expand All @@ -1294,6 +1322,7 @@ void UpdateOffScreen ()
contents [row, c, 0] = ' ';
contents [row, c, 1] = (ushort)Colors.TopLevel.Normal;
contents [row, c, 2] = 0;
contents [row, c, 3] = 0; // underline
dirtyLine [row] = true;
}
}
Expand All @@ -1302,9 +1331,11 @@ void UpdateOffScreen ()
winChanging = false;
}

public override Attribute MakeAttribute (Color fore, Color back)
public override Attribute MakeAttribute (Color fore, Color back, bool underline = false)
{
return MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
var attr = MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
attr.UnderLine = underline;
return attr;
}

int redrawColor = -1;
Expand All @@ -1316,20 +1347,39 @@ void SetColor (int color)
.Select (s => (int)s);
if (values.Contains (color & 0xffff)) {
Console.BackgroundColor = (ConsoleColor)(color & 0xffff);

}
if (values.Contains ((color >> 16) & 0xffff)) {
Console.ForegroundColor = (ConsoleColor)((color >> 16) & 0xffff);
}
}

bool redrawUnderline = false;
void SetUnderline (bool state)
{
redrawUnderline = state;
if (IsWinPlatform == true) {
var OutputHandle = NetWinVTConsole.GetStdHandle (NetWinVTConsole.STD_OUTPUT_HANDLE);
var success = NetWinVTConsole.GetConsoleScreenBufferInfo (OutputHandle, out var csbi);
if (success) {
if (state == true) {
csbi.wAttributes |= 0x8000;
} else {
csbi.wAttributes &= 0x7FFF;
}
NetWinVTConsole.SetConsoleTextAttribute (OutputHandle, csbi.wAttributes);
}
}
}

public override void Refresh ()
{
UpdateScreen ();
}

public override void UpdateScreen ()
{
if (winChanging || Console.WindowHeight == 0 || contents.Length != Rows * Cols * 3
if (winChanging || Console.WindowHeight == 0 || contents.Length != Rows * Cols * 4
|| (!HeightAsBuffer && Rows != Console.WindowHeight)
|| (HeightAsBuffer && Rows != largestWindowHeight)) {
return;
Expand Down Expand Up @@ -1357,6 +1407,10 @@ public override void UpdateScreen ()
if (color != redrawColor) {
SetColor (color);
}
var underline = contents [row, col, 3] == 1 ? true : false;
if (underline != redrawUnderline) {
SetUnderline (underline);
}
if (AlwaysSetPosition && !SetCursorPosition (col, row)) {
return;
}
Expand Down Expand Up @@ -1409,9 +1463,12 @@ public override void Suspend ()
}

int currentAttribute;
int currentUnderlineAttribute;

public override void SetAttribute (Attribute c)
{
currentAttribute = c.Value;
currentUnderlineAttribute = c.UnderLine ? 1 : 0;
}

Key MapKey (ConsoleKeyInfo keyInfo)
Expand Down Expand Up @@ -1705,7 +1762,9 @@ MouseEvent ToDriverMouse (NetEvents.MouseEvent me)

public override Attribute GetAttribute ()
{
return currentAttribute;
Attribute attr = currentAttribute;
attr.UnderLine = currentUnderlineAttribute == 1 ? true : false;
return attr;
}

#region Unused
Expand Down
17 changes: 13 additions & 4 deletions Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ public uint ConsoleMode {

public bool HeightAsBuffer { get; set; }

/// <summary>
/// Underscore - Character Attributes
/// </summary>
public const ushort COMMON_LVB_UNDERSCORE = 0x8000;

[Flags]
public enum ConsoleModes : uint {
EnableProcessedInput = 1,
Expand Down Expand Up @@ -1260,6 +1265,7 @@ public override void AddRune (Rune rune)

if (Clip.Contains (ccol, crow)) {
OutputBuffer [position].Attributes = (ushort)currentAttribute;
OutputBuffer [position].Attributes |= (ushort)(currentUnderlineAttribute ? WindowsConsole.COMMON_LVB_UNDERSCORE : 0);
OutputBuffer [position].Char.UnicodeChar = (char)rune;
WindowsConsole.SmallRect.Update (ref damageRegion, (short)ccol, (short)crow);
}
Expand Down Expand Up @@ -1287,10 +1293,12 @@ public override void AddStr (ustring str)
}

int currentAttribute;
bool currentUnderlineAttribute;

public override void SetAttribute (Attribute c)
{
currentAttribute = c.Value;
currentUnderlineAttribute = c.UnderLine;
}

Attribute MakeColor (ConsoleColor f, ConsoleColor b)
Expand All @@ -1303,9 +1311,11 @@ Attribute MakeColor (ConsoleColor f, ConsoleColor b)
);
}

public override Attribute MakeAttribute (Color fore, Color back)
public override Attribute MakeAttribute (Color fore, Color back, bool underline = false)
{
return MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
var attr = MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
attr.UnderLine = underline;
return attr;
}

public override void Refresh ()
Expand Down Expand Up @@ -1466,8 +1476,7 @@ public override void SendKeys (char keyChar, ConsoleKey key, bool shift, bool al

try {
ProcessInput (input);
} catch (OverflowException) { }
finally {
} catch (OverflowException) { } finally {
keyEvent.bKeyDown = false;
input.KeyEvent = keyEvent;
ProcessInput (input);
Expand Down
15 changes: 12 additions & 3 deletions Terminal.Gui/Core/ConsoleDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,22 @@ public struct Attribute {
/// The background color.
/// </summary>
public Color Background { get; }
/// <summary>
/// The background color.
/// </summary>
public bool UnderLine { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="Attribute"/> struct.
/// </summary>
/// <param name="value">Value.</param>
/// <param name="foreground">Foreground</param>
/// <param name="background">Background</param>
public Attribute (int value, Color foreground = new Color (), Color background = new Color ())
/// <param name="underline">Underline</param>
public Attribute (int value, Color foreground = new Color (), Color background = new Color (), bool underline = false)
{
Value = value;
UnderLine = underline;
Foreground = foreground;
Background = background;
}
Expand All @@ -122,9 +128,11 @@ public struct Attribute {
/// </summary>
/// <param name="foreground">Foreground</param>
/// <param name="background">Background</param>
public Attribute (Color foreground = new Color (), Color background = new Color ())
/// <param name="underline">Underline</param>
public Attribute (Color foreground = new Color (), Color background = new Color (), bool underline = false)
{
Value = Make (foreground, background).Value;
UnderLine = underline;
Foreground = foreground;
Background = background;
}
Expand Down Expand Up @@ -1157,8 +1165,9 @@ public Rect Clip {
/// </summary>
/// <param name="fore">Foreground.</param>
/// <param name="back">Background.</param>
/// <param name="underline">Underline.</param>
/// <returns></returns>
public abstract Attribute MakeAttribute (Color fore, Color back);
public abstract Attribute MakeAttribute (Color fore, Color back, bool underline = false);

/// <summary>
/// Gets the current <see cref="Attribute"/>.
Expand Down
Loading