mirror of
https://github.com/paul-nameless/tg
synced 2024-11-25 13:20:25 +00:00
Merge pull request #101 from paul-nameless/92
Draw users with different colors
This commit is contained in:
commit
4e68561a95
4 changed files with 39 additions and 7 deletions
13
README.md
13
README.md
|
@ -142,6 +142,19 @@ MSG_FLAGS = {
|
||||||
|
|
||||||
# use this app to open url when there are multiple
|
# use this app to open url when there are multiple
|
||||||
URL_VIEW = 'urlview'
|
URL_VIEW = 'urlview'
|
||||||
|
|
||||||
|
# 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,)
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Mailcap file
|
### Mailcap file
|
||||||
|
|
|
@ -67,6 +67,9 @@ ICON_PATH = os.path.join(os.path.dirname(__file__), "resources", "tg.png")
|
||||||
|
|
||||||
URL_VIEW = "urlview"
|
URL_VIEW = "urlview"
|
||||||
|
|
||||||
|
USERS_COLORS = tuple(range(2, 16))
|
||||||
|
|
||||||
|
|
||||||
if os.path.isfile(CONFIG_FILE):
|
if os.path.isfile(CONFIG_FILE):
|
||||||
config_params = runpy.run_path(CONFIG_FILE)
|
config_params = runpy.run_path(CONFIG_FILE)
|
||||||
for param, value in config_params.items():
|
for param, value in config_params.items():
|
||||||
|
|
12
tg/utils.py
12
tg/utils.py
|
@ -1,5 +1,6 @@
|
||||||
import base64
|
import base64
|
||||||
import curses
|
import curses
|
||||||
|
import hashlib
|
||||||
import logging
|
import logging
|
||||||
import mailcap
|
import mailcap
|
||||||
import math
|
import math
|
||||||
|
@ -12,11 +13,12 @@ import struct
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from functools import lru_cache
|
||||||
from logging.handlers import RotatingFileHandler
|
from logging.handlers import RotatingFileHandler
|
||||||
from types import TracebackType
|
from types import TracebackType
|
||||||
from typing import Any, Optional, Tuple, Type
|
from typing import Any, Optional, Tuple, Type
|
||||||
|
|
||||||
from tg import config
|
from tg import colors, config
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
emoji_pattern = re.compile(
|
emoji_pattern = re.compile(
|
||||||
|
@ -252,3 +254,11 @@ def pretty_ts(ts: int) -> str:
|
||||||
if day_diff < 365:
|
if day_diff < 365:
|
||||||
return f"{int(day_diff / 30)} months ago"
|
return f"{int(day_diff / 30)} months ago"
|
||||||
return f"{int(day_diff / 365)} years ago"
|
return f"{int(day_diff / 365)} years ago"
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=256)
|
||||||
|
def get_color_by_str(user: str) -> int:
|
||||||
|
index = int(hashlib.sha1(user.encode()).hexdigest(), 16) % len(
|
||||||
|
config.USERS_COLORS
|
||||||
|
)
|
||||||
|
return config.USERS_COLORS[index]
|
||||||
|
|
18
tg/views.py
18
tg/views.py
|
@ -18,7 +18,7 @@ from tg.colors import (
|
||||||
from tg.models import Model
|
from tg.models import Model
|
||||||
from tg.msg import MsgProxy
|
from tg.msg import MsgProxy
|
||||||
from tg.tdlib import ChatType
|
from tg.tdlib import ChatType
|
||||||
from tg.utils import emoji_pattern, num, truncate_to_len
|
from tg.utils import emoji_pattern, get_color_by_str, num, truncate_to_len
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -161,9 +161,12 @@ class ChatView:
|
||||||
return color | reverse
|
return color | reverse
|
||||||
return color
|
return color
|
||||||
|
|
||||||
def _chat_attributes(self, is_selected: bool = False) -> Tuple[int, ...]:
|
def _chat_attributes(
|
||||||
|
self, is_selected: bool, title: str
|
||||||
|
) -> Tuple[int, ...]:
|
||||||
attrs = (
|
attrs = (
|
||||||
get_color(cyan, -1),
|
get_color(cyan, -1),
|
||||||
|
get_color(get_color_by_str(title), -1),
|
||||||
get_color(blue, -1),
|
get_color(blue, -1),
|
||||||
self._msg_color(is_selected),
|
self._msg_color(is_selected),
|
||||||
)
|
)
|
||||||
|
@ -189,7 +192,7 @@ class ChatView:
|
||||||
last_msg = get_last_msg(chat)
|
last_msg = get_last_msg(chat)
|
||||||
offset = 0
|
offset = 0
|
||||||
for attr, elem in zip(
|
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(
|
self.win.addstr(
|
||||||
i,
|
i,
|
||||||
|
@ -452,7 +455,10 @@ class MsgView:
|
||||||
|
|
||||||
for elements, selected, line_num in msgs_to_draw:
|
for elements, selected, line_num in msgs_to_draw:
|
||||||
column = 0
|
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:
|
if not elem:
|
||||||
continue
|
continue
|
||||||
lines = (column + len(elem)) // self.w
|
lines = (column + len(elem)) // self.w
|
||||||
|
@ -518,10 +524,10 @@ class MsgView:
|
||||||
|
|
||||||
return f"{chat['title']}: {status}".center(self.w)[: self.w]
|
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 = (
|
attrs = (
|
||||||
get_color(cyan, -1),
|
get_color(cyan, -1),
|
||||||
get_color(blue, -1),
|
get_color(get_color_by_str(user), -1),
|
||||||
get_color(yellow, -1),
|
get_color(yellow, -1),
|
||||||
get_color(white, -1),
|
get_color(white, -1),
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue