Skip to content

Commit d671045

Browse files
committed
Add function InvokePrompt to re-evaluate prompt
InvokePrompt can be used from a custom key handler to invoke the prompt function again. This is done in a way that minimizes changes to the display - the previous prompt is overwritten and the cursor is placed where it was before the prompt was evaluated previously. The current input is also preserved.
1 parent 99a26d8 commit d671045

File tree

5 files changed

+70
-10
lines changed

5 files changed

+70
-10
lines changed

PSReadLine/Cmdlets.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -453,14 +453,14 @@ public class SetKeyHandlerCommand : PSCmdlet
453453
"CharacterSearchBackward", "ClearScreen", "Complete", "Copy", "Cut",
454454
"DeleteChar", "DigitArgument", "DisableDemoMode", "EnableDemoMode", "EndOfHistory",
455455
"EndOfLine", "ExchangePointAndMark", "ForwardChar", "ForwardDeleteLine", "ForwardSearchHistory",
456-
"ForwardWord", "GotoBrace", "HistorySearchBackward", "HistorySearchForward", "KillLine",
457-
"KillRegion", "KillWord", "NextHistory", "NextWord", "Paste",
458-
"PossibleCompletions", "PreviousHistory", "Redo", "ReverseSearchHistory", "RevertLine",
459-
"SelectBackwardChar", "SelectBackwardWord", "SelectForwardChar", "SelectForwardWord", "SelectNextWord",
460-
"SelectShellBackwardWord", "SelectShellForwardWord", "SelfInsert", "SetMark", "ShellBackwardKillWord",
461-
"ShellBackwardWord", "ShellForwardWord", "ShellKillWord", "ShowKeyBindings", "TabCompleteNext",
462-
"TabCompletePrevious", "Undo", "UnixWordRubout", "WhatIsKey", "Yank",
463-
"YankLastArg", "YankNthArg", "YankPop")]
456+
"ForwardWord", "GotoBrace", "HistorySearchBackward", "HistorySearchForward", "InvokePrompt",
457+
"KillLine", "KillRegion", "KillWord", "NextHistory", "NextWord",
458+
"Paste", "PossibleCompletions", "PreviousHistory", "Redo", "ReverseSearchHistory",
459+
"RevertLine", "SelectBackwardChar", "SelectBackwardWord", "SelectForwardChar", "SelectForwardWord",
460+
"SelectNextWord", "SelectShellBackwardWord", "SelectShellForwardWord", "SelfInsert", "SetMark",
461+
"ShellBackwardKillWord", "ShellBackwardWord", "ShellForwardWord", "ShellKillWord", "ShowKeyBindings",
462+
"TabCompleteNext", "TabCompletePrevious", "Undo", "UnixWordRubout", "WhatIsKey",
463+
"Yank", "YankLastArg", "YankNthArg", "YankPop")]
464464
[Parameter(Position = 1, Mandatory = true, ParameterSetName = "Function")]
465465
public string Function { get; set; }
466466

PSReadLine/PSReadLineResources.Designer.cs

Lines changed: 20 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PSReadLine/PSReadLineResources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,4 +354,7 @@
354354
<data name="PowerShellColorTable" xml:space="preserve">
355355
<value>\red0\green0\blue0\red0\green0\blue128\red0\green128\blue0\red0\green128\blue128\red128\green0\blue0\red1\green36\blue86\red238\green237\blue240\red192\green192\blue192\red128\green128\blue128\red0\green0\blue255\red0\green255\blue0\red0\green255\blue255\red255\green0\blue0\red255\green0\blue255\red255\gree\red0\green0\blue0;\red0\green0\blue128;\red0\green128\blue0;\red0\green128\blue128;\red128\green0\blue0;\red1\green36\blue86;\red238\green237\blue240;\red192\green192\blue192;\red128\green128\blue128;\red0\green0\blue255;\red0\green255\blue0;\red0\green255\blue255;\red255\green0\blue0;\red255\green0\blue255;\red255\green255\blue0;\red255\green255\blue255;</value>
356356
</data>
357+
<data name="InvokePromptDescription" xml:space="preserve">
358+
<value>Erases the current prompt and calls the prompt function to redisplay the prompt</value>
359+
</data>
357360
</root>

PSReadLine/ReadLine.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3779,6 +3779,39 @@ private static void DumpScreenToClipboard(int top, int count)
37793779
Clipboard.SetDataObject(dataObject, copy: true);
37803780
}
37813781

3782+
/// <summary>
3783+
/// Erases the current prompt and calls the prompt function to redisplay
3784+
/// the prompt. Useful for custom key handlers that change state, e.g.
3785+
/// change the current directory.
3786+
/// </summary>
3787+
public static void InvokePrompt(ConsoleKeyInfo? key = null, object arg = null)
3788+
{
3789+
var currentBuffer = _singleton._buffer.ToString();
3790+
var currentPos = _singleton._current;
3791+
_singleton._buffer.Clear();
3792+
_singleton._current = 0;
3793+
for (int i = 0; i < _singleton._consoleBuffer.Length; i++)
3794+
{
3795+
_singleton._consoleBuffer[i].UnicodeChar = ' ';
3796+
_singleton._consoleBuffer[i].ForegroundColor = Console.ForegroundColor;
3797+
_singleton._consoleBuffer[i].BackgroundColor = Console.BackgroundColor;
3798+
}
3799+
_singleton.Render();
3800+
Console.CursorLeft = 0;
3801+
Console.CursorTop = _singleton._initialY;
3802+
3803+
var ps = PowerShell.Create(RunspaceMode.CurrentRunspace);
3804+
ps.AddCommand("prompt");
3805+
var result = ps.Invoke<string>();
3806+
var strResult = result.Count == 1 ? result[0] : "PS> ";
3807+
Console.Write(strResult);
3808+
3809+
_singleton._consoleBuffer = ReadBufferLines(_singleton._initialY, 1 + _singleton.Options.ExtraPromptLineCount);
3810+
_singleton._buffer.Append(currentBuffer);
3811+
_singleton._current = currentPos;
3812+
_singleton.Render();
3813+
}
3814+
37823815
#endregion Miscellaneous bindable functions
37833816

37843817
private void SetOptionsInternal(SetPSReadlineOption options)

PSReadLine/en-US/about_PSReadline.help.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,12 @@ LONG DESCRIPTION
384384
Turn on demo mode. Displays a window below the input line that shows which
385385
keys are typed.
386386

387+
InvokePrompt (Cmd: unbound Emacs: unbound)
388+
389+
Erases the current prompt and calls the prompt function to redisplay
390+
the prompt. Useful for custom key handlers that change state, e.g.
391+
change the current directory.
392+
387393
WhatIsKey (Cmd: <Alt+?> Emacs: <Alt+?>)
388394

389395
Read a key or chord and display the key binding.

0 commit comments

Comments
 (0)