From 223d2e05c222e9023fdbf5c31db7d032668a0f96 Mon Sep 17 00:00:00 2001 From: Orakaro Date: Mon, 25 Aug 2014 10:09:26 +0900 Subject: [PATCH 1/1] add info and colored --- rainbowstream/colorset/monokai.json | 12 ++++--- rainbowstream/draw.py | 54 ++++++++++++++++++++++++++--- rainbowstream/interactive.py | 9 +++-- 3 files changed, 64 insertions(+), 11 deletions(-) diff --git a/rainbowstream/colorset/monokai.json b/rainbowstream/colorset/monokai.json index 3473aea..553be6a 100644 --- a/rainbowstream/colorset/monokai.json +++ b/rainbowstream/colorset/monokai.json @@ -36,11 +36,13 @@ }, "MESSAGE" : { - "partner" : 112, - "me" : 112, - "to" : 50, - "clock" : 57, - "id" : 166 + "partner" : 112, + "me" : 112, + "me_bg" : 74, + "partner_bg" : 112, + "to" : 50, + "clock" : 57, + "id" : 166 }, "PROFILE" : { diff --git a/rainbowstream/draw.py b/rainbowstream/draw.py index e123ba9..747a68b 100644 --- a/rainbowstream/draw.py +++ b/rainbowstream/draw.py @@ -340,7 +340,6 @@ def print_thread(partner, me_nick, me_name): # Sort messages by time messages = dg['thread'][partner] messages.sort(key = lambda x:parser.parse(x['created_at'])) - # Print the 1st line dg['message_thread_margin'] = margin = 2 left_size = len(partner[0])+len(partner[1]) + margin @@ -357,7 +356,6 @@ def print_thread(partner, me_nick, me_name): printNicely('') printNicely(line) printNicely('') - # Print messages for m in messages: if m['sender_screen_name'] == me_nick: @@ -377,7 +375,8 @@ def print_right_message(m): slicing = [m['text'][i:i+step] for i in range(0, len(m['text']), step)] spaces = w - frame_width - dg['message_thread_margin'] dotline = ' ' * spaces + '-' * frame_width - + dotline = color_func(c['MESSAGE']['me_bg'])(dotline) + # Draw the frame printNicely(dotline) for line in slicing: fill = step - len(line) @@ -386,8 +385,31 @@ def print_right_message(m): screen_line = screen_line + ' >' else: screen_line = screen_line + '|' + screen_line = color_func(c['MESSAGE']['me_bg'])(screen_line) printNicely(screen_line) printNicely(dotline) + # Print clock + date = parser.parse(m['created_at']) + date = arrow.get(date).to('local').datetime + clock_format = '%Y/%m/%d %H:%M:%S' + try: + clock_format = c['FORMAT']['MESSAGE']['CLOCK_FORMAT'] + except: + pass + clock = date.strftime(clock_format) + # Get rainbow id + if m['id'] not in c['message_dict']: + c['message_dict'].append(m['id']) + rid = len(c['message_dict']) - 1 + else: + rid = c['message_dict'].index(m['id']) + rid = str(rid) + # Create line and print + meta = color_func(c['MESSAGE']['clock'])(clock) + \ + color_func(c['MESSAGE']['id'])(' ('+rid+')') + line = ' ' * (w - len(clock + rid) - 3 - dg['message_thread_margin']) + \ + meta + printNicely(line) def print_left_message(m): @@ -401,7 +423,8 @@ def print_left_message(m): slicing = [m['text'][i:i+step] for i in range(0, len(m['text']), step)] spaces = dg['message_thread_margin'] dotline = ' ' * spaces + '-' * frame_width - + dotline = color_func(c['MESSAGE']['partner_bg'])(dotline) + # Draw the frame printNicely(dotline) for line in slicing: fill = step - len(line) @@ -410,8 +433,31 @@ def print_left_message(m): screen_line = ' ' * (spaces-1) + '< ' + screen_line else: screen_line = ' ' * spaces + '|' + screen_line + screen_line = color_func(c['MESSAGE']['partner_bg'])(screen_line) printNicely(screen_line) printNicely(dotline) + # Print clock + date = parser.parse(m['created_at']) + date = arrow.get(date).to('local').datetime + clock_format = '%Y/%m/%d %H:%M:%S' + try: + clock_format = c['FORMAT']['MESSAGE']['CLOCK_FORMAT'] + except: + pass + clock = date.strftime(clock_format) + # Get rainbow id + if m['id'] not in c['message_dict']: + c['message_dict'].append(m['id']) + rid = len(c['message_dict']) - 1 + else: + rid = c['message_dict'].index(m['id']) + rid = str(rid) + # Create line and print + meta = color_func(c['MESSAGE']['clock'])(clock) + \ + color_func(c['MESSAGE']['id'])(' ('+rid+')') + line = ' ' * dg['message_thread_margin'] + \ + meta + printNicely(line) def print_message(m): diff --git a/rainbowstream/interactive.py b/rainbowstream/interactive.py index 45b7853..76755e8 100644 --- a/rainbowstream/interactive.py +++ b/rainbowstream/interactive.py @@ -68,15 +68,20 @@ def read_history(): """ Read history file """ - if os.path.isfile(c['HISTORY_FILENAME']): + try: readline.read_history_file(c['HISTORY_FILENAME']) + except: + pass def save_history(): """ Save history to file """ - readline.write_history_file(c['HISTORY_FILENAME']) + try: + readline.write_history_file(c['HISTORY_FILENAME']) + except: + pass def init_interactive_shell(d): -- 2.25.1