Skip to content

Commit

Permalink
light refactoring, vaguely getting a sense of what a buffer would act…
Browse files Browse the repository at this point in the history
…ually look like
  • Loading branch information
doubleyewdee committed Oct 24, 2018
1 parent 3f44827 commit 673bd3e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
37 changes: 25 additions & 12 deletions src/ConsoleBuffer/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ public sealed class Buffer : INotifyPropertyChanged
private readonly List<Line> lines = new List<Line>();
private readonly object renderLock = new object();

public (short X, short Y) CursorPosition { get; private set; }

private short bufferTopVisibleLine
{
get
{
return (short)Math.Max(0, this.lines.Count - this.Height);
}
}
private short currentLine
{
get
{
return (short)(this.bufferTopVisibleLine + this.CursorPosition.Y);
}
}

public short Width { get; set; }
public short Height { get; set; }

Expand All @@ -25,24 +42,21 @@ public void Append(byte[] bytes, int length)
{
lock (this.renderLock)
{
int currentLine = this.lines.Count - 1;
foreach (char ch in Encoding.UTF8.GetString(bytes, 0, length))
{
if (ch == '\n' || this.lines[currentLine].Length == this.Width)
if (ch == '\n')
{
if (currentLine == this.lines.Count - 1)
Logger.Verbose($"newline (current: {this.lines[this.currentLine]}!");
if (this.currentLine == this.lines.Count - 1)
{
this.lines.Add(new Line());
++currentLine;
}

if (ch == '\n')
continue;
this.CursorPosition = (X: this.CursorPosition.X, Y: (short)Math.Min(this.Height - 1, this.CursorPosition.Y + 1));
}

this.lines[currentLine].Append(new Character { Glyph = ch });
if (ch == ' ')
Logger.Verbose("space!");
this.lines[this.currentLine].Append(new Character { Glyph = ch });
this.CursorPosition = (X: (short)Math.Min(this.Width - 1, this.CursorPosition.X + 1), this.CursorPosition.Y);
}
}
}
Expand All @@ -54,18 +68,17 @@ public void Render(IRenderTarget target)
{
lock (this.renderLock)
{
var startLine = Math.Max(0, this.lines.Count - this.Height);
for (var x = 0; x < this.Height; ++x)
{
var renderLine = startLine + x;
var renderLine = this.bufferTopVisibleLine + x;
var line = renderLine < this.lines.Count ? this.lines[renderLine] : Line.Empty;
short y = 0;
foreach (var c in line)
{
target.RenderCharacter(c, x, y);
++y;
}
while (y < this.Width - 1)
while (y < this.Width)
{
target.RenderCharacter(new Character { Glyph = ' ' }, x, y);
++y;
Expand Down
10 changes: 10 additions & 0 deletions src/ConsoleBuffer/Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,15 @@ IEnumerator IEnumerable.GetEnumerator()
{
return chars.GetEnumerator();
}

public override string ToString()
{
var sb = new StringBuilder(this.chars.Count);
foreach (var c in this.chars)
{
sb.Append(c.Glyph);
}
return sb.ToString();
}
}
}
2 changes: 1 addition & 1 deletion src/ConsoleBuffer/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public sealed class Logger

private Logger()
{
this.writer = new StreamWriter(@"C:\Users\wd\Source\Repos\wincon\wincon.log", true, Encoding.UTF8);
this.writer = new StreamWriter(@"C:\Users\wd\Source\Repos\wincon\wincon.log", false, Encoding.UTF8);
}

private void Write(string msg)
Expand Down
3 changes: 1 addition & 2 deletions src/condo/TerminalManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ private TerminalManager()

public ConsoleWrapper GetOrCreate(int id, string command)
{
ConsoleWrapper con;
lock (this.terminalLock)
{
if (this.terminals.TryGetValue(id, out con))
if (this.terminals.TryGetValue(id, out ConsoleWrapper con))
{
return con;
}
Expand Down

0 comments on commit 673bd3e

Please sign in to comment.