Skip to content

Commit

Permalink
parse xterm color index values, remove a broken bold handler that was…
Browse files Browse the repository at this point in the history
… all lies
  • Loading branch information
doubleyewdee committed Nov 27, 2018
1 parent 1c23787 commit 4a5bf59
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
64 changes: 61 additions & 3 deletions src/ConsoleBuffer/Commands/SetGraphicsRendition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,22 @@ public enum Colors : short
None,
}

public const Colors DefaultForegroundColor = Colors.White;
public const Colors DefaultBackgroundColor = Colors.Black;

public bool HaveBasicForeground { get; private set; }
public Colors BasicForegroundColor { get; private set; } = Colors.None;
public bool HaveForeground { get; private set; }
public Character.ColorInfo ForegroundColor { get; private set; }
public bool HaveXtermForeground { get; private set; }
public int XtermForegroundColor { get; private set; }

public bool HaveBasicBackground { get; private set; }
public Colors BasicBackgroundColor { get; private set; } = Colors.None;
public bool HaveBackground { get; private set; }
public Character.ColorInfo BackgroundColor { get; private set; }
public bool HaveXtermBackground { get; private set; }
public int XtermBackgroundColor { get; private set; }

public FlagValue ForegroundBright { get; private set; }
public FlagValue BackgroundBright { get; private set; }
Expand Down Expand Up @@ -69,7 +76,6 @@ public SetGraphicsRendition(string bufferData) : base(bufferData)
case 1:
this.ForegroundBright = FlagValue.Set;
break;
case 2:
case 22:
this.ForegroundBright = FlagValue.Unset;
break;
Expand All @@ -96,6 +102,19 @@ public SetGraphicsRendition(string bufferData) : base(bufferData)
this.HaveBasicForeground = true;
this.BasicForegroundColor = (Colors)(pValue - 30);
break;
case 38:
{
if (this.ReadXtermColorIndex(p, out var idx))
{
this.HaveXtermForeground = true;
this.XtermForegroundColor = idx;
}
break;
}
case 39:
this.HaveBasicForeground = true;
this.BasicForegroundColor = DefaultForegroundColor;
break;
case 40:
case 41:
case 42:
Expand All @@ -107,6 +126,19 @@ public SetGraphicsRendition(string bufferData) : base(bufferData)
this.HaveBasicBackground = true;
this.BasicBackgroundColor = (Colors)(pValue - 40);
break;
case 48:
{
if (this.ReadXtermColorIndex(p, out var idx))
{
this.HaveXtermBackground = true;
this.XtermBackgroundColor = idx;
}
break;
}
case 49:
this.HaveBasicBackground = true;
this.BasicBackgroundColor = DefaultBackgroundColor;
break;
case 90:
case 91:
case 92:
Expand Down Expand Up @@ -137,6 +169,32 @@ public SetGraphicsRendition(string bufferData) : base(bufferData)
}
}

private bool ReadXtermColorIndex(int p, out int value)
{
value = -1;
if (++p < this.Parameters.Count)
{
var subCommand = this.ParameterToNumber(p, defaultValue: -1);
if (subCommand == 5)
{
if (++p < this.Parameters.Count)
{
value = this.ParameterToNumber(p, defaultValue: -1, maxValue: int.MaxValue);
return value > -1 && value < 256;
}
}
}

return false;
}

private void Reset()
{
this.ForegroundBright = this.BackgroundBright = this.Underline = this.Inverse = FlagValue.None;
this.HaveBasicForeground = this.HaveForeground = this.HaveXtermForeground = false;
this.HaveBasicBackground = this.HaveBackground = this.HaveXtermBackground = false;
}

private void SetDefault()
{
this.ForegroundBright = FlagValue.Unset;
Expand All @@ -145,8 +203,8 @@ private void SetDefault()
this.Inverse = FlagValue.Unset;
this.HaveForeground = this.HaveBackground = false;
this.HaveBasicForeground = this.HaveBasicBackground = true;
this.BasicForegroundColor = Colors.White;
this.BasicBackgroundColor = Colors.Black;
this.BasicForegroundColor = DefaultForegroundColor;
this.BasicBackgroundColor = DefaultBackgroundColor;
}

public override string ToString()
Expand Down
2 changes: 1 addition & 1 deletion test/ConsoleBufferTests/BufferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void MaxBufferSize()
public void BrightForegroundText()
{
var buffer = new ConsoleBuffer.Buffer(DefaultColumns, DefaultRows);
buffer.AppendString("\x1b[1mbb\x1b[2mn\x1b[1mb\x1b[2mnnnn\n");
buffer.AppendString("\x1b[1mbb\x1b[22mn\x1b[1mb\x1b[22mnnnn\n");
var surface = new RenderTest();
surface.OnChar = (c, x, y) =>
{
Expand Down
39 changes: 38 additions & 1 deletion test/ConsoleBufferTests/SequenceParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ public void SGRReset(string data)

[TestMethod]
[DataRow("1", ConsoleBuffer.Commands.SetGraphicsRendition.FlagValue.Set)]
[DataRow("2", ConsoleBuffer.Commands.SetGraphicsRendition.FlagValue.Unset)]
[DataRow("22", ConsoleBuffer.Commands.SetGraphicsRendition.FlagValue.Unset)]
public void SGRBold(string data, ConsoleBuffer.Commands.SetGraphicsRendition.FlagValue expectedValue)
{
Expand Down Expand Up @@ -313,6 +312,44 @@ public void SGRBasicBackgroundColors(string data, ConsoleBuffer.Commands.SetGrap
cmd.BackgroundBright);
}

[TestMethod]
[DataRow("38;5", false, 0)]
[DataRow("38;5;", false, 0)]
[DataRow("38;5;-1", false, 0)]
[DataRow("38;5;0", true, 0)]
[DataRow("38;5;255", true, 255)]
[DataRow("38;5;256", false, 0)]
public void SGRXtermForeground(string data, bool haveXtermColor, int expectedValue)
{
var parser = this.EnsureCommandParses($"\x1b[{data}m");
var cmd = parser.Command as ConsoleBuffer.Commands.SetGraphicsRendition;
Assert.IsNotNull(cmd);
Assert.AreEqual(haveXtermColor, cmd.HaveXtermForeground);
if (haveXtermColor)
{
Assert.AreEqual(expectedValue, cmd.XtermForegroundColor);
}
}

[TestMethod]
[DataRow("48;5", false, 0)]
[DataRow("48;5;", false, 0)]
[DataRow("48;5;-1", false, 0)]
[DataRow("48;5;0", true, 0)]
[DataRow("48;5;255", true, 255)]
[DataRow("48;5;256", false, 0)]
public void SGRXtermBackground(string data, bool haveXtermColor, int expectedValue)
{
var parser = this.EnsureCommandParses($"\x1b[{data}m");
var cmd = parser.Command as ConsoleBuffer.Commands.SetGraphicsRendition;
Assert.IsNotNull(cmd);
Assert.AreEqual(haveXtermColor, cmd.HaveXtermBackground);
if (haveXtermColor)
{
Assert.AreEqual(expectedValue, cmd.XtermBackgroundColor);
}
}

private SequenceParser EnsureCommandParses(string command)
{
var parser = new SequenceParser();
Expand Down

0 comments on commit 4a5bf59

Please sign in to comment.