Python client test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import curses | |
import sys | |
import random | |
import hackchat | |
import threading | |
class HackClient: | |
def __init__(self, nick, channel): | |
self.nick = nick | |
self.channel = channel | |
self.hc = hackchat.HackChat(nick, channel) | |
self.hc.on_message.append(self.on_message) | |
self.hc.on_join.append(self.on_join) | |
self.hc.on_leave.append(self.on_leave) | |
self.message = "" | |
self.history = [] | |
self.historyCurrent = 0 | |
self.init_curses() | |
self.chat_window = self.stdscr.subwin(curses.LINES-3, curses.COLS-1, 1, 0) | |
self.inputline = self.stdscr.subwin(1, curses.COLS, curses.LINES-1, 0) | |
self.titlebar = self.stdscr.subwin(1, curses.COLS, 0, 0) | |
self.inputline.keypad(True) | |
self.inputline.nodelay(1) | |
self.chat_window.scrollok(1) | |
threading.Thread(target=self.hc.run_loop, daemon=True).start() | |
def init_curses(self): | |
self.stdscr = curses.initscr() | |
curses.noecho() | |
curses.cbreak() | |
curses.start_color() | |
curses.init_pair(1, curses.COLOR_CYAN, 0) | |
curses.init_pair(2, curses.COLOR_YELLOW, 0) | |
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_CYAN) | |
curses.init_pair(4, curses.COLOR_RED, 0) | |
curses.init_pair(5, curses.COLOR_MAGENTA, 0) | |
self.stdscr.clear() | |
def end_curses(self): | |
curses.nocbreak() | |
self.inputline.keypad(False) | |
curses.echo() | |
curses.endwin() | |
def update_and_display(self): | |
self.inputline.clear() | |
self.inputline.bkgd(" ", curses.color_pair(3)) | |
maxmessage = curses.COLS - len(self.nick) - 3 | |
cursorx = len("{}: {}".format(self.nick, self.message[-maxmessage:])) | |
self.inputline.addstr("{}: {}".format(self.nick, self.message[-maxmessage:])) | |
self.inputline.refresh() | |
title_text = "Hack.Chat: {}".format(self.channel) | |
self.titlebar.clear() | |
self.titlebar.bkgd("'", curses.color_pair(3)) | |
self.titlebar.addstr(0, curses.COLS//2-len(title_text)//2, title_text) | |
self.titlebar.refresh() | |
self.chat_window.refresh() | |
self.inputline.move(0, cursorx) | |
try: | |
key = self.inputline.getkey() | |
if key == "\n": | |
if len(self.message.strip()) > 0: | |
self.history.append(self.message.strip()) | |
self.historyCurrent = 0 | |
if self.message.startswith("/"): | |
parts = self.message[1:].strip().split(" ") | |
self.on_command(parts[0], parts[1:]) | |
self.message = "" | |
else: | |
self.chat_window.addstr("[{}] ".format(self.nick), curses.color_pair(2)) | |
self.chat_window.addstr("{}\n".format(self.message.strip())) | |
self.hc.send_message(self.message.strip()) | |
self.message = "" | |
elif key == "KEY_BACKSPACE" and len(self.message) > 0: | |
self.message = self.message[:len(self.message)-1] | |
elif key == "KEY_UP" and len(self.history) > self.historyCurrent: | |
self.historyCurrent += 1 | |
self.message = self.history[-self.historyCurrent] | |
elif key == "KEY_DOWN": | |
if self.historyCurrent > 0: | |
self.historyCurrent -= 1 | |
if self.historyCurrent == 0: | |
self.message = "" | |
else: | |
self.message = self.history[-self.historyCurrent] | |
elif len(key) > 1: | |
pass | |
else: | |
self.message += key | |
except curses.error: | |
pass | |
def run_loop(self): | |
while True: | |
self.update_and_display() | |
curses.napms(10) | |
def on_command(self, command, args): | |
if command == "quit" or command == "q": | |
sys.exit() | |
def on_message(self, hc, message, sender): | |
if self.nick in message: | |
self.chat_window.addstr("[{}] {}\n".format(sender, message), curses.color_pair(4)) | |
curses.beep() | |
else: | |
self.chat_window.addstr("[{}] ".format(sender), curses.color_pair(1)) | |
self.chat_window.addstr("{}\n".format(message)) | |
def on_join(self, hc, nick): | |
self.chat_window.addstr("{} joined the channel.\n".format(nick), curses.color_pair(5)) | |
def on_leave(self, hc, nick): | |
self.chat_window.addstr("{} left the channel.\n".format(nick), curses.color_pair(5)) | |
if __name__ == "__main__": | |
try: | |
if len(sys.argv) == 2: | |
client = HackClient(sys.argv[1], "programming") | |
elif len(sys.argv) == 3: | |
client = HackClient(sys.argv[1], sys.argv[2]) | |
else: | |
sys.exit(1) | |
client.run_loop() | |
except KeyboardInterrupt: | |
client.end_curses() | |
except: | |
client.end_curses() | |
e = sys.exc_info() | |
print("{}: {}".format(e[0], e[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! You help me learning curses lib very much