From 36e788f25cf05b7dc284d400996d246f44e203de Mon Sep 17 00:00:00 2001 From: Nicolargo Date: Mon, 9 May 2016 17:48:58 +0200 Subject: [PATCH] Allow theme to be set in configuration file #862 --- NEWS | 3 ++- conf/glances.conf | 8 ++++++++ glances/client.py | 2 +- glances/outputs/glances_curses.py | 28 +++++++++++++++++++++++----- glances/standalone.py | 2 +- 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index c40e6972..e038854f 100644 --- a/NEWS +++ b/NEWS @@ -11,7 +11,8 @@ Enhancements and new features: * Improve IP plugin to display public IP address (issue #646) * CPU additionnal stats monitoring: Context switch, Interrupts... (issue #810) * [Folders] Differentiate permission issue and non-existence of a directory (issue #828) - * [Web UI] add cpu name in quicklook plugin (issue #825) + * [Web UI] Add cpu name in quicklook plugin (issue #825) + * Allow theme to be set in configuration file (issue #862) Bugs corrected: diff --git a/conf/glances.conf b/conf/glances.conf index d0b2893d..93aca573 100644 --- a/conf/glances.conf +++ b/conf/glances.conf @@ -1,3 +1,11 @@ +############################################################################## +# User interface +############################################################################## + +[outputs] +# Theme name for the Curses interface: black or white +curse_theme=black + ############################################################################## # plugins ############################################################################## diff --git a/glances/client.py b/glances/client.py index 7525af59..ac375f66 100644 --- a/glances/client.py +++ b/glances/client.py @@ -155,7 +155,7 @@ class GlancesClient(object): self.stats.load_limits(self.config) # Init screen - self.screen = GlancesCursesClient(args=self.args) + self.screen = GlancesCursesClient(config=self.config, args=self.args) # Return result return ret diff --git a/glances/outputs/glances_curses.py b/glances/outputs/glances_curses.py index b35ae1ef..0e3ac81a 100644 --- a/glances/outputs/glances_curses.py +++ b/glances/outputs/glances_curses.py @@ -51,8 +51,9 @@ class _GlancesCurses(object): Note: It is a private class, use GlancesCursesClient or GlancesCursesBrowser. """ - def __init__(self, args=None): - # Init args + def __init__(self, config=None, args=None): + # Init + self.config = config self.args = args # Init windows positions @@ -69,6 +70,11 @@ class _GlancesCurses(object): logger.critical("Cannot init the curses library.\n") sys.exit(1) + # Load the 'outputs' section of the configuration file + # - Init the theme (default is black) + self.theme = {'name': 'black'} + self.load_config(self.config) + # Init cursor self._init_cursor() @@ -95,6 +101,18 @@ class _GlancesCurses(object): # History tag self._init_history() + def load_config(self, config): + '''Load the outputs section of the configuration file''' + # Load the theme + if config.has_section('outputs'): + logger.debug('Read the outputs section in the configuration file') + self.theme['name'] = config.get_value('outputs', 'curse_theme', default='black') + logger.debug('Theme for the curse interface: {0}'.format(self.theme['name'])) + + def is_theme(self, name): + '''Return True if the theme *name* should be used''' + return getattr(self.args, 'theme_' + name) or self.theme['name'] == name + def _init_history(self): '''Init the history option''' @@ -141,7 +159,7 @@ class _GlancesCurses(object): if curses.has_colors(): # The screen is compatible with a colored design - if self.args.theme_white: + if self.is_theme('white'): # White theme: black ==> white curses.init_pair(1, curses.COLOR_BLACK, -1) else: @@ -165,14 +183,14 @@ class _GlancesCurses(object): try: curses.init_pair(9, curses.COLOR_MAGENTA, -1) except Exception: - if self.args.theme_white: + if self.is_theme('white'): curses.init_pair(9, curses.COLOR_BLACK, -1) else: curses.init_pair(9, curses.COLOR_WHITE, -1) try: curses.init_pair(10, curses.COLOR_CYAN, -1) except Exception: - if self.args.theme_white: + if self.is_theme('white'): curses.init_pair(10, curses.COLOR_BLACK, -1) else: curses.init_pair(10, curses.COLOR_WHITE, -1) diff --git a/glances/standalone.py b/glances/standalone.py index 5fc11e46..f0cd4e27 100644 --- a/glances/standalone.py +++ b/glances/standalone.py @@ -75,7 +75,7 @@ class GlancesStandalone(object): glances_processes.max_processes = 50 # Init screen - self.screen = GlancesCursesStandalone(args=args) + self.screen = GlancesCursesStandalone(config=config, args=args) @property def quiet(self): -- GitLab