diff --git a/hangups/ui/__main__.py b/hangups/ui/__main__.py index c5ba8181..7291fc10 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' @@ -22,6 +23,7 @@ ('inactive_tab', 'standout', ''), ('msg_date', '', ''), ('msg_sender', '', ''), + ('msg_self', '', ''), ('msg_text', '', ''), ('status_line', 'standout', ''), ('tab_background', 'standout', ''), @@ -31,18 +33,21 @@ ('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_self', 'msg_text', 'status_line', 'tab_background') 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 +78,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. @@ -438,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) @@ -857,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)', @@ -887,20 +892,44 @@ 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-scheme', choices=COL_SCHEMES.keys(), + default='default', help='colour scheme to use') + 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', + help=name + ' foreground color') + 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) datetimefmt = {'date': args.date_format, 'time': args.time_format} + # setup color scheme + 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, + getattr(args, 'col_' + name + '_fg'), + getattr(args, 'col_' + name + '_bg'), + palette_colors) + try: ChatUI( args.token_path, { @@ -911,8 +940,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') 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 2f4554eb..fd7b89d9 100644 --- a/hangups/ui/utils.py +++ b/hangups/ui/utils.py @@ -40,3 +40,24 @@ 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 is None and background is None: + return scheme + + new_scheme = [] + for item in scheme: + if item[0] == name: + if foreground is None: + foreground = item[1] + if background is 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