Giter VIP home page Giter VIP logo

ansicolor's Introduction

PkgGoDev Go Report Card

ansicolor

Ansicolor library provides color console in Windows as ANSICON for Golang.

Features

Escape sequence Text attributes
\x1b[0m All attributes off(color at startup)
\x1b[1m Bold on(enable foreground intensity)
\x1b[4m Underline on
\x1b[5m Blink on(enable background intensity)
\x1b[21m Bold off(disable foreground intensity)
\x1b[24m Underline off
\x1b[25m Blink off(disable background intensity)
Escape sequence Foreground colors
\x1b[30m Black
\x1b[31m Red
\x1b[32m Green
\x1b[33m Yellow
\x1b[34m Blue
\x1b[35m Magenta
\x1b[36m Cyan
\x1b[37m White
\x1b[39m Default(foreground color at startup)
\x1b[90m Light Gray
\x1b[91m Light Red
\x1b[92m Light Green
\x1b[93m Light Yellow
\x1b[94m Light Blue
\x1b[95m Light Magenta
\x1b[96m Light Cyan
\x1b[97m Light White
Escape sequence Background colors
\x1b[40m Black
\x1b[41m Red
\x1b[42m Green
\x1b[43m Yellow
\x1b[44m Blue
\x1b[45m Magenta
\x1b[46m Cyan
\x1b[47m White
\x1b[49m Default(background color at startup)
\x1b[100m Light Gray
\x1b[101m Light Red
\x1b[102m Light Green
\x1b[103m Light Yellow
\x1b[104m Light Blue
\x1b[105m Light Magenta
\x1b[106m Light Cyan
\x1b[107m Light White

Example

package main

import (
	"fmt"
	"os"

	"github.com/shiena/ansicolor"
)

func main() {
	w := ansicolor.NewAnsiColorWriter(os.Stdout)
	text := "%sforeground %sbold%s %sbackground%s\n"
	fmt.Fprintf(w, text, "\x1b[31m", "\x1b[1m", "\x1b[21m", "\x1b[41;32m", "\x1b[0m")
	fmt.Fprintf(w, text, "\x1b[32m", "\x1b[1m", "\x1b[21m", "\x1b[42;31m", "\x1b[0m")
	fmt.Fprintf(w, text, "\x1b[33m", "\x1b[1m", "\x1b[21m", "\x1b[43;34m", "\x1b[0m")
	fmt.Fprintf(w, text, "\x1b[34m", "\x1b[1m", "\x1b[21m", "\x1b[44;33m", "\x1b[0m")
	fmt.Fprintf(w, text, "\x1b[35m", "\x1b[1m", "\x1b[21m", "\x1b[45;36m", "\x1b[0m")
	fmt.Fprintf(w, text, "\x1b[36m", "\x1b[1m", "\x1b[21m", "\x1b[46;35m", "\x1b[0m")
	fmt.Fprintf(w, text, "\x1b[37m", "\x1b[1m", "\x1b[21m", "\x1b[47;30m", "\x1b[0m")
}

screenshot

See also:

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

ansicolor's People

Contributors

cxfksword avatar johnschnake avatar parkghost avatar shiena avatar techtonik 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

ansicolor's Issues

could not import

I try to use the code, but get the following error
go get github.com/shiena/ansicolor
can't load package: package github.com/shiena/ansicolor: no buildable Go source
files in xxxx\github.com\shiena\ansicolor

Expose ResetColor() and SetColor()

Hi @shiena

Thanks for this wonderful library. I'm curious if there is way to expose methods in the form of

ResetColor()
SetColor(atr ...Attribute)

So we could use it with conjunction :

SetColor("\x1b[30m", "\x1b[41m") // Black foreground, red background
fmt.Println("This text will be now in red background with black foregoreund)
fmt.Printf("This too!\n")
ResetColor() 

It would be very useful to change existing by wrapping it.

This library uses something similar: https://github.com/daviddengcn/go-colortext

I also use something similar but it doesn't have any windows support: https://github.com/fatih/color#plug-into-existing-code

panic: index out of range

I got the following panic from within this library:

panic: runtime error: slice bounds out of range

goroutine 21983 [running]:
myApp/vendor/github.com/shiena/ansicolor.(*ansiColorWriter).Write(0xc0421c2000, 0xc04f2485b0, 0xc1, 0xd0, 0xd0, 0x0, 0x0)
        myApp/vendor/github.com/shiena/ansicolor/ansicolor_windows.go:384 +0x52b
myApp/vendor/github.com/sirupsen/logrus.(*Entry).write(0xc04277e060)
        myApp/vendor/github.com/sirupsen/logrus/entry.go:247 +0x158
myApp/vendor/github.com/sirupsen/logrus.Entry.log(0xc0421cc6c0, 0xc0577a6900, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        myApp/vendor/github.com/sirupsen/logrus/entry.go:219 +0x1c8
myApp/vendor/github.com/sirupsen/logrus.(*Entry).Log(0xc0421b9e60, 0xc000000005, 0xc057793d70, 0x1, 0x1)
        myApp/vendor/github.com/sirupsen/logrus/entry.go:256 +0xbe
myApp/vendor/github.com/sirupsen/logrus.(*Entry).Logf(0xc0421b9e60, 0x5, 0xa889c1, 0x5f, 0xc042047440, 0x4, 0x4)
        myApp/vendor/github.com/sirupsen/logrus/entry.go:301 +0xe6
myApp/vendor/github.com/sirupsen/logrus.(*Entry).Debugf(0xc0421b9e60, 0xa889c1, 0x5f, 0xc042047440, 0x4, 0x4)
        myApp/vendor/github.com/sirupsen/logrus/entry.go:309 +0x6c
...

The offending line is nw, err = cw.w.Write(p[first:last])

Determine correct window handle

When setting the terminal color, ansicolor uses the file handle syscall.Stdout for the windows system calls.

In my use case, this is not sufficient. I am compiling my go application as a windows GUI application (no terminal) and, if the command-line argument "-console" is provided, I allocate a new console.

I'm using the following syscalls for that:

procAttachConsole = kernel32.MustFindProc("AttachConsole")
procAllocConsole  = kernel32.MustFindProc("AllocConsole")
procFreeConsole   = kernel32.MustFindProc("FreeConsole")

As a result, the syscall.stdout handle (=1) is not correct, but there is no way to configure another window handle.

Unknown escape sequences cause short write

Problem was discovered when using golangs ssh package. If an application tries to output escape sequences not recognized by ansicolor (e.g. if $TERM=xterm and you run top) then a short write error occurs and the stdout pipe would break (though the connection and stdin pipe still function).

Preferably, the unrecognized sequences would just be placed into the buffer untouched.

Wrong background yellow on Windows 10 1903

Not sure whether it is a Windows issue or ansicolor issue.
Light yellow and white works fine, but yellow broke.
image

package main

import (
	"io"
	"os"
	"sync"

	"github.com/shiena/ansicolor"
)

type logWriter struct {
	sync.Mutex
	writer io.Writer
}

func newLogWriter(wr io.Writer) *logWriter {
	return &logWriter{writer: wr}
}

func main() {
	w := newLogWriter(ansicolor.NewAnsiColorWriter(os.Stdout))
	w.writer.Write([]byte("\x1b[43m Yellow"))
	w.writer.Write([]byte("\x1b[103m Light Yellow"))
	w.writer.Write([]byte("\x1b[47m White \x1b[0m"))
}

When parameter has characters which current console can not display, the next one character disappears

At printing Japanese letter on chcp 65001, they are not displayed because the font is not enough. It is not problem. But, on printing next string, the first character is not displayed though it is alphabet.

(Japanese: chcp 65001 で、日本語を出そうとすると、フォントの関係で日本語が表示されません。それ自体は当然なのですが、それに続く次回の出力で表示可能なはずの1文字が欠けてしまう問題を確認しました。表示できない文字列が入力で入ってくる以上、正常動作できなくて当然ですが、もし可能であれば何かしら対応いただけると幸いです)

package main

import (
        "fmt"
        "os"

        "github.com/shiena/ansicolor"
)

func main() {
        w := ansicolor.NewAnsiColorWriter(os.Stdout)
        fmt.Fprint(w, "Green(\x1B[32mみどり\x1b[0m)")
        fmt.Fprint(w, "Blue(\x1B[34mあお\x1B[0m)")
        fmt.Fprint(w, "\n")
}

// vim:set fenc=utf8:

chcp-932

-> No Problem

chcp-65001

-> The Problem is that Blue's B is not displayed.

Release?

Any chance you could take a SemVer based release for those of us who use versions? Something like 1.0.0 or v1.0.0.

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.