9 "github.com/zyedidia/tcell/v2"
12 // DefStyle is Micro's default style
13 var DefStyle tcell.Style = tcell.StyleDefault
15 // Colorscheme is the current colorscheme
16 var Colorscheme map[string]tcell.Style
18 // GetColor takes in a syntax group and returns the colorscheme's style for that group
19 func GetColor(color string) tcell.Style {
24 groups := strings.Split(color, ".")
27 for i, g := range groups {
32 if style, ok := Colorscheme[curGroup]; ok {
36 } else if style, ok := Colorscheme[color]; ok {
39 st = StringToStyle(color)
45 // ColorschemeExists checks if a given colorscheme exists
46 func ColorschemeExists(colorschemeName string) bool {
47 return FindRuntimeFile(RTColorscheme, colorschemeName) != nil
50 // InitColorscheme picks and initializes the colorscheme when micro starts
51 func InitColorscheme() error {
52 Colorscheme = make(map[string]tcell.Style)
53 DefStyle = tcell.StyleDefault
55 return LoadDefaultColorscheme()
58 // LoadDefaultColorscheme loads the default colorscheme from $(ConfigDir)/colorschemes
59 func LoadDefaultColorscheme() error {
60 return LoadColorscheme(GlobalSettings["colorscheme"].(string))
63 // LoadColorscheme loads the given colorscheme from a directory
64 func LoadColorscheme(colorschemeName string) error {
65 file := FindRuntimeFile(RTColorscheme, colorschemeName)
67 return errors.New(colorschemeName + " is not a valid colorscheme")
69 if data, err := file.Data(); err != nil {
70 return errors.New("Error loading colorscheme: " + err.Error())
72 Colorscheme, err = ParseColorscheme(string(data))
80 // ParseColorscheme parses the text definition for a colorscheme and returns the corresponding object
81 // Colorschemes are made up of color-link statements linking a color group to a list of colors
82 // For example, color-link keyword (blue,red) makes all keywords have a blue foreground and
84 func ParseColorscheme(text string) (map[string]tcell.Style, error) {
86 parser := regexp.MustCompile(`color-link\s+(\S*)\s+"(.*)"`)
88 lines := strings.Split(text, "\n")
90 c := make(map[string]tcell.Style)
92 for _, line := range lines {
93 if strings.TrimSpace(line) == "" ||
94 strings.TrimSpace(line)[0] == '#' {
99 matches := parser.FindSubmatch([]byte(line))
100 if len(matches) == 3 {
101 link := string(matches[1])
102 colors := string(matches[2])
104 style := StringToStyle(colors)
107 if link == "default" {
111 err = errors.New("Color-link statement is not valid: " + line)
118 // StringToStyle returns a style from a string
119 // The strings must be in the format "extra foregroundcolor,backgroundcolor"
120 // The 'extra' can be bold, reverse, italic or underline
121 func StringToStyle(str string) tcell.Style {
123 spaceSplit := strings.Split(str, " ")
124 split := strings.Split(spaceSplit[len(spaceSplit)-1], ",")
126 fg, bg = split[0], split[1]
130 fg = strings.TrimSpace(fg)
131 bg = strings.TrimSpace(bg)
133 var fgColor, bgColor tcell.Color
135 fgColor, _, _ = DefStyle.Decompose()
137 fgColor = StringToColor(fg)
140 _, bgColor, _ = DefStyle.Decompose()
142 bgColor = StringToColor(bg)
145 style := DefStyle.Foreground(fgColor).Background(bgColor)
146 if strings.Contains(str, "bold") {
147 style = style.Bold(true)
149 if strings.Contains(str, "italic") {
150 style = style.Italic(true)
152 if strings.Contains(str, "reverse") {
153 style = style.Reverse(true)
155 if strings.Contains(str, "underline") {
156 style = style.Underline(true)
161 // StringToColor returns a tcell color from a string representation of a color
162 // We accept either bright... or light... to mean the brighter version of a color
163 func StringToColor(str string) tcell.Color {
166 return tcell.ColorBlack
168 return tcell.ColorMaroon
170 return tcell.ColorGreen
172 return tcell.ColorOlive
174 return tcell.ColorNavy
176 return tcell.ColorPurple
178 return tcell.ColorTeal
180 return tcell.ColorSilver
181 case "brightblack", "lightblack":
182 return tcell.ColorGray
183 case "brightred", "lightred":
184 return tcell.ColorRed
185 case "brightgreen", "lightgreen":
186 return tcell.ColorLime
187 case "brightyellow", "lightyellow":
188 return tcell.ColorYellow
189 case "brightblue", "lightblue":
190 return tcell.ColorBlue
191 case "brightmagenta", "lightmagenta":
192 return tcell.ColorFuchsia
193 case "brightcyan", "lightcyan":
194 return tcell.ColorAqua
195 case "brightwhite", "lightwhite":
196 return tcell.ColorWhite
198 return tcell.ColorDefault
200 // Check if this is a 256 color
201 if num, err := strconv.Atoi(str); err == nil {
202 return GetColor256(num)
204 // Probably a truecolor hex value
205 return tcell.GetColor(str)
209 // GetColor256 returns the tcell color for a number between 0 and 255
210 func GetColor256(color int) tcell.Color {
212 return tcell.ColorDefault
214 return tcell.PaletteColor(color)