chzyer / readline

Readline is a pure go(golang) implementation for GNU-Readline kind library
MIT License
2.06k stars 273 forks source link

change of color (ANSI color code unchaged) when calling `log.SetOutput(l.Stderr())` #216

Open gekigek99 opened 1 year ago

gekigek99 commented 1 year ago

example code:

package main

import (
    "fmt"
    "io"
    "log"
    "os"
    "time"

    "github.com/chzyer/readline"
)

const (
    COLOR_RESET = "\033[0m"
    COLOR_GREEN = "\033[32m"
)

func main() {
    go logger()

    time.Sleep(2 * time.Second)

    fmt.Println("calling log.SetOutput() inside GetInput() which changes color")
    go GetInput()

    time.Sleep(time.Hour)
}

func logger() {
    for {
        log.Printf("%scolor%s\n", COLOR_GREEN, COLOR_RESET)
        time.Sleep(500 * time.Millisecond)
    }
}

// GetInput is used to read input from user.
// [goroutine]
func GetInput() {
    l, err := readline.NewEx(
        &readline.Config{
            Prompt: "» ",
            AutoComplete: readline.NewPrefixCompleter(
                readline.PcItem("msh",
                    readline.PcItem("start"),
                    readline.PcItem("freeze"),
                    readline.PcItem("exit"),
                ),
                readline.PcItem("mine"),
            ),
            FuncFilterInputRune: func(r rune) (rune, bool) {
                switch r {
                case readline.CharCtrlZ: // block CtrlZ feature
                    return r, false
                }
                return r, true
            },
        })
    if err != nil {
        fmt.Println("error while starting readline: %s", err.Error())
        return
    }
    defer l.Close()

    log.SetOutput(l.Stderr()) // autorefresh prompt line
    for {
        line, err := l.Readline()
        switch err {
        case nil:
        case io.EOF, readline.ErrInterrupt:
            os.Exit(1)
        default:
            fmt.Println(err.Error())
            continue
        }

        fmt.Println("user input: %s", line)
    }
}

powershell && cmd

first 4 are dark green, last 4 are light green

2022/12/26 19:00:32 color
2022/12/26 19:00:32 color
2022/12/26 19:00:33 color
2022/12/26 19:00:34 color
calling log.SetOutput() inside GetInput() which changes color
2022/12/26 19:00:34 color
2022/12/26 19:00:35 color
2022/12/26 19:00:35 color
2022/12/26 19:00:36 color

git bash

2022/12/26 19:05:44 ←[32mcolor←[0m
2022/12/26 19:05:45 ←[32mcolor←[0m
2022/12/26 19:05:45 ←[32mcolor←[0m
2022/12/26 19:05:46 ←[32mcolor←[0m
calling log.SetOutput() inside GetInput() which changes color
2022/12/26 19:05:46 color
2022/12/26 19:05:47 color
2022/12/26 19:05:47 color
2022/12/26 19:05:48 color

specs

question

slingamn commented 1 year ago

@gekigek99 I'm curious if #218 would work for you?

gekigek99 commented 1 year ago

It's a possibility, how can i test it?

(I mean how can I install a specific branch of a library?)

slingamn commented 1 year ago

Probably the easiest technique:

  1. Fetch this PR and check it out locally. (If you already have a clone of this repository you can git fetch origin pull/218/head and then git checkout FETCH_HEAD.)
  2. Use go mod replace to repoint at your local checkout: https://thewebivore.com/using-replace-in-go-mod-to-point-to-your-local-module/

edit: fix reference to PR number

gekigek99 commented 1 year ago

https://github.com/chzyer/readline/pull/218 indeed solves it!

great job @slingamn! I don't think I ever saw a faster fix ;)

Btw I love your philosophy rep <3