Skip to content

Support for Windows cmd? #2

Closed
Closed

Description

@Equim-chan

I tried to use it in my project. The code is like:

log.Println(Cyan("Broken Pantsu"), Bold(VERSION))

but when I run it on my Windows cmd shell it prints:

00:39:45.419785 main.go:85: ←[36mBroken Pantsu←[0m ←[1mtest-build←[0m

GOARCH=amd64
GOOS=windows

Activity

  1. logrusorgru commented on May 3, 2017

    @logrusorgru
    Owner

    There are not a Windows support. It supports only ANSI-colors. ANSI-colors are escape codes like \033[31m, \033[0m, etc. For example \033[36m is cyan color.

    There is github.com/mattn/go-colorable package that wraps windows output to print ANSI-colors correct. I'm not sure that it works. Here is a research of someone: image.

    If you use log package (not fmt) then you can use SetOutput method. For example

    package youPackage
    
    import (
        "log"
    
        "github.com/mattn/go-colorable" // colorable
        "github.com/logrusorgru/aurora"
    )
    
    func init() {
        log.SetOutput(colorable.NewColorableStdout()) // or NewColorableStderr()
    }
    
    func main() {
       log.Print(aurora.Cyan("hey!"))
        // stuff
    }

    This way, output of log-functions will be handled by the colorable package and colorized correctly (not sure).
    Feel free to ask if it is not clear enough.


    godoc links:

  2. Equim-chan commented on May 3, 2017

    @Equim-chan
    Author

    I know that's ANSI color, but it just becomes weird when the same trick works fine in other languages.
    For example in Nodejs,

    console.log('\x1b[36mCyan\x1b[0m');  // works, maybe the runtime wrapped them implicitly?

    Tools from MinGW that support color also work well, like ls --color and grep --color.
    Also, my shell is cmder actually, but it runs over cmd.

  3. logrusorgru commented on May 3, 2017

    @logrusorgru
    Owner

    Hm, I need reboot to explore the issue. I think that Nodejs has some internal workaround. A moment.

  4. Equim-chan commented on May 3, 2017

    @Equim-chan
    Author

    I just checked the source code of github.com/mattn/go-colorable; its functions are based on importing kernel32.dll and the corresponding Windows APIs, which doesn't seem to be a graceful workaround but at least works.

  5. logrusorgru commented on May 3, 2017

    @logrusorgru
    Owner

    Windows 7 x64. First output uses the colorable. Second one don't use it. And nodejs output for example.
    screen

  6. Equim-chan commented on May 3, 2017

    @Equim-chan
    Author

    Thanks for the testing. One other question is, does this package replace the strings unconditionally? Does it detect the tty context?

  7. logrusorgru commented on May 3, 2017

    @logrusorgru
    Owner

    Yes, I think. Take a look https://github.com/mattn/go-colorable/blob/master/colorable_windows.go#L88. It uses isatty package

  8. Equim-chan commented on May 3, 2017

    @Equim-chan
    Author

    Well, I mean in your aurora package.

  9. logrusorgru commented on May 3, 2017

    @logrusorgru
    Owner

    No, never.

  10. Equim-chan commented on May 3, 2017

    @Equim-chan
    Author

    Hmm, that's not good when stdout is piped to a log file, which does not need colors.

  11. logrusorgru commented on May 3, 2017

    @logrusorgru
    Owner

    You can to use the isatty package with aurora.NewAurora (see README for example). The aurora package uses another approach to turn on/off colors. This way, you can keep colors if you want (even output is a file).


  12. logrusorgru commented on May 3, 2017

    @logrusorgru
    Owner

    For example (the example works fine)

    package main
    
    import (
    	"log"
    	"os"
    
    	"github.com/logrusorgru/aurora"
    	"github.com/mattn/go-colorable"
    	"github.com/mattn/go-isatty"
    )
    
    // colorizer
    var au aurora.Aurora
    
    func init() {
    	au = aurora.NewAurora(isatty.IsTerminal(os.Stdout.Fd())) // create colorizer
    	log.SetOutput(colorable.NewColorableStdout())
    }
    
    func main() {
    	log.Print(au.Cyan("hey!"))  // use colorizer
    }
  13. logrusorgru commented on May 3, 2017

    @logrusorgru
    Owner

    Also. The colorizer has the same set of methods as the aurora packae. I.e. you can call it aurora instead of au to avoid renaming everywhere.

    package main
    
    import (
    	"log"
    	"os"
    
    	auroraPackage "github.com/logrusorgru/aurora"
    	"github.com/mattn/go-colorable"
    	"github.com/mattn/go-isatty"
    )
    
    var aurora auroraPackage.Aurora
    
    func init() {
    	aurora = auroraPackage.NewAurora(isatty.IsTerminal(os.Stdout.Fd()))
    	log.SetOutput(colorable.NewColorableStdout())
    }
    
    func main() {
    	log.Print(aurora.Cyan("hey!")) // aurora.Cyan as it was
    }

    But. Colors are package-level (BoldFg, BlueBg etc)

  14. logrusorgru commented on May 3, 2017

    @logrusorgru
    Owner

    I just checked the source code of github.com/mattn/go-colorable; its functions are based on importing kernel32.dll and the corresponding Windows APIs, which doesn't seem to be a graceful workaround but at least works.

    If you are worried about the 32, the kernel32.dll is for 32- and 64-bit systems too.

  15. logrusorgru commented on May 4, 2017

    @logrusorgru
    Owner

    About "replace the strings unconditionally". There is some overhead, anyway. For example, if colors disabled Cyan("string") wraps the "string" with some internal struct to get Value interface instead of string. Thus, all overheads of log.Print(Cyan("string")) is "string" -> interface{}(...) -> struct{...} -> fmt.Sprint(...) -> the result used by log.Print. And steps that cost are: (1) conversion to interface{} (2) call of fmt.Sprint.

    May be, the package is not best solution for high performance logs, if colors are switched off. The goal was usability. However, it can be faster then other packages, if colors are switched on.

    Also, the package provided with good set of benchmarks.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址