Initial commit (Cython implementation)
This commit is contained in:
commit
6f28ac0382
13 changed files with 1616 additions and 0 deletions
175
src-00_core/src/core.10-text.pyx
Normal file
175
src-00_core/src/core.10-text.pyx
Normal file
|
@ -0,0 +1,175 @@
|
|||
# def textfilter(text, set=None):
|
||||
# """
|
||||
# str text text to filter
|
||||
# dict set {to_replace: replacement}
|
||||
|
||||
# Text filter that replaces occurences of to_replace keys with their respective values.
|
||||
|
||||
# Defaults to filtering of 'bad' characters if no translational dictionary is provided.
|
||||
# """
|
||||
|
||||
# if not set:
|
||||
# set = badchars
|
||||
|
||||
# for to_replace in set.keys():
|
||||
# text = text.replace(to_replace, set[to_replace])
|
||||
|
||||
# return text
|
||||
|
||||
# def cut(text, size, end=0, colors=True):
|
||||
# """
|
||||
# str text text to cut
|
||||
# int size how many chars to return, >=0
|
||||
# int end return chars from 0 = left, 1 = right
|
||||
# bool colors skip color tags
|
||||
# """
|
||||
|
||||
# out = ""
|
||||
|
||||
# strlen = len(text)
|
||||
|
||||
# if end == 0:
|
||||
# ptr = 0 # go from left
|
||||
# else:
|
||||
# ptr = strlen - 1
|
||||
|
||||
# while size:
|
||||
# if end == 0:
|
||||
# if colors and text[ptr] == "<" and strlen - ptr >= 4 and text[ptr+1] == "C" and text[ptr+3] == ">": # we have a tag
|
||||
# out += text[ptr:ptr+4]
|
||||
# ptr += 4
|
||||
# else:
|
||||
# out += text[ptr]
|
||||
# ptr += 1
|
||||
# size -= 1
|
||||
# else:
|
||||
# if colors and text[ptr] == ">" and ptr >= 4 and text[ptr-2] == "C" and text[ptr-3] == "<": # we have a tag
|
||||
# out = text[ptr-3:ptr+1] + out
|
||||
# ptr -= 4
|
||||
# else:
|
||||
# out = text[ptr] + out
|
||||
# ptr -= 1
|
||||
# size -= 1
|
||||
|
||||
# # reached end ?
|
||||
# if (end == 0 and ptr == strlen) or (end == 1 and ptr == -1):
|
||||
# break
|
||||
|
||||
# return out
|
||||
|
||||
# def strtrim(text, length, mode=0, dots=True, colors=True):
|
||||
# """
|
||||
# str text text to trim
|
||||
# int length desired length, >1
|
||||
# int mode -1 = cut chars from the left, 0 = from the middle, 1 = from the right
|
||||
# bool dots add an ellipsis to the point of cutting
|
||||
# bool colors dont count color tags into length; also turns the ellipsis blue
|
||||
# """
|
||||
|
||||
# if len(text) <= length:
|
||||
# return text
|
||||
|
||||
# if length <= 3:
|
||||
# dots = False
|
||||
|
||||
# if dots:
|
||||
# length -= 3
|
||||
|
||||
# if mode == -1:
|
||||
# if dots:
|
||||
# return "..." + cut(text, length, 1, colors)
|
||||
# else:
|
||||
# return cut(text, length, 1, colors)
|
||||
|
||||
# elif mode == 0:
|
||||
# if length%2 == 1:
|
||||
# part1 = cut(text, length/2+1, 0, colors)
|
||||
# else:
|
||||
# part1 = cut(text, length/2, 0, colors)
|
||||
|
||||
# part2 = cut(text, length/2, 1, colors)
|
||||
|
||||
# if dots:
|
||||
# part1 += "..."
|
||||
|
||||
# return part1 + part2
|
||||
|
||||
# else:
|
||||
# if dots:
|
||||
# return cut(text, length, 0, colors) + "..."
|
||||
# else:
|
||||
# return cut(text, length, 0, colors)
|
||||
|
||||
|
||||
def paragraph(text, width=0, indent=0, prefix=None, stamp=None):
|
||||
"""
|
||||
str text text to format
|
||||
int width required line length; if 0, current terminal width will be used;
|
||||
if negative, current terminal width minus supplied amount will be used
|
||||
int indent how many spaces to indent the text with
|
||||
str prefix prefix each line with it; not counted in width (offsets the lines)
|
||||
str stamp placed over the first line's indent (stretching the indent if necessary)
|
||||
|
||||
Formats text into an indented paragraph that fits the terminal and returns it.
|
||||
Correctly handles color tags.
|
||||
"""
|
||||
|
||||
#words = [x.strip() for x in text.split() if x.strip()]
|
||||
words = text.split()
|
||||
|
||||
term_width = get_terminal_size()[1]
|
||||
|
||||
if not width:
|
||||
width = term_width
|
||||
elif width < 0:
|
||||
width += term_width
|
||||
|
||||
lines = [] # individual lines go in this buffer
|
||||
|
||||
first = True
|
||||
while words:
|
||||
if indent:
|
||||
if first and stamp:
|
||||
lines.append([stamp + " "*(indent-1-len(stamp))])
|
||||
else:
|
||||
lines.append([" "*(indent-1)]) # first word = indent minus one space (that's gonna get back while joining)
|
||||
|
||||
first = False
|
||||
else:
|
||||
lines.append([])
|
||||
|
||||
while words:
|
||||
word_added = False
|
||||
|
||||
if len(re.sub("§.", "", ' '.join(lines[-1]))) + len(re.sub("§.", "", words[0])) + 1 <= width:
|
||||
lines[-1].append(words.pop(0))
|
||||
word_added = True
|
||||
|
||||
elif not word_added and len(lines[-1]) == 1 and indent:
|
||||
# no word added and just the indent's in here = word's too long -> screw indent
|
||||
# we might try to keep at least a part of the indent - if possible
|
||||
len_word = len(re.sub("§.", "", words[0]))
|
||||
if len_word < width:
|
||||
lines[-1] = [" "*(width - len_word - 1), words.pop(0)]
|
||||
else:
|
||||
lines[-1] = [words.pop(0)]
|
||||
word_added = True
|
||||
break
|
||||
|
||||
elif not word_added and not lines[-1] and not indent:
|
||||
# no word added, empty line = word's too long -> screw indent
|
||||
lines[-1] = [words.pop(0)]
|
||||
word_added = True
|
||||
break
|
||||
|
||||
else:
|
||||
break
|
||||
|
||||
lines_tmp = []
|
||||
for line in lines:
|
||||
if prefix:
|
||||
line.insert(0, prefix)
|
||||
|
||||
lines_tmp.append(' '.join(line)) # put words together
|
||||
|
||||
return '\n'.join(lines_tmp) # put lines together
|
Loading…
Add table
Add a link
Reference in a new issue