fatih / color

Color package for Go (golang)
https://pkg.go.dev/github.com/fatih/color
MIT License
7.26k stars 615 forks source link

backspace character not working #64

Closed verkestk closed 7 years ago

verkestk commented 7 years ago

Backspace character is not clearing previously output characters.

See screenshot:

screen shot 2017-03-06 at 10 13 10 am

I'm running zsh on OSX.

Here's the program to repro:

package main

import (
    "fmt"
    "github.com/fatih/color"
)

func main() {
    fmt.Println("with fmt:")
    fmt.Print("Hello...")
    fmt.Println("\b\b\b, World!\n")

    fmt.Println("with color:")
    color.Cyan("Hello...")
    color.Cyan("\b\b\b, World!\n")
}
malisit commented 7 years ago

Things get complicated here: https://github.com/fatih/color/blob/9131ab34cf20d2f6d83fdc67168a5430d1c7dc23/color.go#L441-L443 This part adds a newline escape sequence to every string str you used with Cyan function as an argument and since your string is now str\n and the nondestructive backspace only works on the current line, you won't be able to have same results with fmt.Print when you use \b. This can be hurdled by simply removing the mentioned lines.

Here's a workaround for you,

fmt.Print(CyanString("Hello..."))
fmt.Print(CyanString("\b\b\b, World!\n"))

P.S.: I'm not sure if this is an issue, rather, I guess, it's a design choice but I'm in the same side with you and I would prefer these lines to be removed.

fatih commented 7 years ago

Hi @verkestk

@malisit is right. It's a design decision for the custom function. However you can use the constructors which don't have the problem:


func main() {
    fmt.Println("with fmt:")
    fmt.Print("Hello...")
    fmt.Println("\b\b\b, World!\n")

    fmt.Println("with custom color:")
    c := color.New(color.FgCyan)
    c.Print("Hello...")
    c.Println("\b\b\b, World!\n")
}

This works as indented:

screen shot 2017-03-19 at 11 58 49 pm

Thanks for the feedback and thanks @malisit for the lookup. I'm closing as this is expected.