shiena / ansicolor

Ansicolor library provides color console in Windows as ANSICON for Golang.
https://pkg.go.dev/github.com/shiena/ansicolor
MIT License
229 stars 40 forks source link

Unknown escape sequences cause short write #7

Closed johnSchnake closed 9 years ago

johnSchnake commented 9 years ago

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.

shiena commented 9 years ago

@johnSchnake I made the PR #8 Is the problem still remains?

fatih commented 9 years ago

@shiena is there anything I should be aware for the color (https://github.com/fatih/color) package?

shiena commented 9 years ago

@fatih Maybe, there is no problem. The following is a simple difference. Please note the divided escape sequence and the non-color escape sequence.

package main

import (
    "fmt"
    "io"
    "os"

    "github.com/shiena/ansicolor"
)

func main() {
    outputEscseq(ansicolor.NewAnsiColorWriter(os.Stdout))
    outputEscseq(ansicolor.NewModeAnsiColorWriter(os.Stdout, ansicolor.OutputNonColorEscSeq))
}

func outputEscseq(w io.Writer) {
    fmt.Fprint(w, "\x1b[31m")
    fmt.Fprint(w, "fg red,")

    fmt.Fprint(w, "\x1b")
    fmt.Fprint(w, "[")
    fmt.Fprint(w, "3")
    fmt.Fprint(w, "2")
    fmt.Fprint(w, "m") // divided escape sequence
    fmt.Fprint(w, "fg green,")

    fmt.Fprint(w, "\x1b[0m") // reset color

    fmt.Fprint(w, "\x1b[3C") // non-color escape sequence
    fmt.Fprint(w, "3step forward")
    fmt.Fprintln(w)
}

in the case of cmd cmd

in the case of mintty(no windows console) mintty

johnSchnake commented 9 years ago

Sorry for the delay but it looks like this works; I'll let you know if I stumble onto another problem.

shiena commented 9 years ago

OK. This issue was interesting.