-
-
Notifications
You must be signed in to change notification settings - Fork 803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use ANSI color codes if SupportsVirtualTerminal #304
Conversation
It's pretty clearly an issue with I have confirmed that the The first hit on "powershell virtual terminal" brings up PowerShell/PowerShell#1177, which references |
Hrm... wondering if @mcoolive, I don't see a license on your blog - can we have permission to use that code? (BTW, your blog gives a certificate error: Thinking implementation would look something like...
|
You are hitting the issue I reported (well, it's my bug) with escape sequences being emitted after running The workaround for now is to set the console mode. It's a simple P/Invoke, I have a sample (that does more than you need) here: https://gist.github.com/lzybkr/f2059cb2ee8d0c13c65ab933b75e998c In my personal |
function Get-ForegroundVirtualTerminalSequence($Color) { | ||
$e = [char]27 + "[" | ||
switch ($Color) { | ||
([ConsoleColor]::Black) { "${e}30m" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use break
, otherwise every case is needlessly tested.
You should also consider ordering the cases so that the more commonly used colors appear first - PowerShell does not generate efficient switch tables so each case is tested before the preceding cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, using an array would be more efficient - indexing into the array will be much faster.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check my latest? (I moved these functions to a different file, too.)
@eweilnau is working on a Write-AnsiHost if you want to go that route (the idea being to accept escaped text and output using write-host for compatibilty) -- he's trying to implement a little more than just colors (i.e. movement). |
ff6c66b
to
55ba9e2
Compare
👍 What would be the preferred way to guard against trying to compile or use that type on non-Windows?
🆒 not sure if it would make sense to take a dependency on PowerLine or copy the relevant bits here. |
c633f94
to
2a99a2b
Compare
@@ -262,7 +273,10 @@ if ($Host.UI.RawUI.BackgroundColor -eq [ConsoleColor]::DarkMagenta) { | |||
$s.WorkingForegroundColor = $s.WorkingForegroundBrightColor | |||
} | |||
|
|||
function Global:Write-VcsStatus { $Global:VcsPromptStatuses | foreach { & $_ } } | |||
function Global:Write-VcsStatus { | |||
Set-ConsoleMode -ANSI |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works! 🎉
We could definitely consider collaborating on a "escape sequences to write-host" translation stuff as a small external LegacyHost module, and PowerLine and Posh-Git could both take dependencies on that, if you like? By the way, I forgot to mention this: I don't just test for SupportsVirtualTerminal, I also explicitly support ConEmu -- because currently, their ANSI support is far more thorough than Windows', and it works way back on old versions of Windows 😁 $Host.UI.SupportsVirtualTerminal -or ($Env:ConEmuANSI -eq "ON") |
Hat tip to @lzybkr
- Enabled if $Host.UI.SupportsVirtualTerminal - Enabled if %ConEmuANSI% is 'ON' - Can be manually set in $PROFILE for other ANSI-capable consoles
12a3655
to
5271273
Compare
👍
Good tip. I've moved the check to the initialization of |
Oh, I like that, I'll add a |
This is what I use for testing for if (($PSVersionTable.PSVersion.Major -le 5) -or $IsWindows) { Cross-platform PowerShell starts at v6. At v6 and after, you can test for the Windows platform via the built-in variable |
Would it be unreasonable to just do something like this early on, to use throughout? $IsWindows = $IsWindows -or ($PSVersionTable.PSVersion.Major -le 5) |
There is also another rub here. It turns out that for IsAdminProcess testing, the required types are available on .NET Core but only on Windows. In other cases, we may want to test specifically for PS Core or PS Desktop. For that, we test first for |
You should be able to just test: |
Speaking of which, we've had a few PRs come in about fixing strict mode errors but I don't really have an opinion. Does the PS community have a convention on modules' handling of strict mode? |
Reopening targeting |
Let me just say:
I can't speak for the community, but I don't see much value in strict mode -- because it largely forces me to give up dynamic scoping. I mean, it's great that it can catch the fact that Who thinks this is more maintainable?:if((Get-Variable IsWindows -Scope Global -ValueOnly -ErrorAction Ignore) -ne $False) { "Yep, it's Windows" } |
From some (very limited) testing I did it seems that |
I tend to agree - if we're going to use a dynamic language, we might as well take advantage of it being dynamic. |
I believe @rkeithhill is correct, although I don't quite know why -- StrictMode is supposed to affect child scopes, and modules are (technically?) in the global scope. But I'll take it 😉 Anyway, bottom line is: don't worry about it. |
I have been nerd-sniped: Jaykul/PowerLine#1.
The notion of
Write-Prompt
either returning astring
(maybe empty) or actually doing aWrite-Host
seems quite wrong, but I'm not sure how else to handle it without breaking users of older hosts. Any ideas, @rkeithhill?Opened as WIP because I'm seeing some weird behavior in the prompt after running Git commands that use the pager. Haven't had a chance to dig into what might be going on there, but I'm open to ideas.
Closes #282
Also, for SEO purposes (because I was surprised that I had to write them myself), herein can be found PowerShell functions to convert
ConsoleColor
to ANSI color escape codes/sequences.