本文整理汇总了Python中prompt_toolkit.formatted_text.FormattedText方法的典型用法代码示例。如果您正苦于以下问题:Python formatted_text.FormattedText方法的具体用法?Python formatted_text.FormattedText怎么用?Python formatted_text.FormattedText使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类prompt_toolkit.formatted_text
的用法示例。
在下文中一共展示了formatted_text.FormattedText方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: print_lexer
# 需要导入模块: from prompt_toolkit import formatted_text [as 别名]
# 或者: from prompt_toolkit.formatted_text import FormattedText [as 别名]
def print_lexer(
body: str, lexer: Lexer, label: str = None, prefix: str = None, indent: int = None
):
if COLORIZE:
prefix_str = prefix + " " if prefix else ""
if prefix_str or indent:
prefix_body = prefix_str + " " * (indent or 0)
lexer.add_filter(PrefixFilter(prefix=prefix_body))
tokens = list(pygments.lex(body, lexer=lexer))
if label:
fmt_label = [("fg:ansimagenta", label)]
if prefix_str:
fmt_label.insert(0, ("", prefix_str))
print_formatted(FormattedText(fmt_label))
print_formatted(PygmentsTokens(tokens))
else:
print_ext(body, label=label, prefix=prefix)
示例2: print_ext
# 需要导入模块: from prompt_toolkit import formatted_text [as 别名]
# 或者: from prompt_toolkit.formatted_text import FormattedText [as 别名]
def print_ext(
*msg,
color: str = None,
label: str = None,
prefix: str = None,
indent: int = None,
**kwargs,
):
prefix_str = prefix or ""
if indent:
prefix_str += " " * indent
if color and COLORIZE:
msg = [(color, " ".join(map(str, msg)))]
if prefix_str:
msg.insert(0, ("", prefix_str + " "))
if label:
msg.insert(0, ("fg:ansimagenta", label + "\n"))
print_formatted(FormattedText(msg), **kwargs)
return
if label:
print(label, **kwargs)
if prefix_str:
msg = (prefix_str, *msg)
print(*msg, **kwargs)
示例3: __init__
# 需要导入模块: from prompt_toolkit import formatted_text [as 别名]
# 或者: from prompt_toolkit.formatted_text import FormattedText [as 别名]
def __init__(self, client_state):
def get_message():
# If there is a message to be shown for this client, show that.
if client_state.message:
return client_state.message
else:
return ''
def get_tokens():
message = get_message()
if message:
return FormattedText([
('class:message', message),
('[SetCursorPosition]', ''),
('class:message', ' '),
])
else:
return ''
@Condition
def is_visible():
return bool(get_message())
super(MessageToolbar, self).__init__(get_tokens)
示例4: main
# 需要导入模块: from prompt_toolkit import formatted_text [as 别名]
# 或者: from prompt_toolkit.formatted_text import FormattedText [as 别名]
def main():
print(HTML("\n<u>True color test.</u>"))
for template in [
"bg:#{0:02x}0000", # Red.
"bg:#00{0:02x}00", # Green.
"bg:#0000{0:02x}", # Blue.
"bg:#{0:02x}{0:02x}00", # Yellow.
"bg:#{0:02x}00{0:02x}", # Magenta.
"bg:#00{0:02x}{0:02x}", # Cyan.
"bg:#{0:02x}{0:02x}{0:02x}", # Gray.
]:
fragments = []
for i in range(0, 256, 4):
fragments.append((template.format(i), " "))
print(FormattedText(fragments), color_depth=ColorDepth.DEPTH_4_BIT)
print(FormattedText(fragments), color_depth=ColorDepth.DEPTH_8_BIT)
print(FormattedText(fragments), color_depth=ColorDepth.DEPTH_24_BIT)
print()
示例5: main
# 需要导入模块: from prompt_toolkit import formatted_text [as 别名]
# 或者: from prompt_toolkit.formatted_text import FormattedText [as 别名]
def main():
style = Style.from_dict({"hello": "#ff0066", "world": "#44ff44 italic",})
# Print using a a list of text fragments.
text_fragments = FormattedText(
[("class:hello", "Hello "), ("class:world", "World"), ("", "\n"),]
)
print(text_fragments, style=style)
# Print using an HTML object.
print(HTML("<hello>hello</hello> <world>world</world>\n"), style=style)
# Print using an HTML object with inline styling.
print(
HTML(
'<style fg="#ff0066">hello</style> '
'<style fg="#44ff44"><i>world</i></style>\n'
)
)
# Print using ANSI escape sequences.
print(ANSI("\x1b[31mhello \x1b[32mworld\n"))
示例6: getMagicPromptLines
# 需要导入模块: from prompt_toolkit import formatted_text [as 别名]
# 或者: from prompt_toolkit.formatted_text import FormattedText [as 别名]
def getMagicPromptLines(self, patch):
'''
Get the text lines from a MagicMock object from withCliPromptMock.
Args:
patch (mock.MagicMock): The MagicMock object from withCliPromptMock.
Returns:
list: A list of lines.
'''
self.true(patch.called, 'Assert prompt was called')
lines = []
for args in patch.call_args_list:
arg = args[0][0]
if isinstance(arg, str):
lines.append(arg)
continue
if isinstance(arg, FormattedText):
color, text = arg[0]
lines.append(text)
continue
raise ValueError(f'Unknown arg: {type(arg)}/{arg}')
return lines
示例7: getMagicPromptColors
# 需要导入模块: from prompt_toolkit import formatted_text [as 别名]
# 或者: from prompt_toolkit.formatted_text import FormattedText [as 别名]
def getMagicPromptColors(self, patch):
'''
Get the colored lines from a MagicMock object from withCliPromptMock.
Args:
patch (mock.MagicMock): The MagicMock object from withCliPromptMock.
Returns:
list: A list of tuples, containing color and line data.
'''
self.true(patch.called, 'Assert prompt was called')
lines = []
for args in patch.call_args_list:
arg = args[0][0]
if isinstance(arg, str):
continue
if isinstance(arg, FormattedText):
color, text = arg[0]
lines.append((color, text))
continue
raise ValueError(f'Unknown arg: {type(arg)}/{arg}')
return lines
示例8: _render_sdl
# 需要导入模块: from prompt_toolkit import formatted_text [as 别名]
# 或者: from prompt_toolkit.formatted_text import FormattedText [as 别名]
def _render_sdl(self, sdl: str) -> None:
desc_doc = pt_document.Document(sdl)
lexer = pt_lexers.PygmentsLexer(eql_pygments.EdgeQLLexer)
formatter = lexer.lex_document(desc_doc)
for line in range(desc_doc.line_count):
pt_shortcuts.print_formatted_text(
pt_formatted_text.FormattedText(formatter(line)),
style=self.style
)
print()
示例9: print_in_colors
# 需要导入模块: from prompt_toolkit import formatted_text [as 别名]
# 或者: from prompt_toolkit.formatted_text import FormattedText [as 别名]
def print_in_colors(out):
style = Style.from_dict({"cli_out": "fg:{}".format(config.cli_info_color)})
print_formatted_text(FormattedText([("class:cli_out", str(out))]), style=style)
示例10: banner
# 需要导入模块: from prompt_toolkit import formatted_text [as 别名]
# 或者: from prompt_toolkit.formatted_text import FormattedText [as 别名]
def banner(animation=True):
banners = [banner1, banner2, banner3]
color = ColorSelected()
text = FormattedText([
(color.theme.banner, choice(banners)),
(color.theme.primary, info)
])
print_formatted_text(text)
if animation:
little_animation()
示例11: main
# 需要导入模块: from prompt_toolkit import formatted_text [as 别名]
# 或者: from prompt_toolkit.formatted_text import FormattedText [as 别名]
def main():
tokens = FormattedText([("fg:" + name, name + " ") for name in NAMED_COLORS])
print(HTML("\n<u>Named colors, using 16 color output.</u>"))
print("(Note that it doesn't really make sense to use named colors ")
print("with only 16 color output.)")
print(tokens, color_depth=ColorDepth.DEPTH_4_BIT)
print(HTML("\n<u>Named colors, use 256 colors.</u>"))
print(tokens)
print(HTML("\n<u>Named colors, using True color output.</u>"))
print(tokens, color_depth=ColorDepth.TRUE_COLOR)
示例12: test_formatted_text_with_style
# 需要导入模块: from prompt_toolkit import formatted_text [as 别名]
# 或者: from prompt_toolkit.formatted_text import FormattedText [as 别名]
def test_formatted_text_with_style():
f = _Capture()
style = Style.from_dict({"hello": "#ff0066", "world": "#44ff44 italic",})
tokens = FormattedText([("class:hello", "Hello "), ("class:world", "world"),])
# NOTE: We pass the default (8bit) color depth, so that the unit tests
# don't start failing when environment variables change.
pt_print(tokens, style=style, file=f, color_depth=ColorDepth.DEFAULT)
assert b"\x1b[0;38;5;197mHello" in f.data
assert b"\x1b[0;38;5;83;3mworld" in f.data
示例13: _internal_prompt_print
# 需要导入模块: from prompt_toolkit import formatted_text [as 别名]
# 或者: from prompt_toolkit.formatted_text import FormattedText [as 别名]
def _internal_prompt_print(self, *args, **kwargs):
kwargs['sep'] = kwargs.pop('sep', ' ')
kwargs['end'] = kwargs.pop('end', '\n')
kwargs['file'] = kwargs.pop('file', sys.stdout)
kwargs['style'] = token_style
frags = []
for a in args:
if isinstance(a, FormattedText):
frags.append(a)
else:
frags.append(FormattedText([("class:command", str(a))]))
print_formatted_text(*frags, **kwargs)
示例14: printf
# 需要导入模块: from prompt_toolkit import formatted_text [as 别名]
# 或者: from prompt_toolkit.formatted_text import FormattedText [as 别名]
def printf(self, mesg, addnl=True, color=None):
if not self.colorsenabled:
return self.outp.printf(mesg, addnl=addnl)
# print_formatted_text can't handle \r
mesg = mesg.replace('\r', '')
if color is not None:
mesg = FormattedText([(color, mesg)])
return print_formatted_text(mesg, end='\n' if addnl else '')
示例15: color_formatted_text
# 需要导入模块: from prompt_toolkit import formatted_text [as 别名]
# 或者: from prompt_toolkit.formatted_text import FormattedText [as 别名]
def color_formatted_text(data, msg_type):
if msg_type in constants.STYLE:
return FormattedText([('class:{}'.format(msg_type), data)])
else:
return FormattedText([('', data)])