From 06db6981b51cf26fd6f61180415843b8b8480e53 Mon Sep 17 00:00:00 2001 From: Paul Nameless Date: Wed, 1 Jul 2020 10:31:51 +0800 Subject: [PATCH 1/6] Draw users with different colors --- tg/colors.py | 3 +++ tg/utils.py | 12 +++++++++++- tg/views.py | 11 +++++++---- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/tg/colors.py b/tg/colors.py index 2adb508..695dd04 100644 --- a/tg/colors.py +++ b/tg/colors.py @@ -16,6 +16,9 @@ white = curses.COLOR_WHITE yellow = curses.COLOR_YELLOW default = -1 + +user_colors = list(c for c in range(0, 16) if c not in (black, white)) + # modes normal = curses.A_NORMAL bold = curses.A_BOLD diff --git a/tg/utils.py b/tg/utils.py index eecc3ff..b54d7af 100644 --- a/tg/utils.py +++ b/tg/utils.py @@ -1,5 +1,6 @@ import base64 import curses +import hashlib import logging import mailcap import math @@ -12,11 +13,12 @@ import struct import subprocess import sys from datetime import datetime +from functools import lru_cache from logging.handlers import RotatingFileHandler from types import TracebackType from typing import Any, Optional, Tuple, Type -from tg import config +from tg import colors, config log = logging.getLogger(__name__) emoji_pattern = re.compile( @@ -252,3 +254,11 @@ def pretty_ts(ts: int) -> str: if day_diff < 365: return f"{int(day_diff / 30)} months ago" return f"{int(day_diff / 365)} years ago" + + +@lru_cache(maxsize=128) +def get_color_by_user(user: str) -> int: + index = int(hashlib.sha1(user.encode()).hexdigest(), 16) % len( + colors.user_colors + ) + return colors.user_colors[index] diff --git a/tg/views.py b/tg/views.py index 161468c..1b16fd8 100644 --- a/tg/views.py +++ b/tg/views.py @@ -18,7 +18,7 @@ from tg.colors import ( from tg.models import Model from tg.msg import MsgProxy from tg.tdlib import ChatType -from tg.utils import emoji_pattern, num, truncate_to_len +from tg.utils import emoji_pattern, get_color_by_user, num, truncate_to_len log = logging.getLogger(__name__) @@ -452,7 +452,10 @@ class MsgView: for elements, selected, line_num in msgs_to_draw: column = 0 - for attr, elem in zip(self._msg_attributes(selected), elements): + user = elements[1] + for attr, elem in zip( + self._msg_attributes(selected, user), elements + ): if not elem: continue lines = (column + len(elem)) // self.w @@ -518,10 +521,10 @@ class MsgView: return f"{chat['title']}: {status}".center(self.w)[: self.w] - def _msg_attributes(self, is_selected: bool) -> Tuple[int, ...]: + def _msg_attributes(self, is_selected: bool, user: str) -> Tuple[int, ...]: attrs = ( get_color(cyan, -1), - get_color(blue, -1), + get_color(get_color_by_user(user), -1), get_color(yellow, -1), get_color(white, -1), ) From ac972ef6c31d8bb8488f0ccbbfa8cd8f444a9218 Mon Sep 17 00:00:00 2001 From: Paul Nameless Date: Wed, 1 Jul 2020 10:36:34 +0800 Subject: [PATCH 2/6] Do not use too bright color --- tg/colors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tg/colors.py b/tg/colors.py index 695dd04..73bf21a 100644 --- a/tg/colors.py +++ b/tg/colors.py @@ -17,7 +17,7 @@ yellow = curses.COLOR_YELLOW default = -1 -user_colors = list(c for c in range(0, 16) if c not in (black, white)) +user_colors = list(c for c in range(0, 16) if c not in (black, white, 8)) # modes normal = curses.A_NORMAL From e0e4a4f46d2e68fef8b4107954afa5d0cc877dc1 Mon Sep 17 00:00:00 2001 From: Paul Nameless Date: Wed, 1 Jul 2020 11:33:11 +0800 Subject: [PATCH 3/6] Update readme and configure color range to use --- README.md | 6 ++++++ tg/config.py | 2 ++ tg/utils.py | 9 +++++---- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2766674..e646187 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,12 @@ 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 +# 233 is recommended and default, because it excludes gray +USERS_COLORS = 233 ``` ### Mailcap file diff --git a/tg/config.py b/tg/config.py index b318618..3faef0b 100644 --- a/tg/config.py +++ b/tg/config.py @@ -67,6 +67,8 @@ ICON_PATH = os.path.join(os.path.dirname(__file__), "resources", "tg.png") URL_VIEW = "urlview" +USERS_COLORS = 234 + if os.path.isfile(CONFIG_FILE): config_params = runpy.run_path(CONFIG_FILE) for param, value in config_params.items(): diff --git a/tg/utils.py b/tg/utils.py index b54d7af..33b9a84 100644 --- a/tg/utils.py +++ b/tg/utils.py @@ -256,9 +256,10 @@ def pretty_ts(ts: int) -> str: return f"{int(day_diff / 365)} years ago" -@lru_cache(maxsize=128) +@lru_cache(maxsize=256) def get_color_by_user(user: str) -> int: - index = int(hashlib.sha1(user.encode()).hexdigest(), 16) % len( - colors.user_colors + if config.USERS_COLORS <= 1: + return colors.blue + return ( + int(hashlib.sha1(user.encode()).hexdigest(), 16) % config.USERS_COLORS ) - return colors.user_colors[index] From ebb251cb4d41a10855312c8364e84af1b222634c Mon Sep 17 00:00:00 2001 From: Paul Nameless Date: Wed, 1 Jul 2020 14:43:26 +0800 Subject: [PATCH 4/6] Specifiy range of colors to use for color users and make draw colors in chat pane --- README.md | 11 +++++++---- tg/colors.py | 2 -- tg/config.py | 2 +- tg/utils.py | 7 +++---- tg/views.py | 7 +++++-- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e646187..691cccf 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/tg/colors.py b/tg/colors.py index 73bf21a..623e8c1 100644 --- a/tg/colors.py +++ b/tg/colors.py @@ -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 diff --git a/tg/config.py b/tg/config.py index 3faef0b..89f86f2 100644 --- a/tg/config.py +++ b/tg/config.py @@ -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) diff --git a/tg/utils.py b/tg/utils.py index 33b9a84..5bb262e 100644 --- a/tg/utils.py +++ b/tg/utils.py @@ -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] diff --git a/tg/views.py b/tg/views.py index 1b16fd8..1afedfc 100644 --- a/tg/views.py +++ b/tg/views.py @@ -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, From 10b27783090cb0fa36a22ee63e066006b4d85689 Mon Sep 17 00:00:00 2001 From: Paul Nameless Date: Wed, 1 Jul 2020 19:36:15 +0800 Subject: [PATCH 5/6] Use 16 colors by default to color users --- README.md | 12 ++++++++---- tg/config.py | 3 ++- tg/utils.py | 2 +- tg/views.py | 6 +++--- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 691cccf..4c1c0ed 100644 --- a/README.md +++ b/README.md @@ -143,10 +143,14 @@ MSG_FLAGS = { # use this app to open url when there are multiple URL_VIEW = 'urlview' -# 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 = tuple(range(233)) +# Specifies range of colors to use for drawing users with +# different colors +# this one uses base 16 colors which should look good by default +USERS_COLORS = tuple(range(2, 16)) + +# to use 256 colors, set range appropriately +# though 233 looks better, because last colors are black and gray +# USERS_COLORS = tuple(range(233)) # to make one color for all users # USERS_COLORS = (4,) diff --git a/tg/config.py b/tg/config.py index 89f86f2..9a10e75 100644 --- a/tg/config.py +++ b/tg/config.py @@ -67,7 +67,8 @@ ICON_PATH = os.path.join(os.path.dirname(__file__), "resources", "tg.png") URL_VIEW = "urlview" -USERS_COLORS = tuple(range(233)) +USERS_COLORS = tuple(range(2, 16)) + if os.path.isfile(CONFIG_FILE): config_params = runpy.run_path(CONFIG_FILE) diff --git a/tg/utils.py b/tg/utils.py index 5bb262e..faeb69b 100644 --- a/tg/utils.py +++ b/tg/utils.py @@ -257,7 +257,7 @@ def pretty_ts(ts: int) -> str: @lru_cache(maxsize=256) -def get_color_by_user(user: str) -> int: +def get_color_by_str(user: str) -> int: index = int(hashlib.sha1(user.encode()).hexdigest(), 16) % len( config.USERS_COLORS ) diff --git a/tg/views.py b/tg/views.py index 1afedfc..e73874a 100644 --- a/tg/views.py +++ b/tg/views.py @@ -18,7 +18,7 @@ from tg.colors import ( from tg.models import Model from tg.msg import MsgProxy from tg.tdlib import ChatType -from tg.utils import emoji_pattern, get_color_by_user, num, truncate_to_len +from tg.utils import emoji_pattern, get_color_by_str, num, truncate_to_len log = logging.getLogger(__name__) @@ -166,7 +166,7 @@ class ChatView: ) -> Tuple[int, ...]: attrs = ( get_color(cyan, -1), - get_color(get_color_by_user(title), -1), + get_color(get_color_by_str(title), -1), get_color(blue, -1), self._msg_color(is_selected), ) @@ -527,7 +527,7 @@ class MsgView: def _msg_attributes(self, is_selected: bool, user: str) -> Tuple[int, ...]: attrs = ( get_color(cyan, -1), - get_color(get_color_by_user(user), -1), + get_color(get_color_by_str(user), -1), get_color(yellow, -1), get_color(white, -1), ) From 7f1bdab07ec77a785ff75990e6db31226baa1d62 Mon Sep 17 00:00:00 2001 From: Paul Nameless Date: Wed, 1 Jul 2020 19:40:14 +0800 Subject: [PATCH 6/6] Remove added accidentally line --- tg/colors.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tg/colors.py b/tg/colors.py index 623e8c1..2adb508 100644 --- a/tg/colors.py +++ b/tg/colors.py @@ -16,7 +16,6 @@ white = curses.COLOR_WHITE yellow = curses.COLOR_YELLOW default = -1 - # modes normal = curses.A_NORMAL bold = curses.A_BOLD