def init(args): """Initialise the UI, including logging""" if args["--vverbose"]: level = "DEBUG" elif args["--verbose"]: level = "INFO" else: level = None global QUIET global VERBOSE QUIET = args["--quiet"] VERBOSE = args["--verbose"] or args["--vverbose"] root_logger = logging.getLogger("hark_lang") if not args["--no-colours"]: import coloredlogs cf.use_true_colors() cf.use_palette(UI_COLORS) cf.update_palette(UI_COLORS) if level: coloredlogs.install( fmt="[%(asctime)s.%(msecs)03d] %(name)-25s %(message)s", datefmt="%H:%M:%S", level=level, logger=root_logger, ) else: cf.disable() # FIXME Logger doesn't have basicConfig if level: root_logger.basicConfig(level=level)
def colored(text, color=None, on_color=None, attrs=None, style=None, palette=None): """ Colorize text. :param text: text to be colorized :param color: text color :param on_color: background color :param attrs: single styling attribute or list of styling attributes :param style: colorful styling function, e.g. red_on_green (for green foreground and red background colors) :param palette: predefined palette's name (e.g. 'monokai') :return: styled string Available styling attributes: blinkrapid, blinkslow, bold, concealed, dimmed, inversed, italic, reset, struckthrough, underlined Available palettes: monokai, solarized """ if isinstance(style, (list, tuple)): style = "_".join(style) s = style or "" if palette: colorful.use_style(palette) if s == "": if attrs: if not isinstance(attrs, list): attrs = [attrs] for attr in attrs: if attr not in colorful.ansi.MODIFIERS.keys(): raise ValueError("Bad ANSI modifier '%s'" % attr) s += str(attr) + "_" if color: if color not in colorful.colorpalette.keys(): raise ValueError("Bad color '%s'" % color) s += str(color) + "_" if on_color: if on_color not in colorful.colorpalette.keys(): raise ValueError("Bad color '%s'" % on_color) s += "on_" + str(on_color) if s != "": c = getattr(colorful, s.rstrip("_")) try: return c(text).styled_string if s and TTY else text finally: # ensure that the palette is restored colorful.use_palette(COLOR_PALETTE)
def load_theme(): try: theme_name = os.environ['PURE_THEME'] except KeyError: theme_name = 'tomorrow' finally: theme_path = Path(os.getcwd() + '/pure/theme/' + theme_name + '.json') with open(str(theme_path), 'r') as theme: scheme = json.load(theme) colorful.use_true_colors() colorful.use_palette(scheme) return theme_name, scheme
def printc(content, bg='#000000', fg='#ffffff'): colorful.use_palette({'fg': fg, 'bg': bg}) print(colorful.fg_on_bg(str(content)), file=sys.stderr)