Instantly share code, notes, and snippets.

Embed
What would you like to do?
curses chat ui
import curses
(NORMAL, GREEN, RED, YELLOW, BLUE) = range(1,6)
(BUSY, AVAILABLE, IDLE) = (RED, GREEN, YELLOW)
WIN_CONTACTLIST_X = 0
WIN_CONTACTLIST_Y = 0
WIN_CONTACTLIST_WIDTH = 28
WIN_CONTACTLIST_HEIGHT = 0
WIN_MESSAGES_X = WIN_CONTACTLIST_X + WIN_CONTACTLIST_WIDTH
WIN_MESSAGES_Y = 0
WIN_MESSAGES_WIDTH = 0
WIN_MESSAGES_HEIGHT = 50
WIN_TEXTBOX_X = 0
WIN_TEXTBOX_Y = 0
WIN_TEXTBOX_WIDTH = 0
WIN_TEXTBOX_HEIGHT = 3
class Messenger(object):
def __init__(self, contacts, messages):
self.contacts = contacts
self.messages = messages
self.screen = curses.initscr()
curses.start_color()
curses.init_pair(NORMAL, curses.COLOR_WHITE, curses.COLOR_BLACK)
curses.init_pair(RED, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(GREEN, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(YELLOW, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(BLUE, curses.COLOR_BLUE, curses.COLOR_BLACK)
self.screen.border(0)
(self.MAX_Y, self.MAX_X) = self.screen.getmaxyx()
def display(self, where, msg, color, y, x):
where.addstr(y, x, msg, curses.color_pair(color))
def draw_message(self, where, name, msg, y):
where.addstr(y, 1, '<', curses.color_pair(BLUE))
where.addstr(y, 2, name, curses.color_pair(NORMAL))
where.addstr(y, 2 + len(name), '>', curses.color_pair(BLUE))
where.addstr(y, 2 + len(name) + 2, msg, curses.color_pair(NORMAL))
def draw_contacts(self, contacts):
win = curses.newwin(
self.MAX_Y-2,
WIN_CONTACTLIST_WIDTH,
WIN_CONTACTLIST_Y,
WIN_CONTACTLIST_X)
win.clear()
win.box()
(x, y) = (1,3)
self.display(win, 'CONTACTS', NORMAL, 1, 1)
self.display(win, 'Order: [ ] Name [X] Status', NORMAL, self.MAX_Y-4,1)
for name, status in contacts:
self.display(win, '#', status, y, x+3)
self.display(win, name, NORMAL, y, x+5)
y += 1
# Temp draw current contact
self.display(win, '>>', BLUE, 3,1)
self.screen.refresh()
win.refresh()
def draw_messages(self, messages):
win = curses.newwin(
self.MAX_Y-2,
WIN_MESSAGES_WIDTH,
WIN_MESSAGES_Y,
WIN_MESSAGES_X)
(y,x) = (1,1)
win.clear()
win.box()
for name, msg in messages:
self.draw_message(win, name, msg, y)
y+=1
self.screen.refresh()
win.refresh()
def draw_textbox(self):
win = curses.newwin(
WIN_TEXTBOX_HEIGHT,
WIN_TEXTBOX_WIDTH,
self.MAX_Y-3,
WIN_TEXTBOX_X)
win.clear()
win.box()
self.screen.refresh()
win.refresh()
def main(self):
self.screen.clear()
# Draw stuff
self.draw_contacts(self.contacts)
self.draw_messages(self.messages)
self.draw_textbox()
self.screen.refresh()
self.screen.getch()
curses.endwin()
if __name__ == '__main__':
contacts = [
('Omar Abou Mrad', BUSY),
('Joe Rahme', AVAILABLE),
('David Ranger', IDLE),
('Roula Abou Mrad', AVAILABLE),
('Raul Abou Mrad', BUSY),
]
messages = [
('David Ranger', 'Hey man what\'s up!'),
('Omar Abou Mrad', 'Nothing much just chillin, how are you?'),
('David Ranger', 'Same old same old.'),
('Omar Abou Mrad', 'Same here.')
]
Messenger(contacts,messages).main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment