Skip to content

We need a way to customize output color for error/warning, etc #651

Closed

Description

@Jaykul

Specifically, in PowerShell.exe we have the ability to change the color used for errors, warnings, etc.:

C:\PS> $Host.PrivateData                                                    
                                      
ErrorForegroundColor    : DarkRed     
ErrorBackgroundColor    : Black       
WarningForegroundColor  : Yellow      
WarningBackgroundColor  : Black       
DebugForegroundColor    : Green       
DebugBackgroundColor    : Black       
VerboseForegroundColor  : Cyan        
VerboseBackgroundColor  : Black       
ProgressForegroundColor : DarkMagenta 
ProgressBackgroundColor : Gray        

In VSCode I can change the colors of the 16 ANSI colors, but I can't find a way to change which of them you are using to write errors out.

Activity

  1. changed the title [-]We need a way to customize colors[/-] [+]We need a way to customize output color for error/warning, etc[/+] on Apr 10, 2018
  2. TylerLeonhardt commented on Apr 10, 2018

    @TylerLeonhardt
    Member

    Thanks Joel! I reasonable request indeed.

  3. SeeminglyScience commented on Apr 11, 2018

    @SeeminglyScience
    Collaborator

    If anyone is interested in picking this up the general idea is to

    1. Port ConsoleColorProxy and the PrivateData member from ConsoleHost in pwsh to EditorServicesPSHost.PrivateData

    2. Change the Write* methods in EditorServicesPSHostUserInterface to use the new colors

  4. KeroroLulu commented on Apr 17, 2018

    @KeroroLulu
    Contributor

    I would like to work on it ! Can I be assign ?

  5. TylerLeonhardt commented on Apr 18, 2018

    @TylerLeonhardt
    Member

    B.G. (@KeroroLulu) you can work on any issue at any time you'd like :)

    We don't usually 'assign' work. You can freely start and submit a Pull Request when you're ready to get feedback!

    Once the PR looks good, we will merge it in!

  6. KeroroLulu commented on Apr 18, 2018

    @KeroroLulu
    Contributor

    @tylerl0706 Thank you :)

    I'm not used to stuff like that, so if I have any questions I'll ask you !

  7. TylerLeonhardt commented on Apr 18, 2018

    @TylerLeonhardt
    Member

    You can always ask questions too!

    Let me know if you have any 👍

  8. KeroroLulu commented on Apr 18, 2018

    @KeroroLulu
    Contributor

    @tylerl0706 In the instructions

    Port ConsoleColorProxy and the PrivateData member from ConsoleHost in pwsh to EditorServicesPSHost.PrivateData

    The link for EditorServicesPSHost.PrivateData redirect here, I should create this file and copy/past ConsoleColorProxy class and PrivateData member in it, or this file already exist ?

    Edit

    Nevermind I just found PrivateData. Sorry but my english comprehension is not perfect but what do you mean by "Port ConsoleColorProxy ..." ? I guess that it's not just a copy/past, right ?

  9. TylerLeonhardt commented on Apr 18, 2018

    @TylerLeonhardt
    Member

    No problem :) Port usually means "take that and add it to this" sometimes it does mean actually copy/paste. However I think it's a little bit more work than copy/paste.

    First copy paste those classes over into EditorServicesPSHost.PrivateData, then try to build and see what you need to fix to get those classes to work in PSES(PowerShell Editor Services).

    Then, follow Patrick Meinecke (@SeeminglyScience)'s next step and Change the Write* methods in EditorServicesPSHostUserInterface to use the new colors.

  10. KeroroLulu commented on Apr 18, 2018

    @KeroroLulu
    Contributor

    @tylerl0706 Ok ! But I have to copy ConsoleColorProxy class into the override or above like in ConsoleHost ?

    Edit

    I can't use ConsoleHostUserInterface due to his level of restriction I tried using Microsoft.PowerShell.EditorServices.Console; but it doesn't work. Which import should I use to prevent this error ?

  11. TylerLeonhardt commented on Apr 18, 2018

    @TylerLeonhardt
    Member

    What was the error that you got?

  12. KeroroLulu commented on Apr 18, 2018

    @KeroroLulu
    Contributor

    @tylerl0706 My powershell is in french but by translating we got :

    Session/Host/EditorServicesPSHost.cs(89,38): error CS0122: 'ConsoleHostUserInterface' is unreachable because of its level of protection

    I think that's because ConsoleHostUserInterface is in Powershell and not in Powershell Editor Service

  13. 1 remaining item

  14. KeroroLulu commented on Apr 19, 2018

    @KeroroLulu
    Contributor

    @tylerl0706 The problem is that EditorServicesPSHostUserInterface doesn't contain any color method so when I replace ConsoleHostUserInterface I have this error :

    Session/Host/EditorServicesPSHost.cs(99,45): error CS1061: 'EditorServicesPSHostUserInterface' doesn't contain any definition for 'ErrorForegroundColor' and no extension method 'ErrorForegroundColor' accepting first argument of type 'EditorServicesPSHostUserInterface' has been found

    Should I use WriteErrorLine instead of ErrorForegroundColoror ErrorBackgroundColor?

    For example

    public ConsoleColor ErrorBackgroundColor
                {
                    [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
                    get
                    { return _hostUserInterface.Write.backgroundColor; }
                    [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
                    set
                    { _hostUserInterface.Write.backgroundColor = value; }
                }
    

    something like that ?
    (Sorry for asking so many questions ^^")

  15. TylerLeonhardt commented on Apr 19, 2018

    @TylerLeonhardt
    Member
  16. TylerLeonhardt commented on Apr 19, 2018

    @TylerLeonhardt
    Member

    And then don't forget to edit these to use those values instead of the hardcoded ones 🙂

    Ask all the questions you need. You've got this 💪 you're going to fix this issue 😃

  17. KeroroLulu commented on Apr 19, 2018

    @KeroroLulu
    Contributor

    @tylerl0706 Console.BackgroundColor doesn't exist in Microsoft.PowerShell.EditorServices.Console. Where is BackgroundColor exactly ? I will add it to Console.

    Edit

    I found it I think it's

    public static ConsoleColor BackgroundColor
            {
                get;
                set;
            }
    

    Tell me if I'm wrong. I added it to EditorServicesPSHostUserInterface

    Edit 2

    I changed all the WriteOutput method for Debug, Error, Verbose and Warning :

    public override void WriteDebugLine(string message)
            {
                this.WriteOutput(
                    DebugMessagePrefix + message,
                    true,
                    OutputType.Debug,
                    foregroundColor: this.DebugForegroundColor,
                    backgroundColor: this.DebugBackgroundColor);
            }
    

    tell me if I did nothing wrong with the Invoke-Build there's no error. I would like to test it but I don't really know how.

    And finally WriteProgress I don't know if I have to change something for the color or if it's ok.

  18. TylerLeonhardt commented on Apr 19, 2018

    @TylerLeonhardt
    Member

    That's great B.G. (@KeroroLulu)!

    Here's what you can do to test:
    Right now, the best way to do this is by building and launching the vscode powershell extension.

    Follow these instructions to test your code out:
    https://github.com/PowerShell/vscode-powershell/blob/master/docs/development.md

  19. KeroroLulu commented on Apr 19, 2018

    @KeroroLulu
    Contributor

    @tylerl0706 Should I add Unit Test to my code ?

    Edit

    I'm on mac and I'm trying to launch the extension with F5, it propose me several process to attach my extension (zsh, adobe, cloud, etc...) which one should I use ?

    Edit 2

    I did it somehow by using code-insiders --extensionDevelopmentPath="/Users/My/vscode-powershell" but then I don't know how it works exactly I tried to change some background value for warning but it change nothing so I don't know if this is the code or the way I test it.

  20. TylerLeonhardt commented on Apr 19, 2018

    @TylerLeonhardt
    Member

    B.G. (@KeroroLulu) does $Host.PrivateData give you anything?

  21. KeroroLulu commented on Apr 19, 2018

    @KeroroLulu
    Contributor

    It gives me :

    ErrorForegroundColor    : Red
    ErrorBackgroundColor    : -1
    WarningForegroundColor  : Yellow
    WarningBackgroundColor  : -1
    DebugForegroundColor    : Yellow
    DebugBackgroundColor    : -1
    VerboseForegroundColor  : Yellow
    VerboseBackgroundColor  : -1
    ProgressForegroundColor : Yellow
    ProgressBackgroundColor : DarkCyan
    

    I just tested it by deleting the message but it's just my way to test it that doesn't work. How can I print warning or error by using PWSH Editor Services ?

  22. TylerLeonhardt commented on Apr 19, 2018

    @TylerLeonhardt
    Member

    If you change $Host.PrivateData.ErrorForegroundColor in the integrated terminal to DarkCyan, and do a:

    Write-Error "foo"

    does it still print in red or dark cyan?

    Note this will only work if you made the correct changes to the Write* function here:

    to use the this.ErrorForegroundColor (I think it's called) instead of "Red"

    You're getting there! 😄

  23. KeroroLulu commented on Apr 19, 2018

    @KeroroLulu
    Contributor

    @tylerl0706 Well I changed it in EditorServicesPSHostInterface

    public ConsoleColor ErrorForegroundColor { get; set; } = ConsoleColor.DarkCyan;
    

    But it doesn't change in my pwsh

    Here's my WriteOutput

    public override void WriteErrorLine(string value)
            {
                this.WriteOutput(
                    value,
                    true,
                    OutputType.Error,
                    foregroundColor: this.ErrorForegroundColor,
                    backgroundColor: this.ErrorBackgroundColor);
            }
    

    I did my Invoke-Build Build and there's no error

    Edit

    It's really strange, I change my WriteOutput

    public override void WriteErrorLine(string value)
            {
                this.WriteOutput(
                    value,
                    true,
                    OutputType.Error,
                    foregroundColor: ConsoleColor.DarkCyan,
                    backgroundColor: this.ErrorBackgroundColor);
            }
    

    But my error foreground color is still red !

    Edit 2

    Nevermind !! It works that was just a little problem from my installation :)

    I will do the Pull Request !

  24. TylerLeonhardt commented on Apr 20, 2018

    @TylerLeonhardt
    Member

    That's great B.G. (@KeroroLulu)!! 🎉 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions