From 2d0a195981d84f636320a1f5450714cd22de8871 Mon Sep 17 00:00:00 2001 From: Chip Locke Date: Sat, 29 Sep 2018 19:25:47 -0700 Subject: [PATCH] UI vaguely half-ass works --- src/ConsoleBuffer/ConsoleWrapper.cs | 34 ++++++++++++++-------- src/condo/App.xaml | 3 +- src/condo/App.xaml.cs | 4 +++ src/condo/MainWindow.xaml | 2 +- src/condo/MainWindow.xaml.cs | 45 +++++++++++++++-------------- src/condo/TerminalManager.cs | 38 ++++++++++++++++++++++++ src/condo/condo.csproj | 1 + 7 files changed, 92 insertions(+), 35 deletions(-) create mode 100644 src/condo/TerminalManager.cs diff --git a/src/ConsoleBuffer/ConsoleWrapper.cs b/src/ConsoleBuffer/ConsoleWrapper.cs index 0981752..7cce3e3 100644 --- a/src/ConsoleBuffer/ConsoleWrapper.cs +++ b/src/ConsoleBuffer/ConsoleWrapper.cs @@ -48,20 +48,30 @@ public string Contents public ConsoleWrapper(string command) { - this.Contents = string.Empty; - - if (string.IsNullOrWhiteSpace(command)) + using (var sr = new StreamWriter(new FileStream(@"c:\users\wd\source\repos\wincon\fuckme.log", FileMode.Create))) { - throw new ArgumentException("No command specified.", nameof(command)); - } + sr.WriteLine($"let's start this shit"); - this.Command = command; + this.Contents = string.Empty; - this.CreatePTY(); - this.InitializeStartupInfo(); - this.StartProcess(); + if (string.IsNullOrWhiteSpace(command)) + { + throw new ArgumentException("No command specified.", nameof(command)); + } - Task.Run(() => this.ReadConsoleTask()); + this.Command = command; + sr.WriteLine($"running {command}"); + + this.CreatePTY(); + sr.WriteLine($"created PTY"); + this.InitializeStartupInfo(); + sr.WriteLine($"initialized startup shit"); + this.StartProcess(); + sr.WriteLine($"started the fucking process"); + + Task.Run(() => this.ReadConsoleTask()); + sr.WriteLine($"fuck ME"); + } } private void CreatePTY() @@ -126,7 +136,7 @@ private void ReadConsoleTask() { using (var ptyOutput = new FileStream(this.readHandle, FileAccess.Read)) { - var input = new byte[2048]; + var input = new byte[32]; while (true) { @@ -149,7 +159,7 @@ private static void ThrowForHResult(int hr, string exceptionMessage) #region INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; - public void OnPropertyChanged(string name) + private void OnPropertyChanged(string name) { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } diff --git a/src/condo/App.xaml b/src/condo/App.xaml index e415c21..726da67 100644 --- a/src/condo/App.xaml +++ b/src/condo/App.xaml @@ -2,7 +2,8 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:condo" - StartupUri="MainWindow.xaml"> + StartupUri="MainWindow.xaml" + DispatcherUnhandledException="Application_DispatcherUnhandledException"> diff --git a/src/condo/App.xaml.cs b/src/condo/App.xaml.cs index ca5887f..416a9cb 100644 --- a/src/condo/App.xaml.cs +++ b/src/condo/App.xaml.cs @@ -13,5 +13,9 @@ namespace condo /// public partial class App : Application { + private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) + { + System.Windows.MessageBox.Show(e.Exception.ToString()); + } } } diff --git a/src/condo/MainWindow.xaml b/src/condo/MainWindow.xaml index 87922ea..d096db3 100644 --- a/src/condo/MainWindow.xaml +++ b/src/condo/MainWindow.xaml @@ -7,6 +7,6 @@ mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> - + diff --git a/src/condo/MainWindow.xaml.cs b/src/condo/MainWindow.xaml.cs index e593666..81f9868 100644 --- a/src/condo/MainWindow.xaml.cs +++ b/src/condo/MainWindow.xaml.cs @@ -1,34 +1,37 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; - -namespace condo +namespace condo { + using System; + using System.ComponentModel; + using System.IO; + using System.Threading; + using System.Threading.Tasks; + using System.Windows; + /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { - private ConsoleBuffer.ConsoleWrapper consoleWrapper; - public MainWindow() { InitializeComponent(); + this.stuff.Text = "sdkjashfgdjkas"; + var terminal = TerminalManager.Instance.GetOrCreate(0, "ping -t localhost"); + terminal.PropertyChanged += this.UpdateContents; + } + + private void UpdateContents(object sender, PropertyChangedEventArgs args) + { + var con = sender as ConsoleBuffer.ConsoleWrapper; + if (sender == null && args.PropertyName != "Content") + { + return; // XXX: log? + } - this.consoleWrapper = new ConsoleBuffer.ConsoleWrapper("debian run yes"); - this.stuff.DataContext = this.consoleWrapper; - this.stuff.Text = this.consoleWrapper.Contents; + Dispatcher.InvokeAsync(() => + { + this.stuff.Text = con.Contents; + }); } } } diff --git a/src/condo/TerminalManager.cs b/src/condo/TerminalManager.cs new file mode 100644 index 0000000..3fb4972 --- /dev/null +++ b/src/condo/TerminalManager.cs @@ -0,0 +1,38 @@ +namespace condo +{ + using ConsoleBuffer; + using System.Collections.Generic; + using System.Threading.Tasks; + + /// + /// Manages creating/destroying terminals through queued requests. + /// + public sealed class TerminalManager + { + public static readonly TerminalManager Instance = new TerminalManager(); + + private readonly object terminalLock = new object(); + private readonly Dictionary terminals; + + private TerminalManager() + { + this.terminals = new Dictionary(); + } + + public ConsoleWrapper GetOrCreate(int id, string command) + { + ConsoleWrapper con; + lock (this.terminalLock) + { + if (this.terminals.TryGetValue(id, out con)) + { + return con; + } + + con = new ConsoleWrapper(command); + this.terminals[id] = con; + return con; + } + } + } +} \ No newline at end of file diff --git a/src/condo/condo.csproj b/src/condo/condo.csproj index 4392656..766d9a7 100644 --- a/src/condo/condo.csproj +++ b/src/condo/condo.csproj @@ -55,6 +55,7 @@ MSBuild:Compile Designer + MSBuild:Compile Designer