Skip to content

Commit 219ce65

Browse files
svetadobSvetlana Kirdyaykina
and
Svetlana Kirdyaykina
authored
Set env variable to disable logs colorization (#139)
Co-authored-by: Svetlana Kirdyaykina <[email protected]>
1 parent 1f14529 commit 219ce65

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

difflib/difflib.go

+22-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"fmt"
2222
"io"
23+
"os"
2324
"runtime"
2425
"strings"
2526
)
@@ -29,6 +30,8 @@ const (
2930
green = "\033[32m"
3031
)
3132

33+
var colorizeLog = true
34+
3235
func min(a, b int) int {
3336
if a < b {
3437
return a
@@ -205,12 +208,15 @@ func (m *SequenceMatcher) isBJunk(s string) bool {
205208
// If IsJunk is not defined:
206209
//
207210
// Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where
208-
// alo <= i <= i+k <= ahi
209-
// blo <= j <= j+k <= bhi
211+
//
212+
// alo <= i <= i+k <= ahi
213+
// blo <= j <= j+k <= bhi
214+
//
210215
// and for all (i',j',k') meeting those conditions,
211-
// k >= k'
212-
// i <= i'
213-
// and if i == i', j <= j'
216+
//
217+
// k >= k'
218+
// i <= i'
219+
// and if i == i', j <= j'
214220
//
215221
// In other words, of all maximal matching blocks, return one that
216222
// starts earliest in a, and of all those maximal matching blocks that
@@ -563,6 +569,7 @@ type UnifiedDiff struct {
563569
// 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
564570
// The modification times are normally expressed in the ISO 8601 format.
565571
func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
572+
SetColorizeLogs()
566573
buf := bufio.NewWriter(writer)
567574
defer buf.Flush()
568575
wf := func(format string, args ...interface{}) error {
@@ -638,7 +645,7 @@ func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
638645
}
639646

640647
func colorize(color, message string) string {
641-
if runtime.GOOS == "windows" {
648+
if runtime.GOOS == "windows" || !colorizeLog {
642649
return message
643650
}
644651
return color + message + "\033[0m"
@@ -783,3 +790,12 @@ func SplitLines(s string) []string {
783790
lines[len(lines)-1] += "\n"
784791
return lines
785792
}
793+
794+
// Remove colorizing from the log if env variable COLORIZE_LOGS=false
795+
func SetColorizeLogs() {
796+
colorize := os.Getenv("COLORIZE_LOGS")
797+
798+
if colorize == "false" {
799+
colorizeLog = false
800+
}
801+
}

0 commit comments

Comments
 (0)