Coder Social home page Coder Social logo

go-colorable's Introduction

go-colorable

Build Status Codecov GoDoc Go Report Card

Colorable writer for windows.

For example, most of logger packages doesn't show colors on windows. (I know we can do it with ansicon. But I don't want.) This package is possible to handle escape sequence for ansi color on windows.

Too Bad!

So Good!

Usage

logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true})
logrus.SetOutput(colorable.NewColorableStdout())

logrus.Info("succeeded")
logrus.Warn("not correct")
logrus.Error("something error")
logrus.Fatal("panic")

You can compile above code on non-windows OSs.

Installation

$ go get github.com/mattn/go-colorable

License

MIT

Author

Yasuhiro Matsumoto (a.k.a mattn)

go-colorable's People

Contributors

alecrabbit avatar aviau avatar cenkalti avatar dolmen avatar filimonov avatar hymkor avatar ikedam avatar koron avatar magicshui avatar mattn avatar mislav avatar naoyukis avatar ncw avatar radeksimko avatar rbtnn avatar secdre4mer avatar segrey avatar tklauser avatar toshimaru avatar tyru avatar uji avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-colorable's Issues

Windows colored output is mixed up when running in cmd.exe

Windows 10 (version 2004)
go version go1.14.2 windows/amd64

Steps to reproduce:

  1. Create "mixed-output.go"
package main

import (
    "fmt"
    "github.com/mattn/go-colorable"
    "sync"
)

func main() {
    stdout := colorable.NewColorableStdout()
    wg := &sync.WaitGroup{}
    for i := 0; i < 3; i++ {
       wg.Add(1)
       go func() {
           _, _ = fmt.Fprintln(stdout, "\u001b[32mHello\u001b[0m", "World")
           wg.Done()
       }()
    }
    wg.Wait()
}
  1. Open Command Prompt (cmd.exe) and cd to file's parent folder.
  2. go build mixed-output.go
  3. Run mixed-output.exe, the output is mixed.

Mixed output:
image

Works fine in Git bash (MinGW):
image

feature request: enable virtual console terminal processing on windows

As of the newer Windows version you can enable virtual terminal processing which provides control cursor movement, color/font mode, and other operations when written to the output stream.

This can be enabled via GetConsoleMode and other windows c api functions.

An example of how V does it:

C.SetConsoleMode(C.GetStdHandle(C.STD_OUTPUT_HANDLE), C.ENABLE_PROCESSED_OUTPUT | 0x0004) // enable_virtual_terminal_processing
    C.SetConsoleMode(C.GetStdHandle(C.STD_ERROR_HANDLE), C.ENABLE_PROCESSED_OUTPUT | 0x0004) // enable_virtual_terminal_processing
    unsafe {
        C.setbuf(C.stdout, 0)
        C.setbuf(C.stderr, 0)
    }

This feature is very handy for CLI Makers who want to target Windows.

PS: If you don't have time to figure it out then I can trying doing it and then PR it.

cannot find module providing package gopkg.in/mattn/go-colorable.v0

I've used Kubebuilder to generate a bunch of code for a Kubernetes custom resource controller. I'd like my custom resource controller to manage BOSH releases so I've got code similar to this to construct a BOSH Director client to pass to my reconciliation controller so it can use it to manage BOSH releases. After adding this code to some of the Kubebuilder-generated code, and running make generate from the Kubebuilder-generated Makefile, it fails when I run go fmt ./....

$ go fmt ./...
go: downloading gopkg.in/mattn/go-colorable.v0 v0.1.2
go: downloading gopkg.in/mattn/go-isatty.v0 v0.0.8
build github.com/amitkgupta/boshv3: cannot load gopkg.in/mattn/go-colorable.v0: cannot find module providing package gopkg.in/mattn/go-colorable.v0

I'm not importing go-colorable directly anywhere in my code, my guess is some of the packages from github.com/cloudfoundry/bosh-cli that I'm importing have some recursive dependency on go-colorable, so I don't think I can solve this by modifying any import paths in my own code.

Any pointers on how I might troubleshoot this? I saw #35 but none of the ideas there seemed to help.

Go modules bug

When trying to go get go-colorable from gopkg, I'm currently getting this message (using Go 1.12):

go get -u gopkg.in/mattn/go-colorable.v0
go: gopkg.in/mattn/[email protected]: go.mod has non-....v0 module path "github.com/mattn/go-colorable" at revision v0.1.1
go get: error loading module requirements

It seems to me like there is some kind of conflict between the gopkg.in/mattn/go-colorable.v0 actual package path and the name of the module declared in the go.mod file github.com/mattn/go-colorable.

For now, forcing the use of the lib without the go.mod file in it solves the issue:

go get -u gopkg.in/mattn/[email protected]
go: finding gopkg.in/mattn/go-colorable.v0 v0.1.0
go: downloading gopkg.in/mattn/go-colorable.v0 v0.1.0
go: extracting gopkg.in/mattn/go-colorable.v0 v0.1.0

Use in logrus

Hey Mattn, I like this workaround for Windows. Instead of documenting it as a workaround in the README, could we use this smartly upstream without downloading the library for all platforms?

Cross-compile dependency issue

There is a subtle problem with cross-compilation for windows under linux/darwin.

  1. go get github.com/mattn/go-colorable being run under linux/darwin skips dependencies in file colorable_windows.go, because of "_windows" prefix. This file though has one external dependency - 'github.com/mattn/isatty', which is ignored and isn't downloaded during go get.
  2. If user want to cross-compile package for windows, go build will return error as it has missing dependency:
    $ GOOS=windows go build github.com/mattn/go-colorable colorable_windows.go:13:2: cannot find package "github.com/mattn/go-isatty" in any of: /usr/local/go/src/github.com/mattn/go-isatty (from $GOROOT) /Users/divan/src/github.com/mattn/go-isatty (from $GOPATH)
  3. Being a general purpose library, it's imported from many more projects, gin-gonic among them.
    It ends with users confusion, when he tries to cross-compile package, which depends on go-colorable and it takes time to figure out this issue.

My proposal is to import 'github.com/mattn/isatty' into colorable_others.go as well to allow go get resolve it as dependency regardless of which platform is used.
+ _ "github.com/mattn/go-isatty"

Let me know mean of comment in colorable_test.go

Let me know this comment's mean.
// TODO move colorable wrapping outside the test

colorable_test.go:19

func checkEncoding(t *testing.T, data []byte) {
	// Send non-UTF8 data to colorable
	b := bytes.NewBuffer(make([]byte, 0, 10))
	if b.Len() != 0 {
		t.FailNow()
	}
	// TODO move colorable wrapping outside the test
	NewNonColorable(b).Write(data)
	if b.Len() != len(data) {
		t.Fatalf("%d bytes expected, got %d", len(data), b.Len())
	}
}

I wanna try this TODO.

Add SetConsoleTitle support to windows

I have

title:= \033]0;abcd\007

use

fmt.Print(title)

On linux(or mac) os to set the Terminal Title to "abcd".but on windows os is don't work.
Windows Console use "SetConsoleTitle" API to do it.

Sorry,my English are too bad.:)

Messed ANSI sequences in terminal output (Windows, cmd.exe, PowerShell) instead of colors

This ticket originates in research done for epinio/epinio/issues/673, itself a follow up of epinio/epinio/issues/494

The relevant setup is

  • Platform: Win 10 Home (Virtual Machine, Linux-hosted, VirtualBox)
  • Modules (grep 'fatih\|mattn' go.mod):
    • github.com/fatih/color v1.12.0
    • github.com/mattn/go-isatty v0.0.12
  • Sum Modules (grep 'fatih\|mattn' go.sum |grep -v go.mod):
    • github.com/fatih/color v1.12.0 h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc=
    • github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
    • github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
    • github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
      Without excluding go.mod lines I see a lot more versions for all the packages.

Below a screenshot demonstrating the differences between a mingw bash (from git for windows) on one side, and cmd.exe and PowerShell on the other side:
Screenshot

While I originally thought that maybe the IsTerminal generates an incorrect result, disabling handling with the output from my last tests I now have to suspect an issue either here in go-colorable, or in its user, https://github.com/fatih/color, which is the actual package used by the epinio code. And I do not see how our use of the fatih/color would be incorrect.

With regard to the version numbers, fatih/color is at 1.12.0, the latest release as of May 24 this year.
Which in turn uses the latest tag 0.1.8 for go-colorable, as of Oct 3 last year.

Any ideas where else to look for the issue ?

Write escaped text to file

Is it somehow possible to write the escaped text to a file? I found colorable.NewColorable(file *os.File) but I was not able to get the output escaped.

Please tag on the latest commit

On the working directory for my product which imports go-colorable,

set GO111MODULE=on
go get -u

go-colorable v0.0.9 is downloaded, but it is too old (commited on 2017) and has some bugs not fixed.

Would you tag the latest commit ?

(For now, I got the latest revision by go get -m github.com/mattn/go-colorable@efa589957cd060542a26d2dd7832fd6a6c6c3ade )

Needs a license

Hi, thanks for this useful project!

Could go-colorable have an open-source license so that we may include, distribute, and modify this project?

Adding a license is easy: just to go the root of the project in the web browser, click "Create new file" button, name the new file something like LICENSE.md and the license picker dropdown will appear, letting you pre-populate the license file with text of one of the most popular open source licenses.

Thanks!

Move to start of line doesn't work if you give it 0

I've been using \x1b[0G to mean move to start of line.

This is actually wrong, it should be \x1b[1G.

However this works in all the terminals I've tried so maybe go-colorable should support it too?

Let me know if you agree and if you'd like me to send a patch.

Thanks

Aliases for save/restore cursor position

Beside ANSI codes <esc>[s (save cursor position) and <esc>[u (restore cursor position) there are also widely used DEC code: <esc>7 and <esc>8 which do mostly the same, but are preferred sometimes because <esc>[s and <esc>[u don't work in some terminals like VT100 and Mosh (See mobile-shell/mosh#726)

See also:
"Some terminals require the DEC sequences ESC 7 / ESC 8 instead which is more widely supported." See
https://en.wikipedia.org/wiki/ANSI_escape_code#Examples

See also:
https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#span-idsimplecursorpositioningspanspan-idsimplecursorpositioningspanspan-idsimplecursorpositioningspansimple-cursor-positioning

https://vt100.net/docs/vt100-ug/chapter3.html#DECSC

Can you add such an alias?

Crash when using from "vagrant ssh windows"

When using from an ssh session using vagrant into a windows, then it crashes with in "colorable_windows.go" line 507

https://github.com/mattn/go-colorable/blob/master/colorable_windows.go#L507

image

image

It does not happen all the time but feels uncomfortable to have this in prod.
Steps:

  1. be on linux
  2. open terminal and vagrant ssh windows
  3. do some high intensive outputs , like animation with colors, I use the spinner as you can see from screenshots

Something clashes there

Cursor movement commands don't default to 1

According to my reading a cursor movment such as \x1b[A should be interpreted as \x1b[1A so the default movement is 1 character. This is obeyed by xterms I've tested.

However \x1b[A is ignored by go-colorable because of this code

case 'A':
n, err = strconv.Atoi(buf.String())
if err != nil {
continue
}

I think this problem affects A, B, C, D

Let me know if you would like me to send a patch to fix this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.