Added /colors command to toggle coloring.

This commit is contained in:
empathetic-alligator 2014-12-14 03:11:59 -05:00
parent 21bf1ad147
commit c88c30391d
2 changed files with 30 additions and 2 deletions

View File

@ -59,6 +59,7 @@ type Client struct {
silencedUntil time.Time silencedUntil time.Time
lastTX time.Time lastTX time.Time
beepMe bool beepMe bool
colorMe bool
} }
func NewClient(server *Server, conn *ssh.ServerConn) *Client { 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), Msg: make(chan string, MSG_BUFFER),
ready: make(chan struct{}, 1), ready: make(chan struct{}, 1),
lastTX: time.Now(), lastTX: time.Now(),
colorMe: true,
} }
} }
@ -82,6 +84,9 @@ func (c *Client) SysMsg(msg string, args ...interface{}) {
} }
func (c *Client) Write(msg string) { func (c *Client) Write(msg string) {
if(!c.colorMe) {
msg = DeColorString(msg)
}
c.term.Write([]byte(msg + "\r\n")) 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) { func (c *Client) Rename(name string) {
c.Name = name 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 { func (c *Client) Fingerprint() string {
@ -324,6 +337,14 @@ func (c *Client) handleShell(channel ssh.Channel) {
c.Server.SetMotd(c, newmotd) c.Server.SetMotd(c, newmotd)
c.Server.MotdBroadcast(c) 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: default:
c.SysMsg("Invalid command: %s", line) c.SysMsg("Invalid command: %s", line)

View File

@ -5,6 +5,7 @@ import (
"strings" "strings"
"math/rand" "math/rand"
"time" "time"
"regexp"
) )
const RESET string = "\033[0m" const RESET string = "\033[0m"
@ -16,6 +17,12 @@ const BLINK string = "\033[5m"
const INVERT string = "\033[7m" const INVERT string = "\033[7m"
var colors = []string { "31", "32", "33", "34", "35", "36", "37", "91", "92", "93", "94", "95", "96", "97" } 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 { func RandomColor256() string {
return fmt.Sprintf("38;05;%d", rand.Intn(256)) return fmt.Sprintf("38;05;%d", rand.Intn(256))