shiena / ansicolor Goto Github PK
View Code? Open in Web Editor NEWAnsicolor library provides color console in Windows as ANSICON for Golang.
Home Page: https://pkg.go.dev/github.com/shiena/ansicolor
License: MIT License
Ansicolor library provides color console in Windows as ANSICON for Golang.
Home Page: https://pkg.go.dev/github.com/shiena/ansicolor
License: MIT License
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.
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])
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:
-> No Problem
-> The Problem is that Blue's B
is not displayed.
Not sure whether it is a Windows issue or ansicolor issue.
Light yellow and white works fine, but yellow broke.
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"))
}
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.
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
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
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
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.