Skip to content

Commit

Permalink
futzed with task exceptions (still hosed), lines can now have charact…
Browse files Browse the repository at this point in the history
…ers set at specific positions, still avoiding the actual sequence parsing
  • Loading branch information
doubleyewdee committed Oct 25, 2018
1 parent 673bd3e commit 8d1a95c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 32 deletions.
17 changes: 12 additions & 5 deletions src/ConsoleBuffer/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ 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 cursorX;
private short cursorY;
public (short X, short Y) CursorPosition => (this.cursorX, this.cursorY);

private short bufferTopVisibleLine
{
Expand Down Expand Up @@ -46,17 +48,22 @@ public void Append(byte[] bytes, int length)
{
if (ch == '\n')
{
Logger.Verbose($"newline (current: {this.lines[this.currentLine]}!");
Logger.Verbose($"newline (current: {this.lines[this.currentLine]})");
if (this.currentLine == this.lines.Count - 1)
{
this.lines.Add(new Line());
}

this.CursorPosition = (X: this.CursorPosition.X, Y: (short)Math.Min(this.Height - 1, this.CursorPosition.Y + 1));
this.cursorY = (short)Math.Min(this.Height - 1, this.cursorY + 1);
}
else if (ch == '\r')
{
Logger.Verbose($"carriage return");
this.cursorX = 0;
}

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);
this.lines[this.currentLine].Set(this.cursorX, new Character { Glyph = ch });
this.cursorX = (short)Math.Min(this.Width - 1, this.cursorX + 1);
}
}
}
Expand Down
32 changes: 19 additions & 13 deletions src/ConsoleBuffer/ConsoleWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ public sealed class ConsoleWrapper : IDisposable, INotifyPropertyChanged

public string Command { get; private set; }

public short Width { get { return this.width; } set { this.width = value; } }
private short width;
public short Height { get { return this.height; } set { this.height = value; } }
private short height;
public short Width { get; set; }
public short Height { get; set; }

/// <summary>
/// The handle from which we read data from the console.
Expand Down Expand Up @@ -128,13 +126,21 @@ private void ReadConsoleTask()
return;
}

Logger.Verbose("reading ...");
var read = ptyOutput.Read(input, 0, input.Length);
Logger.Verbose("appending ...");
this.Buffer.Append(input, read);
Logger.Verbose("notifying ...");
this.OnPropertyChanged(nameof(this.Buffer));
Logger.Verbose("notified!");
try
{
Logger.Verbose("reading ...");
var read = ptyOutput.Read(input, 0, input.Length);
Logger.Verbose("appending ...");
this.Buffer.Append(input, read);
Logger.Verbose("notifying ...");
this.OnPropertyChanged(nameof(this.Buffer));
Logger.Verbose("notified!");
}
catch (Exception ex) // XXX: this is so fucking annoying that I have to do this.
{
Logger.Verbose(ex.ToString());
throw;
}
}
}
}
Expand All @@ -146,8 +152,8 @@ private void UpdateDimensions(short newHeight, short newWidth)

if (this.consoleHandle != IntPtr.Zero && (newHeight != this.Height || newWidth != this.Width))
{
this.height = newHeight;
this.width = newWidth;
this.Height = newHeight;
this.Width = newWidth;
NativeMethods.ResizePseudoConsole(this.consoleHandle, new NativeMethods.COORD { X = this.Width, Y = this.Height });
}
}
Expand Down
24 changes: 14 additions & 10 deletions src/ConsoleBuffer/Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,27 @@ public sealed class Line : IEnumerable<Character>
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();
if (this.chars.Count <= pos)
{
this.Extend(pos);
}
this.chars[pos] = ch;
}

private void Extend(int pos)
{
// XXX: not efficient.
while (this.chars.Count <= pos)
{
this.chars.Add(new Character { Glyph = ' ' });
}
}

public IEnumerator<Character> GetEnumerator()
Expand Down
3 changes: 2 additions & 1 deletion src/ConsoleBuffer/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public sealed class Logger

private Logger()
{
this.writer = new StreamWriter(@"C:\Users\wd\Source\Repos\wincon\wincon.log", false, Encoding.UTF8);
var logfile = Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), @"Source\Repos\wincon\wincon.log");
this.writer = new StreamWriter(logfile, false, Encoding.UTF8);
}

private void Write(string msg)
Expand Down
3 changes: 0 additions & 3 deletions src/condo/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<runtime>
<ThrowUnobservedTaskExceptions enabled="true" />
</runtime>
</configuration>
6 changes: 6 additions & 0 deletions src/condo/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

Expand All @@ -18,6 +19,11 @@ public App()
{
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;

// XXX: this is some garbage. tasks are a pain.
#if DEBUG
new Timer((_) => GC.Collect(2, GCCollectionMode.Forced, true, true), null, TimeSpan.Zero, TimeSpan.FromMilliseconds(50));
#endif
}

private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
Expand Down

0 comments on commit 8d1a95c

Please sign in to comment.