/// <summary> /// Entry point - called from the PowerShell function PSConsoleHostReadline /// after the prompt has been displayed. /// </summary> /// <returns>The complete command line.</returns> public static string ReadLine() { uint dwConsoleMode; var handle = NativeMethods.GetStdHandle((uint)StandardHandleId.Input); NativeMethods.GetConsoleMode(handle, out dwConsoleMode); try { // Clear a couple flags so we can actually receive certain keys: // ENABLE_PROCESSED_INPUT - enables Ctrl+C // ENABLE_LINE_INPUT - enables Ctrl+S NativeMethods.SetConsoleMode(handle, dwConsoleMode & ~(NativeMethods.ENABLE_PROCESSED_INPUT | NativeMethods.ENABLE_LINE_INPUT)); _singleton.Initialize(); return(_singleton.InputLoop()); } catch (OperationCanceledException) { // Console is exiting - return value isn't too critical - null or 'exit' could work equally well. return(""); } finally { NativeMethods.SetConsoleMode(handle, dwConsoleMode); } }
/// <summary> /// Entry point - called from the PowerShell function PSConsoleHostReadline /// after the prompt has been displayed. /// </summary> /// <returns>The complete command line.</returns> public static string ReadLine(Runspace remoteRunspace = null, EngineIntrinsics engineIntrinsics = null) { var handle = NativeMethods.GetStdHandle((uint)StandardHandleId.Input); NativeMethods.GetConsoleMode(handle, out _singleton._prePSReadlineConsoleMode); try { // Clear a couple flags so we can actually receive certain keys: // ENABLE_PROCESSED_INPUT - enables Ctrl+C // ENABLE_LINE_INPUT - enables Ctrl+S NativeMethods.SetConsoleMode(handle, _singleton._prePSReadlineConsoleMode & ~(NativeMethods.ENABLE_PROCESSED_INPUT | NativeMethods.ENABLE_LINE_INPUT)); _singleton.Initialize(remoteRunspace, engineIntrinsics); return(_singleton.InputLoop()); } catch (OperationCanceledException) { // Console is exiting - return value isn't too critical - null or 'exit' could work equally well. return(""); } catch (ExitException) { return("exit"); } catch (Exception e) { // If we're running tests, just throw. if (_singleton._mockableMethods != _singleton) { throw; } while (e.InnerException != null) { e = e.InnerException; } var oldColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(PSReadLineResources.OopsAnErrorMessage1); Console.ForegroundColor = oldColor; var sb = new StringBuilder(); for (int i = 0; i < _lastNKeys.Count; i++) { sb.Append(' '); sb.Append(_lastNKeys[i].ToGestureString()); KeyHandler handler; if (_singleton._dispatchTable.TryGetValue(_lastNKeys[i], out handler) && "AcceptLine".Equals(handler.BriefDescription, StringComparison.OrdinalIgnoreCase)) { // Make it a little easier to see the keys sb.Append('\n'); } // TODO: print non-default function bindings and script blocks } Console.WriteLine(PSReadLineResources.OopsAnErrorMessage2, _lastNKeys.Count, sb, e); var lineBeforeCrash = _singleton._buffer.ToString(); _singleton.Initialize(remoteRunspace, _singleton._engineIntrinsics); InvokePrompt(); Insert(lineBeforeCrash); return(_singleton.InputLoop()); } finally { NativeMethods.SetConsoleMode(handle, _singleton._prePSReadlineConsoleMode); } }