|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Diagnostics; |
4 | 4 | using System.Diagnostics.CodeAnalysis; |
| 5 | +using System.Linq; |
5 | 6 | using System.Management.Automation; |
6 | 7 | using System.Management.Automation.Language; |
7 | 8 | using System.Management.Automation.Runspaces; |
@@ -126,16 +127,28 @@ private static ConsoleKeyInfo ReadKey() |
126 | 127 |
|
127 | 128 | // If we timed out, check for event subscribers (which is just |
128 | 129 | // a hint that there might be an event waiting to be processed.) |
129 | | - // If there are any event subscribers, run a tiny useless bit |
130 | | - // of PowerShell so that the events can be processed. |
131 | | - if (Runspace.DefaultRunspace.Events.Subscribers.Count > 0) |
| 130 | + // If there are any event subscribers that have an action (which might |
| 131 | + // write to the console) and have a source object (i.e. aren't engine |
| 132 | + // events), run a tiny useless bit of PowerShell so that the events |
| 133 | + // can be processed. |
| 134 | + var eventSubscribers = Runspace.DefaultRunspace.Events.Subscribers; |
| 135 | + if (eventSubscribers.Any(sub => sub.Action != null && sub.SourceObject != null)) |
132 | 136 | { |
133 | 137 | if (ps == null) |
134 | 138 | { |
135 | 139 | ps = PowerShell.Create(RunspaceMode.CurrentRunspace); |
136 | | - ps.AddCommand("Out-Null"); |
| 140 | + ps.AddScript("0"); |
137 | 141 | } |
| 142 | + |
| 143 | + // To detect output during possible event processing, see if the cursor moved |
| 144 | + // and rerender if so. |
| 145 | + var y = Console.CursorTop; |
138 | 146 | ps.Invoke(); |
| 147 | + if (y != Console.CursorTop) |
| 148 | + { |
| 149 | + _singleton._initialY = Console.CursorTop; |
| 150 | + _singleton.Render(); |
| 151 | + } |
139 | 152 | } |
140 | 153 | } |
141 | 154 | } |
|
0 commit comments