-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I made a whole ton of changes and didn't commit them atomically like …
…a smart person. was probably drinking beer. anyway some skeletal stuff works now.
- Loading branch information
1 parent
2d0a195
commit 3f44827
Showing
12 changed files
with
334 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
namespace ConsoleBuffer | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.IO; | ||
using System.Text; | ||
|
||
public sealed class Buffer : INotifyPropertyChanged | ||
{ | ||
private readonly List<Line> lines = new List<Line>(); | ||
private readonly object renderLock = new object(); | ||
|
||
public short Width { get; set; } | ||
public short Height { get; set; } | ||
|
||
public Buffer(short width, short height) | ||
{ | ||
this.Width = width; | ||
this.Height = height; | ||
this.lines.Add(new Line()); | ||
} | ||
|
||
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 (currentLine == this.lines.Count - 1) | ||
{ | ||
this.lines.Add(new Line()); | ||
++currentLine; | ||
} | ||
|
||
if (ch == '\n') | ||
continue; | ||
} | ||
|
||
this.lines[currentLine].Append(new Character { Glyph = ch }); | ||
if (ch == ' ') | ||
Logger.Verbose("space!"); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Render character-by-character onto the specified target. | ||
/// </summary> | ||
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 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) | ||
{ | ||
target.RenderCharacter(new Character { Glyph = ' ' }, x, y); | ||
++y; | ||
} | ||
} | ||
} | ||
} | ||
|
||
#region INotifyPropertyChanged | ||
public event PropertyChangedEventHandler PropertyChanged; | ||
private void OnPropertyChanged(string name) | ||
{ | ||
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); | ||
} | ||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ConsoleBuffer | ||
{ | ||
// XXX: Gonna end up with a lot of these and they're really freakin' big. | ||
public struct Character | ||
{ | ||
public struct ColorInfo | ||
{ | ||
public byte R; | ||
public byte G; | ||
public byte B; | ||
public byte A; | ||
} | ||
|
||
public ColorInfo Foreground { get; set; } | ||
public ColorInfo Background { get; set; } | ||
public char Glyph { get; set; } // XXX: char won't cut it for emoji/etc, gonna have to re-do this later! | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace ConsoleBuffer | ||
{ | ||
public interface IRenderTarget | ||
{ | ||
/// <summary> | ||
/// Instructs the target to render the character 'c' at x/y coordinates. | ||
/// </summary> | ||
/// <param name="c">Character to render.</param> | ||
/// <param name="x">Horizontal offset.</param> | ||
/// <param name="y">Vertical offset.</param> | ||
void RenderCharacter(Character c, int x, int y); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ConsoleBuffer | ||
{ | ||
public sealed class Line : IEnumerable<Character> | ||
{ | ||
public static readonly Line Empty = new Line(); | ||
|
||
private readonly List<Character> chars = new List<Character>(); | ||
public int Length => this.chars.Count; | ||
|
||
/// <summary> | ||
/// Throw another character on to the end of the line. | ||
/// </summary> | ||
/// <param name="ch"></param> | ||
public void Append(Character ch) | ||
{ | ||
this.chars.Add(ch); | ||
} | ||
|
||
/// <summary> | ||
/// Set a character value at a specified position. If the line is not long enough it is extended with blanks. | ||
/// </summary> | ||
/// <param name="pos">Position to set.</param> | ||
/// <param name="ch">Character value.</param> | ||
public void Set(int pos, Character ch) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public IEnumerator<Character> GetEnumerator() | ||
{ | ||
return chars.GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return chars.GetEnumerator(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
namespace ConsoleBuffer | ||
{ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
|
||
public sealed class Logger | ||
{ | ||
private static readonly Logger Instance = new Logger(); | ||
|
||
private readonly StreamWriter writer; | ||
|
||
private Logger() | ||
{ | ||
this.writer = new StreamWriter(@"C:\Users\wd\Source\Repos\wincon\wincon.log", true, Encoding.UTF8); | ||
} | ||
|
||
private void Write(string msg) | ||
{ | ||
lock (this.writer) | ||
{ | ||
this.writer.WriteLine($"{DateTimeOffset.Now}: {msg}"); | ||
this.writer.Flush(); | ||
} | ||
} | ||
|
||
public static void Verbose(string msg) | ||
{ | ||
Instance.Write(msg); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.