Draw users with different colors

This commit is contained in:
Paul Nameless 2020-07-01 10:31:51 +08:00
parent 3ceb816961
commit 06db6981b5
3 changed files with 21 additions and 5 deletions

View file

@ -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

View file

@ -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]

View file

@ -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),
)