Avoid crash on certain terminals (workaround for #70)

This commit is contained in:
Lonami Exo 2017-05-07 13:33:35 +02:00
parent 49a76f23f5
commit db79ff08c7

View File

@ -13,7 +13,11 @@ def print_title(title):
# Clear previous window # Clear previous window
print('\n') print('\n')
print('=={}=='.format('=' * len(title))) print('=={}=='.format('=' * len(title)))
try:
print('= {} ='.format(title)) print('= {} ='.format(title))
except UnicodeEncodeError:
# Issue #70, some consoles lack support for certain characters
print('= {} ='.format('?' * len(title)))
print('=={}=='.format('=' * len(title))) print('=={}=='.format('=' * len(title)))
@ -77,13 +81,16 @@ class InteractiveTelegramClient(TelegramClient):
i = None i = None
while i is None: while i is None:
try:
print_title('Dialogs window') print_title('Dialogs window')
# Display them so the user can choose # Display them so the user can choose
for i, entity in enumerate(entities): for i, entity in enumerate(entities):
i += 1 # 1-based index for normies i += 1 # 1-based index for normies
try:
print('{}. {}'.format(i, get_display_name(entity))) print('{}. {}'.format(i, get_display_name(entity)))
except UnicodeEncodeError:
# Issue #70, some consoles lack support for certain characters
print('{}. {}'.format(i, '(could not render name)'))
# Let the user decide who they want to talk to # Let the user decide who they want to talk to
print() print()
@ -99,11 +106,11 @@ class InteractiveTelegramClient(TelegramClient):
self.log_out() self.log_out()
return return
try:
i = int(i if i else 0) - 1 i = int(i if i else 0) - 1
# Ensure it is inside the bounds, otherwise set to None and retry # Ensure it is inside the bounds, otherwise set to None and retry
if not 0 <= i < dialog_count: if not 0 <= i < dialog_count:
i = None i = None
except ValueError: except ValueError:
i = None i = None
@ -190,8 +197,7 @@ class InteractiveTelegramClient(TelegramClient):
print('Profile picture downloaded to {}'.format( print('Profile picture downloaded to {}'.format(
output)) output))
else: else:
print('"{}" does not seem to have a profile picture.' print('No profile picture found for this user.')
.format(get_display_name(entity)))
# Send chat message (if any) # Send chat message (if any)
elif msg: elif msg:
@ -254,6 +260,7 @@ class InteractiveTelegramClient(TelegramClient):
@staticmethod @staticmethod
def update_handler(update_object): def update_handler(update_object):
try:
if type(update_object) is UpdateShortMessage: if type(update_object) is UpdateShortMessage:
if update_object.out: if update_object.out:
print('You sent {} to user #{}'.format(update_object.message, print('You sent {} to user #{}'.format(update_object.message,
@ -270,3 +277,7 @@ class InteractiveTelegramClient(TelegramClient):
print('[Chat #{}, user #{} sent {}]'.format( print('[Chat #{}, user #{} sent {}]'.format(
update_object.chat_id, update_object.from_id, update_object.chat_id, update_object.from_id,
update_object.message)) update_object.message))
except UnicodeEncodeError:
# Issue #70, some consoles lack support for certain characters
print('(Could not print update since it contains '
'characters your terminal does not support)')