]> Cypherpunks.ru repositories - goredo.git/blobdiff - log.go
Less hard-coded escape codes
[goredo.git] / log.go
diff --git a/log.go b/log.go
index 2745fd4ee71a05f90837f80f1fd2e2137f4e304b..1a804355dafe689f140724786f17663606e32d51 100644 (file)
--- a/log.go
+++ b/log.go
@@ -20,11 +20,14 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 package main
 
 import (
+       "bytes"
        "flag"
        "fmt"
        "os"
        "strings"
        "sync"
+
+       "golang.org/x/term"
 )
 
 const (
@@ -35,26 +38,6 @@ const (
        EnvLogPid  = "REDO_LOG_PID"
        EnvLogJS   = "REDO_LOG_JS"
        EnvNoColor = "NO_COLOR"
-
-       CReset   = "\033[0m"
-       CBold    = "\033[1m"
-       CBlack   = "\033[30;1m"
-       CRed     = "\033[31;1m"
-       CGreen   = "\033[32;1m"
-       CYellow  = "\033[33;1m"
-       CBlue    = "\033[34;1m"
-       CMagenta = "\033[35;1m"
-       CCyan    = "\033[36;1m"
-       CWhite   = "\033[37;1m"
-
-       CDebug = CYellow
-       CRedo  = CGreen
-       CWait  = CBlue
-       CLock  = CCyan
-       CErr   = CRed
-       CWarn  = CMagenta
-       CJS    = CWhite
-       CNone  = "NONE"
 )
 
 var (
@@ -66,6 +49,16 @@ var (
        LogJS   bool
        MyPid   int
 
+       CDebug string
+       CRedo  string
+       CWait  string
+       CLock  string
+       CErr   string
+       CWarn  string
+       CJS    string
+       CReset string
+       CNone  string = "NONE"
+
        flagDebug   = flag.Bool("debug", false, fmt.Sprintf("enable debug logging (%s=1)", EnvDebug))
        flagLogWait = flag.Bool("log-wait", false, fmt.Sprintf("enable wait messages logging (%s=1)", EnvLogWait))
        flagLogLock = flag.Bool("log-lock", false, fmt.Sprintf("enable lock messages logging (%s=1)", EnvLogLock))
@@ -76,6 +69,19 @@ var (
        LogLenPrev int
 )
 
+func init() {
+       var b bytes.Buffer
+       t := term.NewTerminal(&b, "")
+       CDebug = string(t.Escape.Yellow)
+       CRedo = string(t.Escape.Green)
+       CWait = string(t.Escape.Blue)
+       CLock = string(t.Escape.Cyan)
+       CErr = string(t.Escape.Red)
+       CWarn = string(t.Escape.Magenta)
+       CJS = string(t.Escape.White)
+       CReset = string(t.Escape.Reset)
+}
+
 func fillUpToTermSize(s, end string) string {
        sLen := len(s)
        if sLen < LogLenPrev {