question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when trying to activate PredictionMode

See original GitHub issue

Hello,

When i try to activate PredictionMode, i ran into an error saying that the mode cannot be activated.

Environment

PSReadline version: 2.1.0
os: 6.3.9600.16384 (winblue_rtm.130821-1623)
PS file version: 10.0.14409.1005 (rs1_srvoob.161208-1155)
HostName: ConsoleHost
BufferWidth: 170
BufferHeight: 500

Exception report

Set-PSReadLineOption : The predictive suggestion feature cannot be enabled because the console output doesn’t support virtual terminal processing or it’s redirected. Au caractère Ligne:1 : 1

  • Set-PSReadLineOption -PredictionSource History
  •   + CategoryInfo          : NotSpecified: (:) [Set-PSReadLineOption], ArgumentException
      + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.SetPSReadLineOption
    
    

Steps to reproduce

Set-PSReadLineOption -PredictionSource History

Expected behavior

Actual behavior

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
BusHerocommented, Jan 16, 2023

A more complex version:

function IsVirtualTerminalProcessingEnabled {
	$MethodDefinitions = @'
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
'@
	$Kernel32 = Add-Type -MemberDefinition $MethodDefinitions -Name 'Kernel32' -Namespace 'Win32' -PassThru
	$hConsoleHandle = $Kernel32::GetStdHandle(-11) # STD_OUTPUT_HANDLE
	$mode = 0
	$Kernel32::GetConsoleMode($hConsoleHandle, [ref]$mode) >$null
	if ($mode -band 0x0004) { # 0x0004 ENABLE_VIRTUAL_TERMINAL_PROCESSING
		return $true
	}
	return $false
}

function CanUsePredictionSource {
	return (! [System.Console]::IsOutputRedirected) -and (IsVirtualTerminalProcessingEnabled)
}

Basically, a powershell version of the check used by PSReadLine here

2reactions
WojciechNagorskicommented, Apr 27, 2023

The profile file should look:

function IsVirtualTerminalProcessingEnabled {
	$MethodDefinitions = @'
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
'@
	$Kernel32 = Add-Type -MemberDefinition $MethodDefinitions -Name 'Kernel32' -Namespace 'Win32' -PassThru
	$hConsoleHandle = $Kernel32::GetStdHandle(-11) # STD_OUTPUT_HANDLE
	$mode = 0
	$Kernel32::GetConsoleMode($hConsoleHandle, [ref]$mode) >$null
	if ($mode -band 0x0004) { # 0x0004 ENABLE_VIRTUAL_TERMINAL_PROCESSING
		return $true
	}
	return $false
}

function CanUsePredictionSource {
	return (! [System.Console]::IsOutputRedirected) -and (IsVirtualTerminalProcessingEnabled)
}

if (CanUsePredictionSource){ 
  Import-Module -Name Terminal-Icons
  Import-Module PSReadLine
  Set-PSReadLineOption -PredictionViewStyle ListView -PredictionSource History -HistoryNoDuplicates
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I set PredictionMode.SSL in antlr4 JavaScript runtime
It suggests a two-stage parsing strategy, first setting the error strategy to BailErrorStrategy and using PredictionMode.
Read more >
Getting Expected Tokens from Error
I tried setting the prediction mode in JavaScript as follows: parser._interp.predictionMode = antlr4.
Read more >
Get help with Windows activation errors
Learn how to troubleshoot Windows activation errors. Browse common activation errors and learn what you can do to fix them.
Read more >
Stable diffusion medvram performance. 512x512 @ 20 steps
I'm just trying to get the hang of it and I was trying to sanity-check my setup by using examples off the Intertubes....
Read more >
PredictionMode (ANTLR 4 Runtime 4.13.0 API)
The SLL(*) prediction mode. This prediction mode ignores the current parser context when making predictions. This is the fastest prediction mode, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found