From 03c0d30be5c9eff80893cce9320a01ef3ce918a5 Mon Sep 17 00:00:00 2001 From: vunhat_minh Date: Mon, 25 Aug 2014 14:22:04 +0900 Subject: [PATCH] working fine in CentOS --- rainbowstream/colorset/config | 10 +- rainbowstream/colorset/larapaste.json | 14 ++- rainbowstream/colorset/monokai.json | 6 +- rainbowstream/colorset/solarized.json | 14 ++- rainbowstream/colorset/tomorrow_night.json | 14 ++- rainbowstream/draw.py | 133 ++++++++++++++------- rainbowstream/py3patch.py | 7 ++ rainbowstream/rainbow.py | 54 +++++---- 8 files changed, 161 insertions(+), 91 deletions(-) diff --git a/rainbowstream/colorset/config b/rainbowstream/colorset/config index 956dd36..47deb6e 100644 --- a/rainbowstream/colorset/config +++ b/rainbowstream/colorset/config @@ -21,6 +21,9 @@ "CONVERSATION_MAX" : 30, // 'quote' format "QUOTE_FORMAT" : "#comment RT #owner: #tweet", + // 'thread' meta format + "THREAD_META_LEFT" : "(#id) #clock", + "THREAD_META_RIGHT" : "#clock (#id)", // 'inbox','sent': default number of direct message "MESSAGES_DISPLAY" : 5, // 'trend': max trending topics @@ -32,8 +35,9 @@ "IGNORE_LIST" : [], // Autocomplete history file name "HISTORY_FILENAME" : "completer.hist", - // Image config + // Image margin "IMAGE_SHIFT" : 2, + // Image max height "IMAGE_MAX_HEIGHT" : 90, // Stream config "USER_DOMAIN" : "userstream.twitter.com", @@ -47,7 +51,9 @@ }, "MESSAGE": { "CLOCK_FORMAT" : "%Y/%m/%d %H:%M:%S", - "DISPLAY" : "\n #sender_name #sender_nick #to #recipient_name #recipient_nick :\n #clock message_id:#id\n #message" + "DISPLAY" : "\n #sender_name #sender_nick #to #recipient_name #recipient_nick :\n #clock message_id:#id\n #message", + "THREAD_META_LEFT" : "[id:#id] #clock", + "THREAD_META_RIGHT" : "#clock [id:#id]" } } } diff --git a/rainbowstream/colorset/larapaste.json b/rainbowstream/colorset/larapaste.json index 3f4011e..b599bd2 100644 --- a/rainbowstream/colorset/larapaste.json +++ b/rainbowstream/colorset/larapaste.json @@ -36,11 +36,15 @@ }, "MESSAGE" : { - "partner" : 37, - "me" : 37, - "to" : 154, - "clock" : 184, - "id" : 202 + "partner" : 37, + "me" : 37, + "me_frame" : 154, + "partner_frame" : "white", + "sender" : 37, + "recipient" : 37, + "to" : 154, + "clock" : 184, + "id" : 202 }, "PROFILE" : { diff --git a/rainbowstream/colorset/monokai.json b/rainbowstream/colorset/monokai.json index 553be6a..736ebec 100644 --- a/rainbowstream/colorset/monokai.json +++ b/rainbowstream/colorset/monokai.json @@ -38,8 +38,10 @@ "MESSAGE" : { "partner" : 112, "me" : 112, - "me_bg" : 74, - "partner_bg" : 112, + "me_frame" : 74, + "partner_frame" : "white", + "sender" : 112, + "recipient" : 112, "to" : 50, "clock" : 57, "id" : 166 diff --git a/rainbowstream/colorset/solarized.json b/rainbowstream/colorset/solarized.json index a0ee32f..65bb09a 100644 --- a/rainbowstream/colorset/solarized.json +++ b/rainbowstream/colorset/solarized.json @@ -36,11 +36,15 @@ }, "MESSAGE" : { - "partner" : 64, - "me" : 64, - "to" : 130, - "clock" : 32, - "id" : 124 + "partner" : 64, + "me" : 64, + "me_frame" : 23, + "partner_frame" : "white", + "sender" : 64, + "recipient" : 64, + "to" : 130, + "clock" : 32, + "id" : 124 }, "PROFILE" : { diff --git a/rainbowstream/colorset/tomorrow_night.json b/rainbowstream/colorset/tomorrow_night.json index 80557ba..31caa8c 100644 --- a/rainbowstream/colorset/tomorrow_night.json +++ b/rainbowstream/colorset/tomorrow_night.json @@ -36,11 +36,15 @@ }, "MESSAGE" : { - "partner" : 67, - "me" : 67, - "to" : 97, - "clock" : 58, - "id" : 166 + "partner" : 67, + "me" : 67, + "me_frame" : 30, + "partner_frame" : "white", + "sender" : 67, + "recipient" : 67, + "to" : 97, + "clock" : 58, + "id" : 166 }, "PROFILE" : { diff --git a/rainbowstream/draw.py b/rainbowstream/draw.py index 747a68b..fe13d17 100644 --- a/rainbowstream/draw.py +++ b/rainbowstream/draw.py @@ -276,21 +276,21 @@ def draw(t, keyword=None, humanize=True, fil=[], ig=[]): formater = fav.join(formater.split('#fav')) formater = tweet.join(formater.split('#tweet')) # Change clock word - word = [w for w in formater.split() if '#clock' in w][0] + word = [wo for wo in formater.split() if '#clock' in wo][0] delimiter = color_func(c['TWEET']['clock'])( clock.join(word.split('#clock'))) formater = delimiter.join(formater.split(word)) # Change id word - word = [w for w in formater.split() if '#id' in w][0] + word = [wo for wo in formater.split() if '#id' in wo][0] delimiter = color_func(c['TWEET']['id'])(id.join(word.split('#id'))) formater = delimiter.join(formater.split(word)) # Change retweet count word - word = [w for w in formater.split() if '#rt_count' in w][0] + word = [wo for wo in formater.split() if '#rt_count' in wo][0] delimiter = color_func(c['TWEET']['retweet_count'])( str(retweet_count).join(word.split('#rt_count'))) formater = delimiter.join(formater.split(word)) # Change favorites count word - word = [w for w in formater.split() if '#fa_count' in w][0] + word = [wo for wo in formater.split() if '#fa_count' in wo][0] delimiter = color_func(c['TWEET']['favorite_count'])( str(favorite_count).join(word.split('#fa_count'))) formater = delimiter.join(formater.split(word)) @@ -323,8 +323,8 @@ def print_threads(d): name = partner[1] screen_name = color_func(c['MESSAGE']['partner'])(screen_name) name = cycle_color(name) - thread_id = color_func(c['MESSAGE']['id'])('thread id:'+str(id)) - line = ' '*2 + name + ' ' + screen_name + \ + thread_id = color_func(c['MESSAGE']['id'])('thread_id:' + str(id)) + line = ' ' * 2 + name + ' ' + screen_name + \ ' (' + str(count) + ' message) ' + thread_id printNicely(line) rel[id] = partner @@ -339,20 +339,31 @@ 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 - right_size = len(me_nick) + len(me_name) + margin - partner_screen_name = color_func(c['MESSAGE']['partner'])('@' + partner[0]) - partner_name = cycle_color(partner[1]) + messages.sort(key=lambda x: parser.parse(x['created_at'])) + # Use legacy display on non-ascii text message + text_ary = [m['text'] for m in messages] + not_ascii_text_ary = [t for t in text_ary if not is_ascii(t)] + if not_ascii_text_ary: + for m in messages: + print_message(m) + printNicely('') + return + # Print the first line + dg['frame_margin'] = margin = 2 + partner_nick = partner[0] + partner_name = partner[1] + left_size = len(partner_nick) + len(partner_name) + 2 + right_size = len(me_nick) + len(me_name) + 2 + partner_nick = color_func(c['MESSAGE']['partner'])('@' + partner_nick) + partner_name = cycle_color(partner_name) me_screen_name = color_func(c['MESSAGE']['me'])('@' + me_nick) me_name = cycle_color(me_name) - left = ' ' * margin + partner_name + ' ' + partner_screen_name + left = ' ' * margin + partner_name + ' ' + partner_nick right = me_name + ' ' + me_screen_name + ' ' * margin h, w = os.popen('stty size', 'r').read().split() w = int(w) - line = '{}{}{}'.format(left, ' '*(w - left_size - right_size - 2 * margin), right) + line = '{}{}{}'.format( + left, ' ' * (w - left_size - right_size - 2 * margin), right) printNicely('') printNicely(line) printNicely('') @@ -370,12 +381,12 @@ def print_right_message(m): """ h, w = os.popen('stty size', 'r').read().split() w = int(w) - frame_width = w //3 - dg['message_thread_margin'] - step = frame_width - 2 * dg['message_thread_margin'] - slicing = [m['text'][i:i+step] for i in range(0, len(m['text']), step)] - spaces = w - frame_width - dg['message_thread_margin'] + frame_width = w // 3 - dg['frame_margin'] + step = frame_width - 2 * dg['frame_margin'] + slicing = [m['text'][i:i + step] for i in range(0, len(m['text']), step)] + spaces = w - frame_width - dg['frame_margin'] dotline = ' ' * spaces + '-' * frame_width - dotline = color_func(c['MESSAGE']['me_bg'])(dotline) + dotline = color_func(c['MESSAGE']['me_frame'])(dotline) # Draw the frame printNicely(dotline) for line in slicing: @@ -385,10 +396,10 @@ def print_right_message(m): screen_line = screen_line + ' >' else: screen_line = screen_line + '|' - screen_line = color_func(c['MESSAGE']['me_bg'])(screen_line) + screen_line = color_func(c['MESSAGE']['me_frame'])(screen_line) printNicely(screen_line) printNicely(dotline) - # Print clock + # Format clock date = parser.parse(m['created_at']) date = arrow.get(date).to('local').datetime clock_format = '%Y/%m/%d %H:%M:%S' @@ -397,18 +408,33 @@ def print_right_message(m): except: pass clock = date.strftime(clock_format) - # Get rainbow id + # Format 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 + id = str(rid) + # Print meta + formater = '' + try: + virtual_meta = formater = c['THREAD_META_RIGHT'] + virtual_meta = clock.join(virtual_meta.split('#clock')) + virtual_meta = id.join(virtual_meta.split('#id')) + # Change clock word + word = [wo for wo in formater.split() if '#clock' in wo][0] + delimiter = color_func(c['MESSAGE']['clock'])( + clock.join(word.split('#clock'))) + formater = delimiter.join(formater.split(word)) + # Change id word + word = [wo for wo in formater.split() if '#id' in wo][0] + delimiter = color_func(c['MESSAGE']['id'])(id.join(word.split('#id'))) + formater = delimiter.join(formater.split(word)) + except Exception: + printNicely(red('Wrong format in config.')) + return + meta = formater + line = ' ' * (w - len(virtual_meta) - dg['frame_margin']) + meta printNicely(line) @@ -418,25 +444,25 @@ def print_left_message(m): """ h, w = os.popen('stty size', 'r').read().split() w = int(w) - frame_width = w //3 - dg['message_thread_margin'] - step = frame_width - 2 * dg['message_thread_margin'] - slicing = [m['text'][i:i+step] for i in range(0, len(m['text']), step)] - spaces = dg['message_thread_margin'] + frame_width = w // 3 - dg['frame_margin'] + step = frame_width - 2 * dg['frame_margin'] + slicing = [m['text'][i:i + step] for i in range(0, len(m['text']), step)] + spaces = dg['frame_margin'] dotline = ' ' * spaces + '-' * frame_width - dotline = color_func(c['MESSAGE']['partner_bg'])(dotline) + dotline = color_func(c['MESSAGE']['partner_frame'])(dotline) # Draw the frame printNicely(dotline) for line in slicing: fill = step - len(line) screen_line = ' ' + line + ' ' * fill + ' |' if slicing[-1] == line: - screen_line = ' ' * (spaces-1) + '< ' + screen_line + screen_line = ' ' * (spaces - 1) + '< ' + screen_line else: screen_line = ' ' * spaces + '|' + screen_line - screen_line = color_func(c['MESSAGE']['partner_bg'])(screen_line) + screen_line = color_func(c['MESSAGE']['partner_frame'])(screen_line) printNicely(screen_line) printNicely(dotline) - # Print clock + # Format clock date = parser.parse(m['created_at']) date = arrow.get(date).to('local').datetime clock_format = '%Y/%m/%d %H:%M:%S' @@ -445,18 +471,33 @@ def print_left_message(m): except: pass clock = date.strftime(clock_format) - # Get rainbow id + # Format 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 + id = str(rid) + # Print meta + formater = '' + try: + virtual_meta = formater = c['THREAD_META_LEFT'] + virtual_meta = clock.join(virtual_meta.split('#clock')) + virtual_meta = id.join(virtual_meta.split('#id')) + # Change clock word + word = [wo for wo in formater.split() if '#clock' in wo][0] + delimiter = color_func(c['MESSAGE']['clock'])( + clock.join(word.split('#clock'))) + formater = delimiter.join(formater.split(word)) + # Change id word + word = [wo for wo in formater.split() if '#id' in wo][0] + delimiter = color_func(c['MESSAGE']['id'])(id.join(word.split('#id'))) + formater = delimiter.join(formater.split(word)) + except Exception: + printNicely(red('Wrong format in config.')) + return + meta = formater + line = ' ' * dg['frame_margin'] + meta printNicely(line) @@ -509,12 +550,12 @@ def print_message(m): formater = recipient_nick.join(formater.split("#recipient_nick")) formater = text.join(formater.split("#message")) # Change clock word - word = [w for w in formater.split() if '#clock' in w][0] + word = [wo for wo in formater.split() if '#clock' in wo][0] delimiter = color_func(c['MESSAGE']['clock'])( clock.join(word.split('#clock'))) formater = delimiter.join(formater.split(word)) # Change id word - word = [w for w in formater.split() if '#id' in w][0] + word = [wo for wo in formater.split() if '#id' in wo][0] delimiter = color_func(c['MESSAGE']['id'])(id.join(word.split('#id'))) formater = delimiter.join(formater.split(word)) except: diff --git a/rainbowstream/py3patch.py b/rainbowstream/py3patch.py index 4ba9591..35927ee 100644 --- a/rainbowstream/py3patch.py +++ b/rainbowstream/py3patch.py @@ -30,3 +30,10 @@ else: raw_input = input lmap = lambda f, a: list(map(f, a)) str2u = u2str = lambda x: x + + +def is_ascii(s): + """ + Check if multibyte character or not + """ + return all(ord(c) < 128 for c in s) diff --git a/rainbowstream/rainbow.py b/rainbowstream/rainbow.py index 6bd1720..8d93084 100644 --- a/rainbowstream/rainbow.py +++ b/rainbowstream/rainbow.py @@ -522,18 +522,20 @@ def inbox(): d = {} uniq_inbox = list(set( - [(m['sender_screen_name'],m['sender']['name']) for m in inbox] + [(m['sender_screen_name'], m['sender']['name']) for m in inbox] )) - uniq_sent= list(set( - [(m['recipient_screen_name'],m['recipient']['name']) for m in sent] + uniq_sent = list(set( + [(m['recipient_screen_name'], m['recipient']['name']) for m in sent] )) for partner in uniq_inbox: inbox_ary = [m for m in inbox if m['sender_screen_name'] == partner[0]] - sent_ary = [m for m in sent if m['recipient_screen_name'] == partner[0]] + sent_ary = [ + m for m in sent if m['recipient_screen_name'] == partner[0]] d[partner] = inbox_ary + sent_ary for partner in uniq_sent: if partner not in d: - d[partner] = [m for m in sent if m['recipient_screen_name'] == partner[0]] + d[partner] = [ + m for m in sent if m['recipient_screen_name'] == partner[0]] g['message_threads'] = print_threads(d) @@ -543,11 +545,11 @@ def thread(): """ try: thread_id = int(g['stuff']) - print_thread(g['message_threads'][thread_id],g['original_name'],g['full_name']) - except Exception as e: - print(e) - import traceback - print(traceback.format_exc()) + print_thread( + g['message_threads'][thread_id], + g['original_name'], + g['full_name']) + except Exception: printNicely(red('No such thread.')) @@ -556,19 +558,19 @@ def message(): Send a direct message """ t = Twitter(auth=authen()) - user = g['stuff'].split()[0] - if user[0].startswith('@'): - try: - content = g['stuff'].split()[1] - except: - printNicely(red('Sorry I can\'t understand.')) - t.direct_messages.new( - screen_name=user[1:], - text=content - ) - printNicely(green('Message sent.')) - else: - printNicely(red('A name should begin with a \'@\'')) + try: + user = g['stuff'].split()[0] + if user[0].startswith('@'): + content = ' '.join(g['stuff'].split()[1:]) + t.direct_messages.new( + screen_name=user[1:], + text=content + ) + printNicely(green('Message sent.')) + else: + printNicely(red('A name should begin with a \'@\'')) + except: + printNicely(red('Sorry I can\'t understand.')) def trash(): @@ -1274,8 +1276,8 @@ def help_messages(): usage += s + grey(u'\u266A' + ' Direct messages \n') usage += s * 2 + light_green('inbox') + ' will show inbox messages. ' + \ light_green('inbox 7') + ' will show newest 7 messages.\n' - usage += s * 2 + light_green('sent') + ' will show sent messages. ' + \ - light_green('sent 7') + ' will show newest 7 messages.\n' + usage += s * 2 + light_green('thread 2') + ' will show full thread with ' + \ + light_yellow('[thread_id=2]') + '.\n' usage += s * 2 + light_green('mes @dtvd88 hi') + ' will send a "hi" messege to ' + \ magenta('@dtvd88') + '.\n' usage += s * 2 + light_green('trash 5') + ' will remove message with ' + \ @@ -1638,7 +1640,7 @@ def listen(): [''], # open url ['fl', 'fr'], # list [], # inbox - [i for i in g['message_threads']], #sent + [i for i in g['message_threads']], # sent [], # trash ['@'], # whois ['@'], # follow -- 2.25.1