From b1a551087c8954b0da96289925ef7121a34d7cca Mon Sep 17 00:00:00 2001 From: James O'Farrell Date: Sun, 1 Nov 2015 11:13:42 +1000 Subject: [PATCH 1/6] Added support for setting colours via command line; Added support for larger colour palettes (88 and 256) --- hangups/ui/__main__.py | 97 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 93 insertions(+), 4 deletions(-) diff --git a/hangups/ui/__main__.py b/hangups/ui/__main__.py index c5ba8181..fbf01a3b 100644 --- a/hangups/ui/__main__.py +++ b/hangups/ui/__main__.py @@ -41,8 +41,8 @@ class ChatUI(object): """User interface for hangups.""" - def __init__(self, refresh_token_path, keybindings, palette, datetimefmt, - disable_notifier): + def __init__(self, refresh_token_path, keybindings, palette, palette_colors, + datetimefmt, disable_notifier): """Start the user interface.""" self._keys = keybindings self._datetimefmt = datetimefmt @@ -73,6 +73,7 @@ def __init__(self, refresh_token_path, keybindings, palette, datetimefmt, event_loop=urwid.AsyncioEventLoop(loop=loop) ) + self._urwid_loop.screen.set_terminal_properties(colors=palette_colors) self._urwid_loop.start() try: # Returns when the connection is closed. @@ -822,6 +823,27 @@ def set_terminal_title(title): """Use an xterm escape sequence to set the terminal title.""" sys.stdout.write("\x1b]2;{}\x07".format(title)) +def add_color_to_scheme(scheme,name,foreground,background,palette_colors): + """Add forground and background colours to a color scheme""" + if foreground == None and background == None: + return scheme + + if foreground == None: + forground = "" + if background == None: + background = "" + + new_scheme = [] + for item in scheme: + if item[0] == name: + if palette_colors > 16: + new_scheme.append((name,'','','',foreground,background)) + else: + new_scheme.append((name,foreground,background)) + else: + new_scheme.append(item) + return new_scheme + def dir_maker(path): """Create a directory if it does not exist.""" @@ -887,6 +909,37 @@ def main(): help='keybinding for alternate up key') key_group.add('--key-down', default='j', help='keybinding for alternate down key') + col_group = parser.add_argument_group('Colors') + col_group.add('--col-palette-colors',choices=('16','88','265'), + default=16, help='Amount of available colors') + col_group.add('--col-active-tab-fg', + help='Active tab foreground color') + col_group.add('--col-active-tab-bg', + help='Active tab background color') + col_group.add('--col-inactive-tab-fg', + help='Inactive tab foreground color') + col_group.add('--col-inactive-tab-bg', + help='Inactive tab background color') + col_group.add('--col-msg-date-fg', + help='Message date foreground color') + col_group.add('--col-msg-date-bg', + help='Message date background color') + col_group.add('--col-msg-sender-fg', + help='Message sender foreground color') + col_group.add('--col-msg-sender-bg', + help='Message sender background color') + col_group.add('--col-msg-text-fg', + help='Message text foreground color') + col_group.add('--col-msg-text-bg', + help='Message text background color') + col_group.add('--col-status-line-fg', + help='Status line foreground color') + col_group.add('--col-status-line-bg', + help='Status line background color') + col_group.add('--col-tab-background-fg', + help='tab-background foreground color') + col_group.add('--col-tab-background-bg', + help='tab background background color') args = parser.parse_args() # Create all necessary directories. @@ -901,6 +954,42 @@ def main(): datetimefmt = {'date': args.date_format, 'time': args.time_format} + #setup color scheme + try: + palette_colors = int(args.col_palette_colors) + except ValueError: + palette_colors = 16 + + col_scheme = COL_SCHEMES[args.col_scheme] + col_scheme = add_color_to_scheme(col_scheme,'active_tab', + args.col_active_tab_fg, + args.col_active_tab_bg, + palette_colors) + col_scheme = add_color_to_scheme(col_scheme,'inactive_tab', + args.col_inactive_tab_fg, + args.col_inactive_tab_bg, + palette_colors) + col_scheme = add_color_to_scheme(col_scheme,'msg_date', + args.col_msg_date_fg, + args.col_msg_date_bg, + palette_colors) + col_scheme = add_color_to_scheme(col_scheme,'msg_sender', + args.col_msg_sender_fg, + args.col_msg_sender_bg, + palette_colors) + col_scheme = add_color_to_scheme(col_scheme,'msg_text', + args.col_msg_text_fg, + args.col_msg_text_bg, + palette_colors) + col_scheme = add_color_to_scheme(col_scheme,'status_line', + args.col_status_line_fg, + args.col_status_line_bg, + palette_colors) + col_scheme = add_color_to_scheme(col_scheme,'tab_background', + args.col_tab_background_fg, + args.col_tab_background_bg, + palette_colors) + try: ChatUI( args.token_path, { @@ -911,8 +1000,8 @@ def main(): 'menu': args.key_menu, 'up': args.key_up, 'down': args.key_down - }, COL_SCHEMES[args.col_scheme], datetimefmt, - args.disable_notifications + }, col_scheme, palette_colors, + datetimefmt, args.disable_notifications ) except KeyboardInterrupt: sys.exit('Caught KeyboardInterrupt, exiting abnormally') From f1bff1d2f62de79546b983a5c85f7e8f03054cb6 Mon Sep 17 00:00:00 2001 From: James O'Farrell Date: Sun, 1 Nov 2015 12:22:56 +1000 Subject: [PATCH 2/6] Fixed styles issues, moved add_color_to_scheme to utils --- hangups/ui/__main__.py | 102 +++++++++-------------------------------- hangups/ui/utils.py | 22 +++++++++ 2 files changed, 43 insertions(+), 81 deletions(-) diff --git a/hangups/ui/__main__.py b/hangups/ui/__main__.py index fbf01a3b..d81fbdfb 100644 --- a/hangups/ui/__main__.py +++ b/hangups/ui/__main__.py @@ -12,6 +12,7 @@ import hangups from hangups.ui.notify import Notifier from hangups.ui.utils import get_conv_name +from hangups.ui.utils import add_color_to_scheme LOG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' @@ -36,13 +37,15 @@ ('tab_background', 'underline', 'black'), }, } +COL_SCHEME_NAMES = ('active_tab', 'inactive_tab', 'msg_date', 'msg_sender', + 'msg_text', 'status_line', 'tab_background') class ChatUI(object): """User interface for hangups.""" - def __init__(self, refresh_token_path, keybindings, palette, palette_colors, - datetimefmt, disable_notifier): + def __init__(self, refresh_token_path, keybindings, palette, + palette_colors, datetimefmt, disable_notifier): """Start the user interface.""" self._keys = keybindings self._datetimefmt = datetimefmt @@ -823,27 +826,6 @@ def set_terminal_title(title): """Use an xterm escape sequence to set the terminal title.""" sys.stdout.write("\x1b]2;{}\x07".format(title)) -def add_color_to_scheme(scheme,name,foreground,background,palette_colors): - """Add forground and background colours to a color scheme""" - if foreground == None and background == None: - return scheme - - if foreground == None: - forground = "" - if background == None: - background = "" - - new_scheme = [] - for item in scheme: - if item[0] == name: - if palette_colors > 16: - new_scheme.append((name,'','','',foreground,background)) - else: - new_scheme.append((name,foreground,background)) - else: - new_scheme.append(item) - return new_scheme - def dir_maker(path): """Create a directory if it does not exist.""" @@ -909,37 +891,18 @@ def main(): help='keybinding for alternate up key') key_group.add('--key-down', default='j', help='keybinding for alternate down key') + + #add color scheme options col_group = parser.add_argument_group('Colors') - col_group.add('--col-palette-colors',choices=('16','88','265'), + col_group.add('--col-palette-colors', choices=('16', '88', '265'), default=16, help='Amount of available colors') - col_group.add('--col-active-tab-fg', - help='Active tab foreground color') - col_group.add('--col-active-tab-bg', - help='Active tab background color') - col_group.add('--col-inactive-tab-fg', - help='Inactive tab foreground color') - col_group.add('--col-inactive-tab-bg', - help='Inactive tab background color') - col_group.add('--col-msg-date-fg', - help='Message date foreground color') - col_group.add('--col-msg-date-bg', - help='Message date background color') - col_group.add('--col-msg-sender-fg', - help='Message sender foreground color') - col_group.add('--col-msg-sender-bg', - help='Message sender background color') - col_group.add('--col-msg-text-fg', - help='Message text foreground color') - col_group.add('--col-msg-text-bg', - help='Message text background color') - col_group.add('--col-status-line-fg', - help='Status line foreground color') - col_group.add('--col-status-line-bg', - help='Status line background color') - col_group.add('--col-tab-background-fg', - help='tab-background foreground color') - col_group.add('--col-tab-background-bg', - help='tab background background color') + for name in COL_SCHEME_NAMES: + new_name = name.replace('_', '-') + col_group.add('--col-' + new_name + '-fg', + help=name + ' foreground color') + col_group.add('--col-' + new_name + '-bg', + help=name + ' background color') + args = parser.parse_args() # Create all necessary directories. @@ -961,35 +924,12 @@ def main(): palette_colors = 16 col_scheme = COL_SCHEMES[args.col_scheme] - col_scheme = add_color_to_scheme(col_scheme,'active_tab', - args.col_active_tab_fg, - args.col_active_tab_bg, - palette_colors) - col_scheme = add_color_to_scheme(col_scheme,'inactive_tab', - args.col_inactive_tab_fg, - args.col_inactive_tab_bg, - palette_colors) - col_scheme = add_color_to_scheme(col_scheme,'msg_date', - args.col_msg_date_fg, - args.col_msg_date_bg, - palette_colors) - col_scheme = add_color_to_scheme(col_scheme,'msg_sender', - args.col_msg_sender_fg, - args.col_msg_sender_bg, - palette_colors) - col_scheme = add_color_to_scheme(col_scheme,'msg_text', - args.col_msg_text_fg, - args.col_msg_text_bg, - palette_colors) - col_scheme = add_color_to_scheme(col_scheme,'status_line', - args.col_status_line_fg, - args.col_status_line_bg, - palette_colors) - col_scheme = add_color_to_scheme(col_scheme,'tab_background', - args.col_tab_background_fg, - args.col_tab_background_bg, - palette_colors) - + for name in COL_SCHEME_NAMES: + col_scheme = add_color_to_scheme(col_scheme, name, + eval('args.col_' + name + '_fg'), + eval('args.col_' + name + '_bg'), + palette_colors) + try: ChatUI( args.token_path, { diff --git a/hangups/ui/utils.py b/hangups/ui/utils.py index 2f4554eb..16c8bd70 100644 --- a/hangups/ui/utils.py +++ b/hangups/ui/utils.py @@ -40,3 +40,25 @@ def get_conv_name(conv, truncate=False, show_unread=False): postfix) else: return ', '.join(names) + postfix + +def add_color_to_scheme(scheme, name, foreground, background, palette_colors): + """Add foreground and background colours to a color scheme""" + if foreground == None and background == None: + return scheme + + new_scheme = [] + for item in scheme: + if item[0] == name: + if foreground == None: + foreground = item[1] + if background == None: + background = item[2] + if palette_colors > 16: + new_scheme.append((name, '', '', '', foreground, background)) + else: + new_scheme.append((name, foreground, background)) + else: + new_scheme.append(item) + return new_scheme + + From 439ef2792277b23c5348a1c5c1228fee91dad930 Mon Sep 17 00:00:00 2001 From: James O'Farrell Date: Sun, 1 Nov 2015 13:01:28 +1000 Subject: [PATCH 3/6] reduces complexity --- hangups/ui/__main__.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/hangups/ui/__main__.py b/hangups/ui/__main__.py index d81fbdfb..862fa6a0 100644 --- a/hangups/ui/__main__.py +++ b/hangups/ui/__main__.py @@ -897,20 +897,20 @@ def main(): col_group.add('--col-palette-colors', choices=('16', '88', '265'), default=16, help='Amount of available colors') for name in COL_SCHEME_NAMES: - new_name = name.replace('_', '-') - col_group.add('--col-' + new_name + '-fg', + col_group.add('--col-' + name.replace('_', '-') + '-fg', help=name + ' foreground color') - col_group.add('--col-' + new_name + '-bg', + col_group.add('--col-' + name.replace('_', '-') + '-bg', help=name + ' background color') - + args = parser.parse_args() # Create all necessary directories. for path in [args.log, args.token_path]: dir_maker(path) - log_level = logging.DEBUG if args.debug else logging.WARNING - logging.basicConfig(filename=args.log, level=log_level, format=LOG_FORMAT) + logging.basicConfig(filename=args.log, + level=logging.DEBUG if args.debug else logging.WARNING, + format=LOG_FORMAT) # urwid makes asyncio's debugging logs VERY noisy, so adjust the log level: logging.getLogger('asyncio').setLevel(logging.WARNING) @@ -918,18 +918,15 @@ def main(): 'time': args.time_format} #setup color scheme - try: - palette_colors = int(args.col_palette_colors) - except ValueError: - palette_colors = 16 + palette_colors = int(args.col_palette_colors) col_scheme = COL_SCHEMES[args.col_scheme] for name in COL_SCHEME_NAMES: col_scheme = add_color_to_scheme(col_scheme, name, - eval('args.col_' + name + '_fg'), - eval('args.col_' + name + '_bg'), + getattr(args, 'col_' + name + '_fg'), + getattr(args, 'col_' + name + '_bg'), palette_colors) - + try: ChatUI( args.token_path, { From 2557eb60dbc97eaacc400e688ac8924a5e27e96c Mon Sep 17 00:00:00 2001 From: James O'Farrell Date: Sun, 1 Nov 2015 13:48:49 +1000 Subject: [PATCH 4/6] support for msg_self name color --- hangups/ui/__main__.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/hangups/ui/__main__.py b/hangups/ui/__main__.py index 862fa6a0..3c012924 100644 --- a/hangups/ui/__main__.py +++ b/hangups/ui/__main__.py @@ -23,6 +23,7 @@ ('inactive_tab', 'standout', ''), ('msg_date', '', ''), ('msg_sender', '', ''), + ('msg_self', '', ''), ('msg_text', '', ''), ('status_line', 'standout', ''), ('tab_background', 'standout', ''), @@ -32,13 +33,14 @@ ('inactive_tab', 'underline', 'light green'), ('msg_date', 'dark cyan', ''), ('msg_sender', 'dark blue', ''), + ('msg_self', 'light blue', ''), ('msg_text', '', ''), ('status_line', 'standout', ''), ('tab_background', 'underline', 'black'), }, } COL_SCHEME_NAMES = ('active_tab', 'inactive_tab', 'msg_date', 'msg_sender', - 'msg_text', 'status_line', 'tab_background') + 'msg_self', 'msg_text', 'status_line', 'tab_background') class ChatUI(object): @@ -442,7 +444,8 @@ def __init__(self, timestamp, text, datetimefmt, user=None, ('msg_text', text) ] if user is not None: - text.insert(1, ('msg_sender', user.first_name + ': ')) + text.insert(1, ('msg_self' if user.is_self else 'msg_sender', + user.first_name + ': ')) self._widget = urwid.Text(text) super().__init__(self._widget) From 476c559d2faaeb458130cef674e16444e1bf7a54 Mon Sep 17 00:00:00 2001 From: James O'Farrell Date: Mon, 2 Nov 2015 19:16:02 +1000 Subject: [PATCH 5/6] Moved --col-scheme to col_grounp and cleaned up all code in hangups/ui/ so it passes the standard pep8 tests --- hangups/ui/__main__.py | 8 ++++---- hangups/ui/notify.py | 5 ++++- hangups/ui/utils.py | 9 ++++----- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/hangups/ui/__main__.py b/hangups/ui/__main__.py index 3c012924..127db2c9 100644 --- a/hangups/ui/__main__.py +++ b/hangups/ui/__main__.py @@ -864,8 +864,6 @@ def main(): help='show this help message and exit') general_group.add('--token-path', default=default_token_path, help='path used to store OAuth refresh token') - general_group.add('--col-scheme', choices=COL_SCHEMES.keys(), - default='default', help='colour scheme to use') general_group.add('--date-format', default='< %y-%m-%d >', help='date format string') general_group.add('--time-format', default='(%I:%M:%S %p)', @@ -895,8 +893,10 @@ def main(): key_group.add('--key-down', default='j', help='keybinding for alternate down key') - #add color scheme options + # add color scheme options col_group = parser.add_argument_group('Colors') + col_group.add('--col-scheme', choices=COL_SCHEMES.keys(), + default='default', help='colour scheme to use') col_group.add('--col-palette-colors', choices=('16', '88', '265'), default=16, help='Amount of available colors') for name in COL_SCHEME_NAMES: @@ -920,7 +920,7 @@ def main(): datetimefmt = {'date': args.date_format, 'time': args.time_format} - #setup color scheme + # setup color scheme palette_colors = int(args.col_palette_colors) col_scheme = COL_SCHEMES[args.col_scheme] diff --git a/hangups/ui/notify.py b/hangups/ui/notify.py index c0b0c8bc..8b77a350 100644 --- a/hangups/ui/notify.py +++ b/hangups/ui/notify.py @@ -24,7 +24,9 @@ 'title "{convo_name}" ' 'subtitle "{sender_name}"'), ] - NOTIFY_ESCAPER = lambda s: s.replace('"', '\\"') + + def NOTIFY_ESCAPER(s): + return s.replace('"', '\\"') else: NOTIFY_CMD = [ 'gdbus', 'call', '--session', '--dest', @@ -33,6 +35,7 @@ 'org.freedesktop.Notifications.Notify', 'hangups', '{replaces_id}', '', '{sender_name}', '{msg_text}', '[]', '{{}}', ' -1' ] + def NOTIFY_ESCAPER(text): """Escape text for passing into gdbus.""" # Prevent the notifier from interpreting markup: diff --git a/hangups/ui/utils.py b/hangups/ui/utils.py index 16c8bd70..fd7b89d9 100644 --- a/hangups/ui/utils.py +++ b/hangups/ui/utils.py @@ -41,17 +41,18 @@ def get_conv_name(conv, truncate=False, show_unread=False): else: return ', '.join(names) + postfix + def add_color_to_scheme(scheme, name, foreground, background, palette_colors): """Add foreground and background colours to a color scheme""" - if foreground == None and background == None: + if foreground is None and background is None: return scheme new_scheme = [] for item in scheme: if item[0] == name: - if foreground == None: + if foreground is None: foreground = item[1] - if background == None: + if background is None: background = item[2] if palette_colors > 16: new_scheme.append((name, '', '', '', foreground, background)) @@ -60,5 +61,3 @@ def add_color_to_scheme(scheme, name, foreground, background, palette_colors): else: new_scheme.append(item) return new_scheme - - From 978a9dccb977b065c414fb623d1be441e5dfc1fa Mon Sep 17 00:00:00 2001 From: James O'Farrell Date: Wed, 4 Nov 2015 04:57:42 +1000 Subject: [PATCH 6/6] 256 not 265 --- hangups/ui/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hangups/ui/__main__.py b/hangups/ui/__main__.py index 127db2c9..7291fc10 100644 --- a/hangups/ui/__main__.py +++ b/hangups/ui/__main__.py @@ -897,7 +897,7 @@ def main(): col_group = parser.add_argument_group('Colors') col_group.add('--col-scheme', choices=COL_SCHEMES.keys(), default='default', help='colour scheme to use') - col_group.add('--col-palette-colors', choices=('16', '88', '265'), + col_group.add('--col-palette-colors', choices=('16', '88', '256'), default=16, help='Amount of available colors') for name in COL_SCHEME_NAMES: col_group.add('--col-' + name.replace('_', '-') + '-fg',