From c88c30391d8c51616713b514651d766e8f6d14f6 Mon Sep 17 00:00:00 2001 From: empathetic-alligator Date: Sun, 14 Dec 2014 03:11:59 -0500 Subject: [PATCH] Added /colors command to toggle coloring. --- client.go | 23 ++++++++++++++++++++++- colors.go | 9 ++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 9b5166e..2e07b38 100644 --- a/client.go +++ b/client.go @@ -59,6 +59,7 @@ type Client struct { silencedUntil time.Time lastTX time.Time beepMe bool + colorMe bool } func NewClient(server *Server, conn *ssh.ServerConn) *Client { @@ -70,6 +71,7 @@ func NewClient(server *Server, conn *ssh.ServerConn) *Client { Msg: make(chan string, MSG_BUFFER), ready: make(chan struct{}, 1), lastTX: time.Now(), + colorMe: true, } } @@ -82,6 +84,9 @@ func (c *Client) SysMsg(msg string, args ...interface{}) { } func (c *Client) Write(msg string) { + if(!c.colorMe) { + msg = DeColorString(msg) + } c.term.Write([]byte(msg + "\r\n")) } @@ -129,7 +134,15 @@ func (c *Client) Resize(width int, height int) error { func (c *Client) Rename(name string) { c.Name = name - c.term.SetPrompt(fmt.Sprintf("[%s] ", c.ColoredName())) + var prompt string + + if(c.colorMe) { + prompt = c.ColoredName() + } else { + prompt = c.Name + } + + c.term.SetPrompt(fmt.Sprintf("[%s] ", prompt)) } func (c *Client) Fingerprint() string { @@ -324,6 +337,14 @@ func (c *Client) handleShell(channel ssh.Channel) { c.Server.SetMotd(c, newmotd) c.Server.MotdBroadcast(c) } + case "/color": + c.colorMe = !c.colorMe + c.Rename(c.Name) + if c.colorMe { + c.SysMsg("Turned on color chat") + } else { + c.SysMsg("Turned off color chat") + } default: c.SysMsg("Invalid command: %s", line) diff --git a/colors.go b/colors.go index 1e85441..29877d8 100644 --- a/colors.go +++ b/colors.go @@ -5,6 +5,7 @@ import ( "strings" "math/rand" "time" + "regexp" ) const RESET string = "\033[0m" @@ -16,6 +17,12 @@ const BLINK string = "\033[5m" const INVERT string = "\033[7m" var colors = []string { "31", "32", "33", "34", "35", "36", "37", "91", "92", "93", "94", "95", "96", "97" } +var r *regexp.Regexp = regexp.MustCompile("\033\\[[\\d;]+m") + +func DeColorString(s string) string { + s = r.ReplaceAllString(s, "") + return s +} func RandomColor256() string { return fmt.Sprintf("38;05;%d", rand.Intn(256)) @@ -38,4 +45,4 @@ func RandomColorInit() { // 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 -} +} \ No newline at end of file