Support loading colorschemes from files

This commit is contained in:
Zachary Yedidia
2016-03-21 18:19:29 -04:00
parent c37410eb7c
commit 60858417f7
3 changed files with 80 additions and 10 deletions

View File

@@ -1,20 +1,56 @@
package main
import (
"fmt"
"github.com/zyedidia/tcell"
"io/ioutil"
"os/user"
"regexp"
"strings"
)
// The current colorscheme
var colorscheme map[string]tcell.Style
const colorschemeName = "solarized"
// Colorscheme is a map from string to style -- it represents a colorscheme
type Colorscheme map[string]tcell.Style
// The current colorscheme
var colorscheme Colorscheme
// InitColorscheme picks and initializes the colorscheme when micro starts
func InitColorscheme() {
colorscheme = DefaultColorscheme()
LoadColorscheme()
// LoadColorscheme may not have found any colorschemes
if colorscheme == nil {
colorscheme = DefaultColorscheme()
}
}
// LoadColorscheme loads the colorscheme from ~/.micro/colorschemes
func LoadColorscheme() {
usr, _ := user.Current()
dir := usr.HomeDir
LoadColorschemeFromDir(dir + "/.micro/colorschemes")
}
// LoadColorschemeFromDir loads the colorscheme from a directory
func LoadColorschemeFromDir(dir string) {
files, _ := ioutil.ReadDir(dir)
for _, f := range files {
if f.Name() == colorschemeName+".micro" {
text, err := ioutil.ReadFile(dir + "/" + f.Name())
if err != nil {
fmt.Println("Error loading colorscheme:", err)
continue
}
colorscheme = ParseColorscheme(string(text))
}
}
}
// DefaultColorscheme returns the default colorscheme
func DefaultColorscheme() map[string]tcell.Style {
c := make(map[string]tcell.Style)
func DefaultColorscheme() Colorscheme {
c := make(Colorscheme)
c["comment"] = StringToStyle("brightgreen")
c["constant"] = StringToStyle("cyan")
c["identifier"] = StringToStyle("blue")
@@ -23,12 +59,41 @@ func DefaultColorscheme() map[string]tcell.Style {
c["type"] = StringToStyle("yellow")
c["special"] = StringToStyle("red")
c["underlined"] = StringToStyle("brightmagenta")
c["ignore"] = StringToStyle("")
c["ignore"] = StringToStyle("default")
c["error"] = StringToStyle("bold brightred")
c["todo"] = StringToStyle("bold brightmagenta")
return c
}
// ParseColorscheme parses the text definition for a colorscheme and returns the corresponding object
func ParseColorscheme(text string) Colorscheme {
parser := regexp.MustCompile(`color-link\s+(\S*)\s+"(.*)"`)
lines := strings.Split(text, "\n")
c := make(Colorscheme)
for _, line := range lines {
if strings.TrimSpace(line) == "" ||
strings.TrimSpace(line)[0] == '#' {
// Ignore this line
continue
}
matches := parser.FindSubmatch([]byte(line))
if len(matches) == 3 {
link := string(matches[1])
colors := string(matches[2])
c[link] = StringToStyle(colors)
} else {
fmt.Println("Color-link statement is not valid:", line)
}
}
return c
}
// StringToStyle returns a style from a string
func StringToStyle(str string) tcell.Style {
var fg string
@@ -44,9 +109,6 @@ func StringToStyle(str string) tcell.Style {
if strings.Contains(str, "bold") {
style = style.Bold(true)
}
if strings.Contains(str, "blink") {
style = style.Blink(true)
}
if strings.Contains(str, "reverse") {
style = style.Reverse(true)
}
@@ -91,7 +153,9 @@ func StringToColor(str string) tcell.Color {
return tcell.ColorAqua
case "brightwhite":
return tcell.ColorWhite
default:
case "default":
return tcell.ColorDefault
default:
return tcell.GetColor(str)
}
}

View File

@@ -27,6 +27,9 @@ func (sl *Statusline) Display() {
file += " " + filetype
statusLineStyle := tcell.StyleDefault.Reverse(true)
if _, ok := colorscheme["statusline"]; ok {
statusLineStyle = colorscheme["statusline"]
}
for x := 0; x < sl.v.width; x++ {
if x < Count(file) {

View File

@@ -339,6 +339,9 @@ func (v *View) Display() {
// Write the line number
lineNumStyle := tcell.StyleDefault
if _, ok := colorscheme["line-number"]; ok {
lineNumStyle = colorscheme["line-number"]
}
// Write the spaces before the line number if necessary
lineNum := strconv.Itoa(lineN + v.topline + 1)
for i := 0; i < maxLineLength-len(lineNum); i++ {