Skip to content

Commit 9273a7e

Browse files
committed
Fix sample for strict mode+better custom handler error msg
Fixes #304
1 parent 0879b08 commit 9273a7e

File tree

5 files changed

+63
-19
lines changed

5 files changed

+63
-19
lines changed

PSReadLine/Options.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,31 @@ public static PSConsoleReadlineOptions GetOptions()
243243
return _singleton._options;
244244
}
245245

246+
class CustomHandlerException : Exception
247+
{
248+
internal CustomHandlerException(Exception innerException)
249+
: base("", innerException)
250+
{
251+
}
252+
}
253+
246254
/// <summary>
247255
/// Helper function for the Set-PSReadlineKeyHandler cmdlet.
248256
/// </summary>
249257
public static void SetKeyHandler(string[] key, ScriptBlock scriptBlock, string briefDescription, string longDescription)
250258
{
251-
Action<ConsoleKeyInfo?, object> handler =
252-
(k, arg) => scriptBlock.Invoke(k, arg);
259+
Action<ConsoleKeyInfo?, object> handler = (k, arg) =>
260+
{
261+
try
262+
{
263+
scriptBlock.Invoke(k, arg);
264+
}
265+
catch (Exception e)
266+
{
267+
throw new CustomHandlerException(e);
268+
}
269+
};
270+
253271
_singleton.SetKeyHandlerInternal(key, handler, briefDescription, longDescription, scriptBlock);
254272
}
255273

PSReadLine/PSReadLineResources.Designer.cs

Lines changed: 9 additions & 0 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
@@ -714,4 +714,7 @@ If there are other parse errors, unresolved commands, or incorrect parameters, s
714714
Or not saving history with:
715715
Set-PSReadlineOption -HistorySaveStyle SaveNothing</value>
716716
</data>
717+
<data name="OopsCustomHandlerException" xml:space="preserve">
718+
<value>An exception occurred in custom key handler, see $error for more information: {0}</value>
719+
</data>
717720
</root>

PSReadLine/ReadLine.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ private bool BreakHandler(ConsoleBreakSignal signal)
251251
/// <returns>The complete command line.</returns>
252252
public static string ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics)
253253
{
254-
var console1 = _singleton._console;
255-
_singleton._prePSReadlineConsoleMode = console1.GetConsoleInputMode();
254+
var console = _singleton._console;
255+
_singleton._prePSReadlineConsoleMode = console.GetConsoleInputMode();
256256
bool firstTime = true;
257257
while (true)
258258
{
@@ -265,11 +265,11 @@ public static string ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsi
265265
// ENABLE_MOUSE_INPUT - mouse events
266266
// ENABLE_WINDOW_INPUT - window resize events
267267
var mode = _singleton._prePSReadlineConsoleMode &
268-
~(NativeMethods.ENABLE_PROCESSED_INPUT |
269-
NativeMethods.ENABLE_LINE_INPUT |
270-
NativeMethods.ENABLE_WINDOW_INPUT |
271-
NativeMethods.ENABLE_MOUSE_INPUT);
272-
console1.SetConsoleInputMode(mode);
268+
~(NativeMethods.ENABLE_PROCESSED_INPUT |
269+
NativeMethods.ENABLE_LINE_INPUT |
270+
NativeMethods.ENABLE_WINDOW_INPUT |
271+
NativeMethods.ENABLE_MOUSE_INPUT);
272+
console.SetConsoleInputMode(mode);
273273

274274
if (firstTime)
275275
{
@@ -288,6 +288,19 @@ public static string ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsi
288288
{
289289
return "exit";
290290
}
291+
catch (CustomHandlerException e)
292+
{
293+
var oldColor = console.ForegroundColor;
294+
console.ForegroundColor = ConsoleColor.Red;
295+
console.WriteLine(
296+
string.Format(CultureInfo.CurrentUICulture, PSReadLineResources.OopsCustomHandlerException, e.InnerException.Message));
297+
console.ForegroundColor = oldColor;
298+
299+
var lineBeforeCrash = _singleton._buffer.ToString();
300+
_singleton.Initialize(runspace, _singleton._engineIntrinsics);
301+
InvokePrompt();
302+
Insert(lineBeforeCrash);
303+
}
291304
catch (Exception e)
292305
{
293306
// If we're running tests, just throw.
@@ -300,7 +313,6 @@ public static string ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsi
300313
{
301314
e = e.InnerException;
302315
}
303-
var console = console1;
304316
var oldColor = console.ForegroundColor;
305317
console.ForegroundColor = ConsoleColor.Red;
306318
console.WriteLine(PSReadLineResources.OopsAnErrorMessage1);
@@ -318,18 +330,17 @@ public static string ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsi
318330
// Make it a little easier to see the keys
319331
sb.Append('\n');
320332
}
321-
// TODO: print non-default function bindings and script blocks
322333
}
323334

324335
console.WriteLine(string.Format(CultureInfo.CurrentUICulture, PSReadLineResources.OopsAnErrorMessage2, _lastNKeys.Count, sb, e));
325336
var lineBeforeCrash = _singleton._buffer.ToString();
326-
_singleton.Initialize(runspace, _singleton._engineIntrinsics);
337+
_singleton.Initialize(runspace, _singleton._engineIntrinsics);
327338
InvokePrompt();
328339
Insert(lineBeforeCrash);
329340
}
330341
finally
331342
{
332-
console1.SetConsoleInputMode(_singleton._prePSReadlineConsoleMode);
343+
console.SetConsoleInputMode(_singleton._prePSReadlineConsoleMode);
333344
}
334345
}
335346
}

PSReadLine/SamplePSReadlineProfile.ps1

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,16 @@ Set-PSReadlineKeyHandler -Key Backspace `
192192
if ($cursor -gt 0)
193193
{
194194
$toMatch = $null
195-
switch ($line[$cursor])
195+
if ($cursor -lt $line.Length)
196196
{
197-
<#case#> '"' { $toMatch = '"'; break }
198-
<#case#> "'" { $toMatch = "'"; break }
199-
<#case#> ')' { $toMatch = '('; break }
200-
<#case#> ']' { $toMatch = '['; break }
201-
<#case#> '}' { $toMatch = '{'; break }
197+
switch ($line[$cursor])
198+
{
199+
<#case#> '"' { $toMatch = '"'; break }
200+
<#case#> "'" { $toMatch = "'"; break }
201+
<#case#> ')' { $toMatch = '('; break }
202+
<#case#> ']' { $toMatch = '['; break }
203+
<#case#> '}' { $toMatch = '{'; break }
204+
}
202205
}
203206

204207
if ($toMatch -ne $null -and $line[$cursor-1] -eq $toMatch)

0 commit comments

Comments
 (0)