Skip to content

Commit 0643245

Browse files
committed
Tweak yielding for event processing slightly
Based on feedback from Lee, the conditions under which we need to pulse the pipeline can be even more restrictive and a more minimal "useless bit of script" can be used.
1 parent 6352072 commit 0643245

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

PSReadLine/ReadLine.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Diagnostics.CodeAnalysis;
5+
using System.Linq;
56
using System.Management.Automation;
67
using System.Management.Automation.Language;
78
using System.Management.Automation.Runspaces;
@@ -126,16 +127,28 @@ private static ConsoleKeyInfo ReadKey()
126127

127128
// If we timed out, check for event subscribers (which is just
128129
// 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))
132136
{
133137
if (ps == null)
134138
{
135139
ps = PowerShell.Create(RunspaceMode.CurrentRunspace);
136-
ps.AddCommand("Out-Null");
140+
ps.AddScript("0");
137141
}
142+
143+
// To detect output during possible event processing, see if the cursor moved
144+
// and rerender if so.
145+
var y = Console.CursorTop;
138146
ps.Invoke();
147+
if (y != Console.CursorTop)
148+
{
149+
_singleton._initialY = Console.CursorTop;
150+
_singleton.Render();
151+
}
139152
}
140153
}
141154
}

0 commit comments

Comments
 (0)