Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# type: (int) -> Iterator[FmtStr]
splitter = self.chunks[0].splitter()
chunks_of_line = []
width_of_line = 0
for source_chunk in self.chunks:
splitter.reinit(source_chunk)
while True:
request = splitter.request(columns - width_of_line)
if request is None:
break # done with this source_chunk
w, new_chunk = request
chunks_of_line.append(new_chunk)
width_of_line += w
if width_of_line == columns:
yield FmtStr(*chunks_of_line)
del chunks_of_line[:]
width_of_line = 0
if chunks_of_line:
yield FmtStr(*chunks_of_line)
def __add__(self, other):
if isinstance(other, FmtStr):
return FmtStr(*(self.chunks + other.chunks))
elif isinstance(other, (bytes, unicode)):
return FmtStr(*(self.chunks + [Chunk(other)]))
else:
raise TypeError('Can\'t add %r and %r' % (self, other))
>>> parse(u'\x01y\x03print\x04')
yellow('print')
>>> parse(u'\x01y\x03print\x04\x01c\x03 \x04\x01g\x031\x04\x01c\x03 \x04\x01Y\x03+\x04\x01c\x03 \x04\x01g\x032\x04')
yellow('print')+cyan(' ')+green('1')+cyan(' ')+bold(yellow('+'))+cyan(' ')+green('2')
"""
rest = s
stuff = []
while True:
if not rest:
break
d, rest = peel_off_string(rest)
stuff.append(d)
return (sum([fs_from_match(d) for d in stuff[1:]], fs_from_match(stuff[0]))
if len(stuff) > 0
else FmtStr())
tail = Chunk(bfs.s[end - bfs_start:], atts=bfs.atts)
new_components.append(tail)
elif bfs_start < end < bfs_end:
divide = start - bfs_start
tail = Chunk(bfs.s[end - bfs_start:], atts=bfs.atts)
new_components.append(tail)
elif bfs_start >= end or bfs_end <= start:
new_components.append(bfs)
if not inserted:
new_components.extend(new_fs.chunks)
inserted = True
return FmtStr(*[s for s in new_components if s.s])
def fmtstr(string, *args, **kwargs):
# type: (Union[Text, bytes, FmtStr], *Any, **Any) -> FmtStr
"""
Convenience function for creating a FmtStr
>>> fmtstr('asdf', 'blue', 'on_red', 'bold')
on_red(bold(blue('asdf')))
>>> fmtstr('blarg', fg='blue', bg='red', bold=True)
on_red(bold(blue('blarg')))
"""
atts = parse_args(args, kwargs)
if isinstance(string, FmtStr):
pass
elif isinstance(string, (bytes, unicode)):
string = FmtStr.from_str(string)
else:
raise ValueError("Bad Args: %r (of type %s), %r, %r" % (string, type(string), args, kwargs))
return string.copy_with_new_atts(**atts)
def copy_with_new_str(self, new_str):
"""Copies the current FmtStr's attributes while changing its string."""
# What to do when there are multiple Chunks with conflicting atts?
old_atts = dict((att, value) for bfs in self.chunks
for (att, value) in bfs.atts.items())
return FmtStr(Chunk(new_str, old_atts))
except ValueError:
return FmtStr(Chunk(remove_ansi(s)))
else:
chunks = []
cur_fmt = {}
for x in tokens_and_strings:
if isinstance(x, dict):
cur_fmt.update(x)
elif isinstance(x, (bytes, unicode)):
atts = parse_args('', dict((k, v)
for k, v in cur_fmt.items()
if v is not None))
chunks.append(Chunk(x, atts=atts))
else:
raise Exception("logic error")
return FmtStr(*chunks)
else:
return FmtStr(Chunk(s))
index = normalize_slice(self.width, index)
counter = 0
parts = []
for chunk in self.chunks:
if index.start < counter + chunk.width and index.stop > counter:
start = max(0, index.start - counter)
end = min(index.stop - counter, chunk.width)
if end - start == chunk.width:
parts.append(chunk)
else:
s_part = width_aware_slice(chunk.s, max(0, index.start - counter), index.stop - counter)
parts.append(Chunk(s_part, chunk.atts))
counter += chunk.width
if index.stop < counter:
break
return FmtStr(*parts) if parts else fmtstr('')