From 08ff4e7b600e41ea59750f3d002677c9b459b3e0 Mon Sep 17 00:00:00 2001 From: kremlin Date: Sat, 24 Jan 2015 05:39:54 -0500 Subject: [PATCH] finished Xresources parser --- httpd.go | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 108 insertions(+), 9 deletions(-) diff --git a/httpd.go b/httpd.go index 79ea901..d50afdc 100644 --- a/httpd.go +++ b/httpd.go @@ -2,9 +2,14 @@ package main import( "fmt" - "net/http" "os" + "strings" + "strconv" + "bufio" + + "net/http" + "github.com/wsxiaoys/terminal/color" ) @@ -71,11 +76,97 @@ var ansiColors = ktPalette { bg : [3]byte {255,255,255}, } +/* lets get our money's worth out of this utf-8 crap */ +var checkM = "✓" +var xM = "✗" + /* parses a colorfile, returns palette struct. given a nil file pointer, returns standard ANSI color set (our "control") */ func parseColors(colorfile *os.File) (pal ktPalette, err error) { - return ansiColors, nil + if colorfile == nil { return ansiColors, nil } + + ret := ktPalette {} + var e errConsumer + + scanner := bufio.NewScanner(colorfile) + for scanner.Scan() { + + if strings.Contains(scanner.Text(), "color") && + !strings.Contains(scanner.Text(), "!") { + + /* i am so sorry */ + cur := strings.Replace(scanner.Text(), " ", "", -1) + split := strings.Split(cur, ":") + hexColor := strings.Replace(split[1], "#", "", -1) + coNumStr := strings.Replace(split[0], "URxvt*color", "", -1) + + r, err := strconv.ParseUint(hexColor[0:2], 16, 8); e.Consume(err) + g, err := strconv.ParseUint(hexColor[2:4], 16, 8); e.Consume(err) + b, err := strconv.ParseUint(hexColor[4:], 16, 8); e.Consume(err) + + colorNo, err := strconv.Atoi(coNumStr); e.Consume(err) + + if e.err != nil { + return ktPalette{}, e.err + } + + switch(colorNo) { + case 0: ret.black[0] = byte(r); ret.black[1] = byte(g); ret.black[2] = byte(b) + case 1: ret.red[0] = byte(r); ret.red[1] = byte(g); ret.red[2] = byte(b) + case 2: ret.green[0] = byte(r); ret.green[1] = byte(g); ret.green[2] = byte(b) + case 3: ret.yellow[0] = byte(r); ret.yellow[1] = byte(g); ret.yellow[2] = byte(b) + case 4: ret.blue[0] = byte(r); ret.blue[1] = byte(g); ret.blue[2] = byte(b) + case 5: ret.purple[0] = byte(r); ret.purple[1] = byte(g); ret.purple[2] = byte(b) + case 6: ret.cyan[0] = byte(r); ret.cyan[1] = byte(g); ret.cyan[2] = byte(b) + case 7: ret.white[0] = byte(r); ret.white[1] = byte(g); ret.white[2] = byte(b) + case 8: ret.bblack[0] = byte(r); ret.bblack[1] = byte(g); ret.bblack[2] = byte(b) + case 9: ret.bred[0] = byte(r); ret.bred[1] = byte(g); ret.bred[2] = byte(b) + case 10: ret.bgreen[0] = byte(r); ret.bgreen[1] = byte(g); ret.bgreen[2] = byte(b) + case 11: ret.byellow[0] = byte(r); ret.byellow[1] = byte(g); ret.byellow[2] = byte(b) + case 12: ret.bblue[0] = byte(r); ret.bblue[1] = byte(g); ret.bblue[2] = byte(b) + case 13: ret.bpurple[0] = byte(r); ret.bpurple[1] = byte(g); ret.bpurple[2] = byte(b) + case 14: ret.bcyan[0] = byte(r); ret.bcyan[1] = byte(g); ret.bcyan[2] = byte(b) + case 15: ret.bwhite[0] = byte(r); ret.bwhite[1] = byte(g); ret.bwhite[2] = byte(b) + } + + } else if strings.Contains(scanner.Text(), "background") { + + hex := strings.Split(scanner.Text(), "#") + het := hex[1] + + r, err := strconv.ParseUint(het[0:2], 16, 8); e.Consume(err) + g, err := strconv.ParseUint(het[2:4], 16, 8); e.Consume(err) + b, err := strconv.ParseUint(het[4:], 16, 8); e.Consume(err) + + if e.err != nil { + return ktPalette{}, e.err + } + + ret.bg[0] = byte(r) + ret.bg[1] = byte(g) + ret.bg[2] = byte(b) + + } else if strings.Contains(scanner.Text(), "foreground") { + + hex := strings.Split(scanner.Text(), "#") + het := hex[1] + + r, err := strconv.ParseUint(het[0:2], 16, 8); e.Consume(err) + g, err := strconv.ParseUint(het[2:4], 16, 8); e.Consume(err) + b, err := strconv.ParseUint(het[4:], 16, 8); e.Consume(err) + + if e.err != nil { + return ktPalette{}, e.err + } + + ret.fg[0] = byte(r) + ret.fg[1] = byte(g) + ret.fg[2] = byte(b) + } + } + + return ret, e.err } func ktInit(dirPrepend string, port int, colorfilePath string) error { @@ -88,31 +179,39 @@ func ktInit(dirPrepend string, port int, colorfilePath string) error { } pal, err := parseColors(file) - fmt.Print(pal) if err != nil { - color.Printf("@r[%s]@{|} - malformed colorfile\n", xM) + color.Printf("@r[%s]@{|} - malformed colorfile [%s]\n", xM, err) return fmt.Errorf("%s\n", "malformed colorfile") } color.Printf("@g[%s]@{|}\n", checkM) + if pal == ansiColors {} + color.Printf("@ystarting httpd on port @b%d@{|} :: ", port) + return nil } func main() { - err := ktInit("kolors", 999, "/home/kremlin/go/src/k_transpose/kremlin_colors"); + err := ktInit("kolors", 999, "/home/kremlin/.Xresources") /* make sure to close() anything you need to (you need to) */ - resp, err := http.Get("http://kremlin.cc") + _, err = http.Get("http://kremlin.cc") if err != nil {} - fmt.Println(resp) } -var checkM = "✓" -var xM = "✗" +type errConsumer struct { + err error +} + +func (e *errConsumer) Consume(err error) { + if e.err == nil && err != nil { + e.err = err + } +} -- 2.41.0