From caf355f1428747c2485dd2cc7f55604825d73407 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Mon, 4 Feb 2019 11:56:00 -0500 Subject: [PATCH] Issue #349 Implements support for terminals with light backgrounds via a new `CHEAT_COLORSCHEME` envvar. --- README.md | 7 +++++++ cheat/colorize.py | 5 ++++- cheat/configuration.py | 13 +++++++++++++ config/cheat | 7 ++++--- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4579d5e..8d67e00 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,13 @@ export CHEAT_COLORS=true Note that [pygments][] must be installed on your system for this to work. +`cheat` ships with both light and dark colorschemes to support terminals with +different background colors. A colorscheme may be selected via the +`CHEAT_COLORSCHEME` envvar: + +```sh +export CHEAT_COLORSCHEME=light # must be 'light' (default) or 'dark' +``` #### Specifying a Syntax Highlighter #### You may manually specify which syntax highlighter to use for each cheatsheet by diff --git a/cheat/colorize.py b/cheat/colorize.py index 7b18beb..1d24d7c 100644 --- a/cheat/colorize.py +++ b/cheat/colorize.py @@ -59,4 +59,7 @@ class Colorize: except Exception: pass - return highlight(sheet_content, lexer, TerminalFormatter()) + return highlight( + sheet_content, + lexer, + TerminalFormatter(bg=self._config.cheat_colorscheme)) diff --git a/cheat/configuration.py b/cheat/configuration.py index 4358a2f..bd97297 100644 --- a/cheat/configuration.py +++ b/cheat/configuration.py @@ -41,6 +41,13 @@ class Configuration: True, ]) + # self.cheat_colorscheme + self.cheat_colorscheme = self._select([ + os.environ.get('CHEAT_COLORSCHEME'), + config.get('CHEAT_COLORSCHEME'), + 'light', + ]).strip().lower() + # self.cheat_user_dir self.cheat_user_dir = self._select([ os.environ.get('CHEAT_USER_DIR'), @@ -104,4 +111,10 @@ class Configuration: Utils.die("%s %s" % ('CHEAT_HIGHLIGHT must be one of:', highlights)) + # assert that the color scheme is valid + colorschemes = ['light', 'dark'] + if self.cheat_colorscheme not in colorschemes: + Utils.die("%s %s" % + ('CHEAT_COLORSCHEME must be one of:', colorschemes)) + return True diff --git a/config/cheat b/config/cheat index bb18291..fcbcbbd 100644 --- a/config/cheat +++ b/config/cheat @@ -1,5 +1,6 @@ { - "CHEAT_COLORS" : true, - "CHEAT_EDITOR" : "vi", - "CHEAT_PATH" : "/usr/share/cheat" + "CHEAT_COLORS" : true, + "CHEAT_COLORSCHEME" : "light", + "CHEAT_EDITOR" : "vi", + "CHEAT_PATH" : "/usr/share/cheat" }