From 141814bb848e04fb3f1ec64c8e6f10f760e39789 Mon Sep 17 00:00:00 2001 From: Denis Kasak Date: Mon, 12 Nov 2018 22:42:44 +0100 Subject: [PATCH] Support full color pair (fg/bg) for each color. --- matrix/buffer.py | 11 +++++++-- matrix/colors.py | 22 +++++++++++------- matrix/config.py | 60 ++++++++++++++++++++++++++++++++++++++---------- matrix/server.py | 3 ++- matrix/utils.py | 9 ++++++++ 5 files changed, 82 insertions(+), 23 deletions(-) diff --git a/matrix/buffer.py b/matrix/buffer.py index 6c9ac18..cb10db5 100644 --- a/matrix/buffer.py +++ b/matrix/buffer.py @@ -53,7 +53,12 @@ from .colors import Formatted from .config import RedactType from .globals import SCRIPT_NAME, SERVERS, W, TYPING_NOTICE_TIMEOUT from .utf import utf8_decode -from .utils import server_ts_to_weechat, shorten_sender, string_strikethrough +from .utils import ( + server_ts_to_weechat, + shorten_sender, + string_strikethrough, + color_pair, +) OwnMessages = NamedTuple( "OwnMessages", @@ -1446,7 +1451,9 @@ class RoomBuffer(object): "message{del_color}>{ncolor}").format( del_color=W.color("chat_delimiters"), ncolor=W.color("reset"), - error_color=W.color(G.CONFIG.color.error_message)) + error_color=W.color(color_pair( + G.CONFIG.color.error_message_fg, + G.CONFIG.color.error_message_bg))) last_line.message = message diff --git a/matrix/colors.py b/matrix/colors.py index eb6dece..a8a7376 100644 --- a/matrix/colors.py +++ b/matrix/colors.py @@ -35,7 +35,9 @@ from pygments.util import ClassNotFound from . import globals as G from .globals import W -from .utils import string_strikethrough, string_color_and_reset +from .utils import (string_strikethrough, + string_color_and_reset, + color_pair) try: from HTMLParser import HTMLParser @@ -59,10 +61,12 @@ class Formatted(object): @property def textwrapper(self): + quote_pair = color_pair(G.CONFIG.color.quote_fg, + G.CONFIG.color.quote_bg) return textwrap.TextWrapper( width=67, - initial_indent="{}> ".format(W.color(G.CONFIG.color.quote)), - subsequent_indent="{}> ".format(W.color(G.CONFIG.color.quote)), + initial_indent="{}> ".format(W.color(quote_pair)), + subsequent_indent="{}> ".format(W.color(quote_pair)), ) def is_formatted(self): @@ -302,8 +306,10 @@ class Formatted(object): try: lexer = get_lexer_by_name(value) except ClassNotFound: - return string_color_and_reset(string, - G.CONFIG.color.untagged_code) + return string_color_and_reset( + string, + color_pair(G.CONFIG.color.untagged_code_fg, + G.CONFIG.color.untagged_code_bg)) try: style = get_style_by_name(G.CONFIG.look.pygments_style) @@ -361,11 +367,11 @@ class Formatted(object): # If we're quoted code add quotation marks now. if key == "code" and attributes["quote"]: + fg = G.CONFIG.color.quote_fg + bg = G.CONFIG.color.quote_bg text = indent( text, - "{}>{} ".format( - W.color(G.CONFIG.color.quote), W.color("reset") - ), + string_color_and_reset(">", color_pair(fg, bg)) + " ", ) # If we're code don't remove multiple newlines blindly diff --git a/matrix/config.py b/matrix/config.py index c8655f7..98f51c2 100644 --- a/matrix/config.py +++ b/matrix/config.py @@ -642,44 +642,80 @@ class MatrixConfig(WeechatConfig): color_options = [ Option( - "quote", + "quote_fg", "color", "", 0, 0, "lightgreen", - ("Color for matrix style blockquotes"), + "Foreground color for matrix style blockquotes", ), Option( - "error_message", + "quote_bg", + "color", + "", + 0, + 0, + "default", + "Background counterpart of quote_fg", + ), + Option( + "error_message_fg", "color", "", 0, 0, "darkgray", - ("Color for error messages that appear inside a room buffer (" - "e.g. when a message errors out when sending or when a " - "message is redacted)"), + ("Foreground color for error messages that appear inside a " + "room buffer (e.g. when a message errors out when sending or " + "when a message is redacted)"), ), Option( - "unconfirmed_message", + "error_message_bg", + "color", + "", + 0, + 0, + "default", + "Background counterpart of error_message_fg.", + ), + Option( + "unconfirmed_message_fg", "color", "", 0, 0, "darkgray", - ("Color for messages that are printed out but the server " - "hasn't confirmed the that he received them."), + ("Foreground color for messages that are printed out but the " + "server hasn't confirmed the that he received them."), ), Option( - "untagged_code", + "unconfirmed_message_bg", + "color", + "", + 0, + 0, + "default", + "Background counterpart of unconfirmed_message_fg." + ), + Option( + "untagged_code_fg", "color", "", 0, 0, "blue", - ("Color for code without a language specifier. Also used for " - "`inline code`."), + ("Foreground color for code without a language specifier. " + "Also used for `inline code`."), + ), + Option( + "untagged_code_bg", + "color", + "", + 0, + 0, + "default", + "Background counterpart of untagged_code_fg", ), ] diff --git a/matrix/server.py b/matrix/server.py index eef4cab..5546ad1 100644 --- a/matrix/server.py +++ b/matrix/server.py @@ -798,7 +798,8 @@ class MatrixServer(object): plain_message = formatted.to_weechat() plain_message = W.string_remove_color(plain_message, "") attributes = DEFAULT_ATTRIBUTES.copy() - attributes["fgcolor"] = G.CONFIG.color.unconfirmed_message + attributes["fgcolor"] = G.CONFIG.color.unconfirmed_message_fg + attributes["bgcolor"] = G.CONFIG.color.unconfirmed_message_bg new_formatted = Formatted([FormattedString( plain_message, attributes diff --git a/matrix/utils.py b/matrix/utils.py index e711953..d77b6f6 100644 --- a/matrix/utils.py +++ b/matrix/utils.py @@ -123,3 +123,12 @@ def string_color(string, color): lines = ("{}{}{}".format(W.color(color), line, W.color("resetcolor")) for line in lines) return "\n".join(lines) + + +def color_pair(color_fg, color_bg): + """Make a color pair from a pair of colors.""" + + if color_bg: + return "{},{}".format(color_fg, color_bg) + else: + return color_fg