From 46d00b3e557b168d40c79333065e6b5593b564f5 Mon Sep 17 00:00:00 2001 From: "Andreas Renberg (IQAndreas)" Date: Sat, 13 Dec 2014 04:01:37 -0600 Subject: [PATCH] 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 to resume your previous formatting! --- client.go | 4 ++-- colors.go | 13 +++++++++++-- server.go | 4 ++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index a44ef11..804db32 100644 --- a/client.go +++ b/client.go @@ -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) { diff --git a/colors.go b/colors.go index 16e8ad9..d086444 100644 --- a/colors.go +++ b/colors.go @@ -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 to resume your previous formatting! +func ContinuousFormat(format string, str string) string { + return SYSTEM_MESSAGE_FORMAT + strings.Replace(str, RESET, format, -1) + RESET } diff --git a/server.go b/server.go index d238deb..4edfe4d 100644 --- a/server.go +++ b/server.go @@ -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) {