-
Notifications
You must be signed in to change notification settings - Fork 24
/
detect_unix.go
123 lines (106 loc) · 3.8 KB
/
detect_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//go:build !windows
// +build !windows
package box
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/gookit/color"
"github.com/xo/terminfo"
)
// errorMsg prints msg to os.Stderr and uses Red ANSI Color too if supported
func errorMsg(msg string) {
// If the terminal doesn't supports the basic 4 bit
if detectTerminalColor() == terminfo.ColorLevelNone {
fmt.Fprintln(os.Stderr, msg)
} else {
fmt.Fprintln(os.Stderr, color.Red.Sprint(msg))
}
}
// detectTerminalColor detects Color Level Supported
func detectTerminalColor() terminfo.ColorLevel {
// Detect WSL as it has True Color support
wsl, err := ioutil.ReadFile("/proc/sys/kernel/osrelease")
// Additional check needed for Mac Os as it doesn't have "/proc/" folder
if err != nil && !errors.Is(err, os.ErrNotExist) {
log.Fatal(err)
}
// Lowercasing every content inside "/proc/sys/kernal/osrelease"
// because it gives "Microsoft" for WSL and "microsoft" for WSL 2
// so no need of checking twice
if string(wsl) != "" {
wslLower := strings.ToLower(string(wsl))
if strings.Contains(wslLower, "microsoft") {
return terminfo.ColorLevelMillions
}
}
level, err := terminfo.ColorLevelFromEnv()
if err != nil {
log.Fatal(err)
}
return level
}
// roundOffColorVertical rounds off 24 bit Color to the terminals maximum color capacity for Vertical.
func (b Box) roundOffColorVertical(col color.RGBColor) string {
switch detectTerminalColor() {
// Check if the terminal supports 256 Colors only
case terminfo.ColorLevelHundreds:
return col.C256().Sprint(b.Vertical)
// Check if the terminal supports 16 Colors only
case terminfo.ColorLevelBasic:
return col.C16().Sprint(b.Vertical)
// Check if the terminal supports True Color
case terminfo.ColorLevelMillions:
return col.Sprint(b.Vertical)
default:
// Return with a warning as the terminal supports no Color
errorMsg("[warning]: terminal does not support colors, using no effect")
return b.Vertical
}
}
// roundOffTitleColor rounds off 24 bit Color to the terminals maximum color capacity for Title.
func (b Box) roundOffTitleColor(col color.RGBColor, title string) string {
switch detectTerminalColor() {
// Check if the terminal supports 256 Colors only
case terminfo.ColorLevelHundreds:
return col.C256().Sprint(title)
// Check if the terminal supports 16 Colors only
case terminfo.ColorLevelBasic:
return col.C16().Sprint(title)
// Check if the terminal supports True Color
case terminfo.ColorLevelMillions:
return col.Sprint(title)
default:
// Return with a warning as the terminal supports no Color
errorMsg("[warning]: terminal does not support colors, using no effect")
return title
}
}
// roundOffColor checks terminlal color level then rounds off 24 bit color to the level supported
// for TopBar and BottomBar
func roundOffColor(col color.RGBColor, topBar, bottomBar string) (string, string) {
switch detectTerminalColor() {
// Check if the terminal supports 256 Colors only
case terminfo.ColorLevelHundreds:
TopBar := addStylePreservingOriginalFormat(topBar, col.C256().Sprint)
BottomBar := addStylePreservingOriginalFormat(bottomBar, col.C256().Sprint)
return TopBar, BottomBar
// Check if the terminal supports 16 Colors only
case terminfo.ColorLevelBasic:
TopBar := addStylePreservingOriginalFormat(bottomBar, col.C16().Sprint)
BottomBar := addStylePreservingOriginalFormat(bottomBar, col.C16().Sprint)
return TopBar, BottomBar
// Check if the terminal supports True Color
case terminfo.ColorLevelMillions:
TopBar := addStylePreservingOriginalFormat(topBar, col.Sprint)
BottomBar := addStylePreservingOriginalFormat(bottomBar, col.Sprint)
return TopBar, BottomBar
default:
// Return with a warning as the terminal supports no Color
errorMsg("[warning]: terminal does not support colors, using no effect")
return topBar, bottomBar
}
}