Skip to content

Commit

Permalink
Refactor color formatting code
Browse files Browse the repository at this point in the history
  • Loading branch information
mkchoi212 committed Dec 30, 2017
1 parent 873a977 commit b028aea
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 24 deletions.
56 changes: 45 additions & 11 deletions color.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package main

import "fmt"
import (
"fmt"
)

const (
Gray = 0
Red = 1
Green = 2
Yellow = 3
Blue = 4
Purple = 5
FgBlack = 0
FgRed = 1
FgGreen = 2
FgYellow = 3
FgBlue = 4
FgPurple = 5
)

const (
Expand All @@ -18,10 +20,42 @@ const (
Underline = 4
)

func Colorize(s string, c int) string {
return fmt.Sprintf("\033[3%d;%dm%s\033[0m", c, Regular, s)
func colorString(color int, style int, format string, a ...interface{}) string {
var str string
if len(a) == 0 {
str = format
} else {
str = fmt.Sprintf(format, a...)
}
return fmt.Sprintf("\033[3%d;%dm%s\033[0m", color, style, str)
}

// Black is a convenient helper function to return a string with black foreground
func Black(style int, format string, a ...interface{}) string {
return colorString(FgBlack, style, format, a...)
}

// Red is a convenient helper function to return a string with red foreground
func Red(style int, format string, a ...interface{}) string {
return colorString(FgRed, style, format, a...)
}

// Green is a convenient helper function to return a string with green foreground
func Green(style int, format string, a ...interface{}) string {
return colorString(FgGreen, style, format, a...)
}

// Yellow is a convenient helper function to return a string with yellow foreground
func Yellow(style int, format string, a ...interface{}) string {
return colorString(FgYellow, style, format, a...)
}

// Blue is a convenient helper function to return a string with blue foreground
func Blue(style int, format string, a ...interface{}) string {
return colorString(FgBlue, style, format, a...)
}

func ColorizeLight(s string, c int) string {
return fmt.Sprintf("\033[3%d;%dm%s\033[0m", c, Light, s)
// Purple is a convenient helper function to return a string with purple foreground
func Purple(style int, format string, a ...interface{}) string {
return colorString(FgPurple, style, format, a...)
}
8 changes: 4 additions & 4 deletions conflict.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func (c *Conflict) Select(g *gocui.Gui, withHelp bool) error {
for idx, conflict := range conflicts {
var out string
if conflict.Resolved {
out = Colorize(fmt.Sprintf("✅ %s:%d", conflict.FileName, conflict.Start), Green)
out = Green(Regular, "✅ %s:%d", conflict.FileName, conflict.Start)
} else {
out = Colorize(fmt.Sprintf("%d. %s:%d", idx+1, conflict.FileName, conflict.Start), Red)
out = Red(Regular, "%d. %s:%d", idx+1, conflict.FileName, conflict.Start)
}

if conflict.isEqual(c) {
Expand Down Expand Up @@ -119,15 +119,15 @@ func (c *Conflict) getPaddingLines() (topPadding, bottomPadding []string) {
}

for _, l := range lines[start-c.topPeek : start] {
topPadding = append(topPadding, Colorize(l, Gray))
topPadding = append(topPadding, Black(Regular, l))
}

if c.bottomPeek >= len(lines)-c.End {
c.bottomPeek = len(lines) - c.End
}

for _, l := range lines[end : end+c.bottomPeek] {
bottomPadding = append(bottomPadding, Colorize(l, Gray))
bottomPadding = append(bottomPadding, Black(Regular, l))
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func layout(g *gocui.Gui) error {
return err
}
v.Frame = false
prompt := Colorize("[wasd] >>", Green)
prompt := Green(Regular, "[wasd] >>")
v.Write([]byte(prompt))
v.MoveCursor(11, 0, true)
}
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func parseInput(g *gocui.Gui, v *gocui.View) error {
case in == "q":
globalQuit(g)
default:
printPrompt(g, Colorize("[wasd] >>", Red))
printPrompt(g, Green(Regular, "[wasd] >>"))
return nil
}
printPrompt(g, Colorize("[wasd] >>", Green))
printPrompt(g, Green(Regular, "[wasd] >>"))
return nil
}

Expand Down
12 changes: 6 additions & 6 deletions summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func printHelp(v *gocui.View) {
h - print help
q || Ctrl+c - quit application
`
fmt.Fprintf(v, Colorize(instruction, Purple))
fmt.Fprintf(v, Purple(Regular, instruction))
}

func printSummary() {
Expand All @@ -26,22 +26,22 @@ func printSummary() {

for _, c := range conflicts {
if c.Resolved {
line = Colorize(fmt.Sprintf("✔ %s: %d", c.FileName, c.Start), Green)
line = Green(Regular, "✔ %s: %d", c.FileName, c.Start)
resolvedCnt++
} else {
line = Colorize(fmt.Sprintf("✘ %s: %d", c.FileName, c.Start), Red)
line = Red(Regular, "✘ %s: %d", c.FileName, c.Start)
}
fmt.Println(line)
}

var buf bytes.Buffer
if resolvedCnt != len(conflicts) {
buf.WriteString("\nResolved ")
buf.WriteString(ColorizeLight(fmt.Sprintf("%d ", resolvedCnt), Red))
buf.WriteString(Red(Light, "%d ", resolvedCnt))
buf.WriteString("conflict(s) out of ")
buf.WriteString(ColorizeLight(fmt.Sprintf("%d", len(conflicts)), Red))
buf.WriteString(Red(Light, "%d", len(conflicts)))
} else {
buf.WriteString(Colorize(fmt.Sprintf("\nFixed All Conflicts 🎉"), Green))
buf.WriteString(Green(Regular, "\nFixed All Conflicts 🎉"))
}
fmt.Println(buf.String())
}

0 comments on commit b028aea

Please sign in to comment.