Jaykul / PoshConsole

PoshConsole is a WPF control that self-hosts PowerShell, a framework for building PowerShell-based managment apps!
Other
77 stars 18 forks source link

TabControl binding does not work #16

Open akshinmustafayev opened 2 years ago

akshinmustafayev commented 2 years ago

When I try to use control with binding in my TabControl.ContentTemplate application crashes.

Everything is fine if I get it out of TabControl and dont use binding.

My goal is to use separate independent PowerShell console host sessions for each TabItem.

Jaykul commented 2 years ago

Do you mean that crashes any time it's in a TabControl?

akshinmustafayev commented 2 years ago

Yes. To be specific, when binding is used

Jaykul commented 2 years ago

I will have a look and see if I can figure out why. I have to admit I haven't touched this code in years, but I have a PR to review anyway....

akshinmustafayev commented 2 years ago

I have some ideas regarding improving this control.

  1. Console must be run as is. No custom design or whatever
  2. Execution of powershell code in console must be simplified
  3. Support of binding must be implemented

More detailed explanation:

Console must be run as is. No custom design or whatever

By this I mean during testing I was getting custom design in the console. Seemed like it is not a default PowerShell view like this: image

During PowerShell scripts` execution output was mixing with previous lines, it was not properly showed as it would be in default PowerShell console.

Execution of PowerShell code in console must be simplified

By this I mean interaction with the control must be as easy as possible. For example these public functions must be implemented:

public void Invoke(string PowerShellCode);
public void Invoke(string PathToPSFile);
public string Invoke(string PowerShellCode);
public string Invoke(string PathToPSFile);
public string Invoke(Command MyCommand);
public bool DisableConsoleOutput(); // false by default

so usage could be:

// in this scenario output goes directly to our console
myPoshConsoleControl.Invoke(@"D:\script.ps1");

or

// in this scenario output does not go to console
myPoshConsoleControl.DisableConsoleOutput = true;
var result = myPoshConsoleControl.Invoke(@"D:\script.ps1");

// Note: Output goes to textbox control as script continues to execute
await foreach (var outputEvent in result.ListenAsync())
{
    switch (outputEvent)
    {
        case StartedOutputEvent started:
            {
                MessageBox.Show("script started");
            }
            break;
        case StandardOutputEvent stdOut:
            {
                textBox1.Text += stdOut;
            }
            break;
        case StandardErrorEvent stdErr:
            {
                if (stdErr.Text != "")
                {
                    textBox2.Text += stdErr;
                }
            }
            break;
        case ExitedCommandEvent exited:
            {
                    MessageBox.Show("script ended");
            }
            break;
    }
}

or

// buffers
var stdOutBuffer = new StringBuilder();
var stdErrBuffer = new StringBuilder();

// set result to buffers
myPoshConsoleControl.Invoke(@"D:\script.ps1").WithStandardOutputPipe(stdOutBuffer).WithStandardErrorPipe(stdErrBuffer);

// Set values to textboxes after execution finishes
textBox1.Text = stdOutBuffer;
textBox2.Text = stdErrBuffer;

or using Command class

Support of binding must be implemented

Nut much to explain, but the following must be considered:

  1. If I open many tabs one by one, separate independent PowerShell session console hosts must be opened as well
  2. Opening many tabs must not block main application thread - no UI locks. I assume asynchronous open is the key
  3. What if opening console host fails for whatever reason? I suggest reload button must be shown in the center of the control to try to reload console host.

Conclusion

Implementation of such control seems very complicated to me. Don`t know even how to achieve this. I have been looking for such control for my application EasyJob with no luck. In my application I just write output to rich text box, which absolutely does not give any PowerShell console integration experience.