+ try:
+ response = requests.get(mu)
+ image_to_display(BytesIO(response.content))
+ except Exception:
+ printNicely(red('Sorry, image link is broken'))
+
+
+def print_threads(d):
+ """
+ Print threads of messages
+ """
+ id = 1
+ rel = {}
+ for partner in d:
+ messages = d[partner]
+ count = len(messages)
+ screen_name = '@' + partner[0]
+ 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 + \
+ ' (' + str(count) + ' message) ' + thread_id
+ printNicely(line)
+ rel[id] = partner
+ id += 1
+ dg['thread'] = d
+ return rel
+
+
+def print_thread(partner, me_nick, me_name):
+ """
+ Print a thread of messages
+ """
+ # Sort messages by time
+ messages = dg['thread'][partner]
+ messages.sort(key=lambda x: parser.parse(x['created_at']))
+ # Use legacy display on non-ascii text message
+ ms = [m['text'] for m in messages]
+ ums = [m for m in ms if not all(ord(c) < 128 for c in m)]
+ if ums:
+ 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_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)
+ printNicely('')
+ printNicely(line)
+ printNicely('')
+ # Print messages
+ for m in messages:
+ if m['sender_screen_name'] == me_nick:
+ print_right_message(m)
+ elif m['recipient_screen_name'] == me_nick:
+ print_left_message(m)
+
+
+def print_right_message(m):
+ """
+ Print a message on the right of screen
+ """
+ h, w = os.popen('stty size', 'r').read().split()
+ w = int(w)
+ frame_width = w // 3 - dg['frame_margin']
+ frame_width = max(c['THREAD_MIN_WIDTH'], frame_width)
+ step = frame_width - 2 * dg['frame_margin']
+ slicing = textwrap.wrap(m['text'], step)
+ spaces = w - frame_width - dg['frame_margin']
+ dotline = ' ' * spaces + '-' * frame_width
+ dotline = color_func(c['MESSAGE']['me_frame'])(dotline)
+ # Draw the frame
+ printNicely(dotline)
+ for line in slicing:
+ fill = step - len(line)
+ screen_line = ' ' * spaces + '| ' + line + ' ' * fill + ' '
+ if slicing[-1] == line:
+ screen_line = screen_line + ' >'
+ else:
+ screen_line = screen_line + '|'
+ screen_line = color_func(c['MESSAGE']['me_frame'])(screen_line)
+ printNicely(screen_line)
+ printNicely(dotline)
+ # Format 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)
+ # 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'])
+ 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))
+ formater = emojize(formater)
+ except Exception:
+ printNicely(red('Wrong format in config.'))
+ return
+ meta = formater
+ line = ' ' * (w - len(virtual_meta) - dg['frame_margin']) + meta
+ printNicely(line)
+
+
+def print_left_message(m):
+ """
+ Print a message on the left of screen
+ """
+ h, w = os.popen('stty size', 'r').read().split()
+ w = int(w)
+ frame_width = w // 3 - dg['frame_margin']
+ frame_width = max(c['THREAD_MIN_WIDTH'], frame_width)
+ step = frame_width - 2 * dg['frame_margin']
+ slicing = textwrap.wrap(m['text'], step)
+ spaces = dg['frame_margin']
+ dotline = ' ' * spaces + '-' * frame_width
+ 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
+ else:
+ screen_line = ' ' * spaces + '|' + screen_line
+ screen_line = color_func(c['MESSAGE']['partner_frame'])(screen_line)
+ printNicely(screen_line)
+ printNicely(dotline)
+ # Format 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)
+ # 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'])
+ 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))
+ formater = emojize(formater)
+ except Exception:
+ printNicely(red('Wrong format in config.'))
+ return
+ meta = formater
+ line = ' ' * dg['frame_margin'] + meta
+ printNicely(line)