Specifiy range of colors to use for color users and make draw colors in chat pane

This commit is contained in:
Paul Nameless 2020-07-01 14:43:26 +08:00
parent e0e4a4f46d
commit ebb251cb4d
5 changed files with 16 additions and 13 deletions

View file

@ -143,11 +143,14 @@ MSG_FLAGS = {
# use this app to open url when there are multiple
URL_VIEW = 'urlview'
# Use 256, 16, 8 or 1
# - 1: means there is always will be returned only 1 color
# - 8, 16, 256: range of colors to use
# Specifies range of colors to use for drawing different users
# - 1, 8, 16, 256: range of colors to use
# 233 is recommended and default, because it excludes gray
USERS_COLORS = 233
USERS_COLORS = tuple(range(233))
# to make one color for all users
# USERS_COLORS = (4,)
```
### Mailcap file

View file

@ -17,8 +17,6 @@ yellow = curses.COLOR_YELLOW
default = -1
user_colors = list(c for c in range(0, 16) if c not in (black, white, 8))
# modes
normal = curses.A_NORMAL
bold = curses.A_BOLD

View file

@ -67,7 +67,7 @@ ICON_PATH = os.path.join(os.path.dirname(__file__), "resources", "tg.png")
URL_VIEW = "urlview"
USERS_COLORS = 234
USERS_COLORS = tuple(range(233))
if os.path.isfile(CONFIG_FILE):
config_params = runpy.run_path(CONFIG_FILE)

View file

@ -258,8 +258,7 @@ def pretty_ts(ts: int) -> str:
@lru_cache(maxsize=256)
def get_color_by_user(user: str) -> int:
if config.USERS_COLORS <= 1:
return colors.blue
return (
int(hashlib.sha1(user.encode()).hexdigest(), 16) % config.USERS_COLORS
index = int(hashlib.sha1(user.encode()).hexdigest(), 16) % len(
config.USERS_COLORS
)
return config.USERS_COLORS[index]

View file

@ -161,9 +161,12 @@ class ChatView:
return color | reverse
return color
def _chat_attributes(self, is_selected: bool = False) -> Tuple[int, ...]:
def _chat_attributes(
self, is_selected: bool, title: str
) -> Tuple[int, ...]:
attrs = (
get_color(cyan, -1),
get_color(get_color_by_user(title), -1),
get_color(blue, -1),
self._msg_color(is_selected),
)
@ -189,7 +192,7 @@ class ChatView:
last_msg = get_last_msg(chat)
offset = 0
for attr, elem in zip(
self._chat_attributes(is_selected), [f"{date} ", title]
self._chat_attributes(is_selected, title), [f"{date} ", title]
):
self.win.addstr(
i,