Skip to content

Commit

Permalink
just use a circular buffer since it was easy to find on github<3
Browse files Browse the repository at this point in the history
  • Loading branch information
doubleyewdee committed Nov 21, 2018
1 parent 560e684 commit a29cdfd
Show file tree
Hide file tree
Showing 4 changed files with 420 additions and 38 deletions.
36 changes: 15 additions & 21 deletions ConsoleBuffer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ Global
{A16D11B0-4785-434F-8514-ABCC16DDFBFD}.Debug|x86.Build.0 = Debug|Any CPU
{A16D11B0-4785-434F-8514-ABCC16DDFBFD}.Release|Any CPU.ActiveCfg = Debug|Any CPU
{A16D11B0-4785-434F-8514-ABCC16DDFBFD}.Release|Any CPU.Build.0 = Debug|Any CPU
{A16D11B0-4785-434F-8514-ABCC16DDFBFD}.Release|ARM.ActiveCfg = Release|Any CPU
{A16D11B0-4785-434F-8514-ABCC16DDFBFD}.Release|ARM.Build.0 = Release|Any CPU
{A16D11B0-4785-434F-8514-ABCC16DDFBFD}.Release|x64.ActiveCfg = Release|Any CPU
{A16D11B0-4785-434F-8514-ABCC16DDFBFD}.Release|x64.Build.0 = Release|Any CPU
{A16D11B0-4785-434F-8514-ABCC16DDFBFD}.Release|x86.ActiveCfg = Release|Any CPU
{A16D11B0-4785-434F-8514-ABCC16DDFBFD}.Release|x86.Build.0 = Release|Any CPU
{A16D11B0-4785-434F-8514-ABCC16DDFBFD}.Release|ARM.ActiveCfg = Debug|Any CPU
{A16D11B0-4785-434F-8514-ABCC16DDFBFD}.Release|ARM.Build.0 = Debug|Any CPU
{A16D11B0-4785-434F-8514-ABCC16DDFBFD}.Release|x64.ActiveCfg = Debug|Any CPU
{A16D11B0-4785-434F-8514-ABCC16DDFBFD}.Release|x64.Build.0 = Debug|Any CPU
{A16D11B0-4785-434F-8514-ABCC16DDFBFD}.Release|x86.ActiveCfg = Debug|Any CPU
{A16D11B0-4785-434F-8514-ABCC16DDFBFD}.Release|x86.Build.0 = Debug|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Debug|ARM.ActiveCfg = Debug|Any CPU
Expand All @@ -54,12 +54,12 @@ Global
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Debug|x86.Build.0 = Debug|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Release|Any CPU.ActiveCfg = Debug|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Release|Any CPU.Build.0 = Debug|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Release|ARM.ActiveCfg = Release|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Release|ARM.Build.0 = Release|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Release|x64.ActiveCfg = Release|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Release|x64.Build.0 = Release|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Release|x86.ActiveCfg = Release|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Release|x86.Build.0 = Release|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Release|ARM.ActiveCfg = Debug|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Release|ARM.Build.0 = Debug|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Release|x64.ActiveCfg = Debug|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Release|x64.Build.0 = Debug|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Release|x86.ActiveCfg = Debug|Any CPU
{D890EB09-11B9-41A8-B9A7-C1D159312A32}.Release|x86.Build.0 = Debug|Any CPU
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Debug|Any CPU.ActiveCfg = Debug|x86
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Debug|ARM.ActiveCfg = Debug|ARM
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Debug|ARM.Build.0 = Debug|ARM
Expand All @@ -71,15 +71,9 @@ Global
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Debug|x86.Build.0 = Debug|x86
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Debug|x86.Deploy.0 = Debug|x86
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Release|Any CPU.ActiveCfg = Debug|x86
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Release|ARM.ActiveCfg = Release|ARM
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Release|ARM.Build.0 = Release|ARM
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Release|ARM.Deploy.0 = Release|ARM
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Release|x64.ActiveCfg = Release|x64
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Release|x64.Build.0 = Release|x64
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Release|x64.Deploy.0 = Release|x64
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Release|x86.ActiveCfg = Release|x86
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Release|x86.Build.0 = Release|x86
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Release|x86.Deploy.0 = Release|x86
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Release|ARM.ActiveCfg = Debug|x86
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Release|x64.ActiveCfg = Debug|x86
{FB9FD3E5-EDF1-4924-8945-42DC0AFE478F}.Release|x86.ActiveCfg = Debug|x86
{D11953E5-195F-43F4-9109-DE93CED6D016}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D11953E5-195F-43F4-9109-DE93CED6D016}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D11953E5-195F-43F4-9109-DE93CED6D016}.Debug|ARM.ActiveCfg = Debug|Any CPU
Expand Down
18 changes: 6 additions & 12 deletions src/ConsoleBuffer/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public sealed class Buffer : INotifyPropertyChanged
{
private readonly SequenceParser parser = new SequenceParser();
private readonly List<Line> lines = new List<Line>();
private readonly CircularBuffer<Line> lines = new CircularBuffer<Line>(short.MaxValue);
private readonly object renderLock = new object();

private short cursorX;
Expand All @@ -21,7 +21,7 @@ private short bufferTopVisibleLine
{
get
{
return (short)Math.Max(0, this.lines.Count - this.Height);
return (short)Math.Max(0, this.lines.Size - this.Height);
}
}
private short currentLine
Expand All @@ -41,7 +41,7 @@ public Buffer(short width, short height)
{
this.Width = width;
this.Height = height;
this.lines.Add(new Line());
this.lines.PushBack(new Line());
}

public void Append(byte[] bytes, int length)
Expand Down Expand Up @@ -124,15 +124,9 @@ private void HandleControlCharacter(ControlCharacterCommand.ControlCode code)
break;
case ControlCharacterCommand.ControlCode.FF: // NB: could clear screen with this if we were so inclined. apparently xterm treats this as LF though, let's emulate.
case ControlCharacterCommand.ControlCode.LF:
if (this.currentLine == short.MaxValue)
if (this.currentLine == this.lines.Size - 1)
{
// XXX: perf nightmare, need to turn lines into a circular buffer probs.
this.lines.RemoveAt(0);
this.lines.Add(new Line());
}
else if (this.currentLine == this.lines.Count - 1)
{
this.lines.Add(new Line());
this.lines.PushBack(new Line());
}

this.cursorY = (short)Math.Min(this.Height - 1, this.cursorY + 1);
Expand All @@ -158,7 +152,7 @@ public void Render(IRenderTarget target)
for (var y = 0; y < this.Height; ++y)
{
var renderLine = this.bufferTopVisibleLine + y;
var line = renderLine < this.lines.Count ? this.lines[renderLine] : Line.Empty;
var line = renderLine < this.lines.Size ? this.lines[renderLine] : Line.Empty;
short x = 0;
foreach (var c in line)
{
Expand Down
Loading

0 comments on commit a29cdfd

Please sign in to comment.