<commit_before>// 256 color terminal printing in Go.
package golor

import (
	"fmt"
)

// 16-colors
var RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, BLACK, WHITE int
var R, G, C, M, Y, W int

func Colorize(s string, fg int, bg int) string {
	if fg > 255 || bg > 255 {
		panic("invalid color index")
	}
	color := ""
	if fg > -1 {
		color += fmt.Sprintf("\033[38;5;%dm", fg)
	}
	if bg > -1 {
		color += fmt.Sprintf("\033[48;5;%dm", bg)
	}
	reset := "\033[0m"
	return color + s + reset
}

// RGB returns the color code corresponding a RGB value set. Arguments
// must range from 0 to 5.
func RGB(red, green, blue int) int {
	return 16 + (red * 36) + (green * 6) + blue
}

func init() {
	RED = RGB(5, 0, 0)
	GREEN = RGB(0, 5, 0)
	BLUE = RGB(0, 0, 5)
	CYAN = RGB(0, 5, 5)
	MAGENTA = RGB(5, 0, 5)
	YELLOW = RGB(5, 5, 0)
	BLACK = 0
	WHITE = RGB(5, 5, 5)

	R = RED
	G = GREEN
	C = CYAN
	M = MAGENTA
	Y = YELLOW
	W = WHITE
}<commit_msg>move unique color assigner to golor package<commit_after>// 256 color terminal printing in Go.
package golor

import (
	"crypto/sha1"
	"fmt"
)

// 16-colors
var RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, BLACK, WHITE int
var R, G, C, M, Y, W int

func Colorize(s string, fg int, bg int) string {
	if fg > 255 || bg > 255 {
		panic("invalid color index")
	}
	color := ""
	if fg > -1 {
		color += fmt.Sprintf("\033[38;5;%dm", fg)
	}
	if bg > -1 {
		color += fmt.Sprintf("\033[48;5;%dm", bg)
	}
	reset := "\033[0m"
	return color + s + reset
}

// RGB returns the color code corresponding a RGB value set. Arguments
// must range from 0 to 5.
func RGB(red, green, blue int) int {
	return 16 + (red * 36) + (green * 6) + blue
}

// Assign and return an unique color for this string. Automatically
// avoid black/white colors.
func AssignColor(s string) int {
	maxColor := 211 // prevent whitish colors; use prime number for mod.
	minColor := 17  // avoid 16 colors, including the black.
	fg := minColor + stringId(s, maxColor-minColor+1)
	return fg
}

func stringId(s string, mod int) int {
	h := sha1.New()
	h.Write([]byte(s))
	sum := 0
	for _, n := range h.Sum(nil) {
		sum += int(n)
	}
	return sum % mod
}

func init() {
	RED = RGB(5, 0, 0)
	GREEN = RGB(0, 5, 0)
	BLUE = RGB(0, 0, 5)
	CYAN = RGB(0, 5, 5)
	MAGENTA = RGB(5, 0, 5)
	YELLOW = RGB(5, 5, 0)
	BLACK = 0
	WHITE = RGB(5, 5, 5)

	R = RED
	G = GREEN
	C = CYAN
	M = MAGENTA
	Y = YELLOW
	W = WHITE
}
{'fraction_non_alphanumeric': 0.15767284991568298, 'fraction_numerical': 0.05354131534569983, 'mean_word_length': 2.9093904448105437, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '578199', 'n_tokens_mistral': 1130, 'n_tokens_neox': 990, 'n_words': 346}