Ugly hack which fixes two colors in a row

This is a horrible hack to "continue" the previous string color and
format after a RESET has been encountered.

This is not HTML where you can just do a </style> to resume your
previous formatting!
This commit is contained in:
Andreas Renberg (IQAndreas) 2014-12-13 04:01:37 -06:00
parent de3df4e1a4
commit 46d00b3e55
3 changed files with 15 additions and 6 deletions

View File

@ -62,11 +62,11 @@ func (c *Client) ColoredName() string {
}
func (c *Client) SysMsg(msg string, args ...interface{}) {
c.Msg <- SYSTEM_MESSAGE_FORMAT + "-> " + fmt.Sprintf(msg, args...) + RESET
c.Msg <- ContinuousFormat(SYSTEM_MESSAGE_FORMAT, "-> " + fmt.Sprintf(msg, args...))
}
func (c *Client) SysMsg2(msg string, args ...interface{}) {
c.Write(SYSTEM_MESSAGE_FORMAT + "-> " + fmt.Sprintf(msg, args...) + RESET)
c.Write(ContinuousFormat(SYSTEM_MESSAGE_FORMAT, "-> " + fmt.Sprintf(msg, args...)))
}
func (c *Client) Write(msg string) {

View File

@ -1,6 +1,7 @@
package main
import (
"strings"
"math/rand"
"time"
)
@ -8,6 +9,7 @@ import (
const RESET string = "\033[0m"
const BOLD string = "\033[1m"
const DIM string = "\033[2m"
const ITALIC string = "\033[3m"
const UNDERLINE string = "\033[4m"
const BLINK string = "\033[5m"
const INVERT string = "\033[7m"
@ -19,6 +21,13 @@ func RandomColor() string {
return colors[rand.Intn(len(colors))]
}
func ColorString(format string, msg string) string {
return BOLD + "\033[" + format + "m" + msg + RESET
func ColorString(color string, msg string) string {
return BOLD + "\033[" + color + "m" + msg + RESET
}
// Horrible hack to "continue" the previous string color and format
// after a RESET has been encountered.
// This is not HTML where you can just do a </style> to resume your previous formatting!
func ContinuousFormat(format string, str string) string {
return SYSTEM_MESSAGE_FORMAT + strings.Replace(str, RESET, format, -1) + RESET
}

View File

@ -78,7 +78,7 @@ func (s *Server) Len() int {
const SYSTEM_MESSAGE_FORMAT string = "\033[1;3;90m"
func (s *Server) SysMsg(msg string, args ...interface{}) {
s.Broadcast(SYSTEM_MESSAGE_FORMAT + " * " + fmt.Sprintf(msg, args...) + RESET, nil)
s.Broadcast(ContinuousFormat(SYSTEM_MESSAGE_FORMAT, " * " + fmt.Sprintf(msg, args...)), nil)
}
func (s *Server) Broadcast(msg string, except *Client) {
@ -112,7 +112,7 @@ func (s *Server) Add(client *Client) {
num := len(s.clients)
s.lock.Unlock()
s.Broadcast(fmt.Sprintf("%s * %s joined. (Total connected: %d)%s", SYSTEM_MESSAGE_FORMAT, client.ColoredName(), num, RESET), client)
s.Broadcast(ContinuousFormat(SYSTEM_MESSAGE_FORMAT, fmt.Sprintf(" * %s joined. (Total connected: %d)", client.ColoredName(), num)), client)
}
func (s *Server) Remove(client *Client) {