Skip to content

Instantly share code, notes, and snippets.

@GeertJohan
Created February 7, 2013 08:57
Show Gist options
  • Save GeertJohan/4729664 to your computer and use it in GitHub Desktop.
Save GeertJohan/4729664 to your computer and use it in GitHub Desktop.
terminal colors
package main
import (
"fmt"
"tc" //terminal color
)
// First syntax idea
// Please skip this and take a look at the second idea.
// Now that I've put that down I find it way better..
func main() {
var strBla = "blabla"
var numSix = 6
// Parse @fg and @bg
var coloredText = tc.Color("@fg{red} red text @fg{21}@bg{226} blue text and yellow background")
fmt.Println(coloredText)
fmt.Println("this is printed with terminal defaults, tc.Color() always ends with reset.")
// Parse color without reset at the end
var colorSettings tc.ColorWithoutReset("@fg{red} this is printed in red.")
fmt.Println(colorSettings)
fmt.Println("this is also printed in red.")
// Reset coloring
fmt.Print(tc.Reset())
// Combined with Printf
fmt.Printf(tc.Color("@fg{red} %s @fg{21} %d"), strBla, numSix)
// Or directly (wrapped fmt.Printf):
tc.Printf("@fg{red} %s @fg{21} %d", strBla, numSix)
}
// Second syntax idea.
// I find this better readable and more correct.
// Ofcourse there's still the problem that the given format-string should not have user-input.
// Programmers should use Printf for that..
func main() {
var strBla = "blabla"
var numSix = 6
// Parse @fg and @bg
coloredText := tc.Color("[fg-red] red text [fg-blue bg-226] blue text and yellow background")
fmt.Println(coloredText)
fmt.Println("this is printed with terminal defaults, tc.Color() always ends with reset.")
// Parse color without reset at the end
colorSettings := tc.ColorWithoutReset("[fg-red] this is printed in red.")
fmt.Println(colorSettings)
fmt.Println("this is also printed in red.")
// Reset coloring
fmt.Print(tc.Reset())
// Prepared colorored Printf format string
formatString := tc.Color("[fg-red] %s [fg-21] %d")
fmt.Printf(formatString, strBla, numSix)
// Wrapped fmt.Printf:
tc.Printf("[fg-red] %s [fg-21] %d", strBla, numSix)
// Other code's would be:
// [fg-blue] and a lot of other basic color codes.
// [bg-blue] and a lot of other basic color codes.
// [reset]
// [bold] ??
// [italic] ??
// [underlined] ??
}
// Third idea
// No format string
func main() {
var strBla = "blabla"
var numSix = 6
// Parse @fg and @bg
coloredText := tc.Format(tc.fgRed, "red text", tc.fgBlue, tc.bgYellow, "blue text and yellow background")
fmt.Println(coloredText)
fmt.Println("this is printed with terminal defaults, tc.Color() always ends with reset.")
// Parse color without reset at the end
colorSettings := tc.FormatWithoutReset(tc.fgRed, "this is printed in red.")
fmt.Println(colorSettings)
fmt.Println("this is also printed in red.")
// Reset coloring
fmt.Print(tc.Reset())
// Prepared colorored Printf format string
formatString := tc.Format(tc.fgRed, "%s ", tc.fg(21) ,"%d")
fmt.Printf(formatString, strBla, numSix)
// Wrapped fmt.Printf:
tc.Printf(tc.fgRed, "%s ", tc.fg(21), tc.bg(240) ,"%d", strBla, numSix)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment