diff options
author | mathieui <mathieui@mathieui.net> | 2019-09-22 17:35:07 +0200 |
---|---|---|
committer | mathieui <mathieui@mathieui.net> | 2020-05-09 19:46:17 +0200 |
commit | 41127e50abc3e126f953af5ad638f92d0848f9f1 (patch) | |
tree | ebc7c7a4557e1d005f2bf5a52b84f460342dca63 /poezio/ui/funcs.py | |
parent | d22b4b8c218cbbaee62002d751bd69bfe1d1deab (diff) | |
download | poezio-41127e50abc3e126f953af5ad638f92d0848f9f1.tar.gz poezio-41127e50abc3e126f953af5ad638f92d0848f9f1.tar.bz2 poezio-41127e50abc3e126f953af5ad638f92d0848f9f1.tar.xz poezio-41127e50abc3e126f953af5ad638f92d0848f9f1.zip |
Move message rendering code to Message.render()
Also:
- rename format_chars to FORMAT_CHARS because it’s static constant
- move Line, Message, and a few funcs/consts to a new poezio.ui module
Diffstat (limited to 'poezio/ui/funcs.py')
-rw-r--r-- | poezio/ui/funcs.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/poezio/ui/funcs.py b/poezio/ui/funcs.py new file mode 100644 index 00000000..260cc037 --- /dev/null +++ b/poezio/ui/funcs.py @@ -0,0 +1,60 @@ +""" +Standalone functions used by the modules +""" + +import string +from typing import Optional, List +from poezio.ui.consts import FORMAT_CHAR, FORMAT_CHARS + +DIGITS = string.digits + '-' + + +def find_first_format_char(text: str, + chars: str = None) -> int: + to_find = chars or FORMAT_CHARS + pos = -1 + for char in to_find: + p = text.find(char) + if p == -1: + continue + if pos == -1 or p < pos: + pos = p + return pos + + +def truncate_nick(nick: Optional[str], size=10) -> Optional[str]: + if size < 1: + size = 1 + if nick and len(nick) > size: + return nick[:size] + '…' + return nick + + +def parse_attrs(text: str, previous: Optional[List[str]] = None) -> List[str]: + next_attr_char = text.find(FORMAT_CHAR) + if previous: + attrs = previous + else: + attrs = [] + while next_attr_char != -1 and text: + if next_attr_char + 1 < len(text): + attr_char = text[next_attr_char + 1].lower() + else: + attr_char = '\0' + if attr_char == 'o': + attrs = [] + elif attr_char == 'u': + attrs.append('u') + elif attr_char == 'b': + attrs.append('b') + elif attr_char == 'i': + attrs.append('i') + if attr_char in DIGITS and attr_char: + color_str = text[next_attr_char + 1:text.find('}', next_attr_char)] + if color_str: + attrs.append(color_str + '}') + text = text[next_attr_char + len(color_str) + 2:] + else: + text = text[next_attr_char + 2:] + next_attr_char = text.find(FORMAT_CHAR) + return attrs |