CLI chat output now uses ANSI escape codes to show colours and styles in chat msgs

This commit is contained in:
Kier Davis 2012-09-11 23:27:39 +01:00 committed by Ammar Askar
parent ea570d8eef
commit 9a715a72b8
1 changed files with 35 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import hashlib
import string
import Utils
import sys
import re
from networking import PacketSenderManager
from Crypto.Random import _UserFriendlyRNG
from Crypto.Util import asn1
@ -22,6 +23,37 @@ except ImportError:
EntityID = 0
def translate_escape(m):
c = m.group(1).lower()
if c == "0": return "\x1b[30m\x1b[21m" # black
elif c == "1": return "\x1b[34m\x1b[21m" # dark blue
elif c == "2": return "\x1b[32m\x1b[21m" # dark green
elif c == "3": return "\x1b[36m\x1b[21m" # dark cyan
elif c == "4": return "\x1b[31m\x1b[21m" # dark red
elif c == "5": return "\x1b[35m\x1b[21m" # purple
elif c == "6": return "\x1b[33m\x1b[21m" # gold
elif c == "7": return "\x1b[37m\x1b[21m" # gray
elif c == "8": return "\x1b[30m\x1b[1m" # dark gray
elif c == "9": return "\x1b[34m\x1b[1m" # blue
elif c == "a": return "\x1b[32m\x1b[1m" # bright green
elif c == "b": return "\x1b[36m\x1b[1m" # cyan
elif c == "c": return "\x1b[31m\x1b[1m" # red
elif c == "d": return "\x1b[35m\x1b[1m" # pink
elif c == "e": return "\x1b[33m\x1b[1m" # yellow
elif c == "f": return "\x1b[37m\x1b[1m" # white
elif c == "k": return "\x1b[5m" # random
elif c == "l": return "\x1b[1m" # bold
elif c == "m": return "\x1b[9m" # strikethrough (escape code not widely supported)
elif c == "n": return "\x1b[4m" # underline
elif c == "o": return "\x1b[3m" # italic (escape code not widely supported)
elif c == "r": return "\x1b[0m" # reset
return ""
def translate_escapes(s):
return re.sub(ur"\xa7([0-9a-zA-Z])", translate_escape, s) + "\x1b[0m"
class ServerConnection(threading.Thread):
def __init__(self, window, username, password, sessionID, server, port):
@ -221,7 +253,8 @@ class PacketListener(threading.Thread):
elif(response == "\x03"):
message = PacketListenerManager.handle03(self.FileObject)
if(self.connection.NoGUI):
filtered_string = filter(lambda x: x in string.printable, message)
# Add "\x1b" because it is essential for ANSI escapes emitted by translate_escapes
filtered_string = filter(lambda x: x in string.printable + "\x1b", translate_escapes(message))
#print message.replace(u'\xa7', '&')
print filtered_string
elif(self.window):
@ -359,4 +392,4 @@ class PacketListener(threading.Thread):
if(self.window == None):
print "Protocol error: " + hex(ord(response))
self.socket.close()
break
break