Skip to content

Commit

Permalink
Set env variable to disable logs colorization (#139)
Browse files Browse the repository at this point in the history
Co-authored-by: Svetlana Kirdyaykina <[email protected]>
  • Loading branch information
svetadob and Svetlana Kirdyaykina authored May 8, 2024
1 parent 1f14529 commit 219ce65
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions difflib/difflib.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"io"
"os"
"runtime"
"strings"
)
Expand All @@ -29,6 +30,8 @@ const (
green = "\033[32m"
)

var colorizeLog = true

func min(a, b int) int {
if a < b {
return a
Expand Down Expand Up @@ -205,12 +208,15 @@ func (m *SequenceMatcher) isBJunk(s string) bool {
// If IsJunk is not defined:
//
// Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where
// alo <= i <= i+k <= ahi
// blo <= j <= j+k <= bhi
//
// alo <= i <= i+k <= ahi
// blo <= j <= j+k <= bhi
//
// and for all (i',j',k') meeting those conditions,
// k >= k'
// i <= i'
// and if i == i', j <= j'
//
// k >= k'
// i <= i'
// and if i == i', j <= j'
//
// In other words, of all maximal matching blocks, return one that
// starts earliest in a, and of all those maximal matching blocks that
Expand Down Expand Up @@ -563,6 +569,7 @@ type UnifiedDiff struct {
// 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
// The modification times are normally expressed in the ISO 8601 format.
func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
SetColorizeLogs()
buf := bufio.NewWriter(writer)
defer buf.Flush()
wf := func(format string, args ...interface{}) error {
Expand Down Expand Up @@ -638,7 +645,7 @@ func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
}

func colorize(color, message string) string {
if runtime.GOOS == "windows" {
if runtime.GOOS == "windows" || !colorizeLog {
return message
}
return color + message + "\033[0m"
Expand Down Expand Up @@ -783,3 +790,12 @@ func SplitLines(s string) []string {
lines[len(lines)-1] += "\n"
return lines
}

// Remove colorizing from the log if env variable COLORIZE_LOGS=false
func SetColorizeLogs() {
colorize := os.Getenv("COLORIZE_LOGS")

if colorize == "false" {
colorizeLog = false
}
}

0 comments on commit 219ce65

Please sign in to comment.