def create_output( stdout: Optional[TextIO] = None, always_prefer_tty: bool = True ) -> Output: """ Return an :class:`~prompt_toolkit.output.Output` instance for the command line. :param stdout: The stdout object :param always_prefer_tty: When set, look for `sys.stderr` if `sys.stdout` is not a TTY. (The prompt_toolkit render output is not meant to be consumed by something other then a terminal, so this is a reasonable default.) """ # Consider TERM and PROMPT_TOOLKIT_COLOR_DEPTH environment variables. # Notice that PROMPT_TOOLKIT_COLOR_DEPTH value is the default that's used # if the Application doesn't override it. term_from_env = get_term_environment_variable() color_depth_from_env = ColorDepth.from_env() if stdout is None: # By default, render to stdout. If the output is piped somewhere else, # render to stderr. stdout = sys.stdout if always_prefer_tty: for io in [sys.stdout, sys.stderr]: if io.isatty(): stdout = io break # If the patch_stdout context manager has been used, then sys.stdout is # replaced by this proxy. For prompt_toolkit applications, we want to use # the real stdout. from prompt_toolkit.patch_stdout import StdoutProxy while isinstance(stdout, StdoutProxy): stdout = stdout.original_stdout if is_windows(): from .conemu import ConEmuOutput from .win32 import Win32Output from .windows10 import Windows10_Output, is_win_vt100_enabled if is_win_vt100_enabled(): return cast( Output, Windows10_Output(stdout, default_color_depth=color_depth_from_env), ) if is_conemu_ansi(): return cast( Output, ConEmuOutput(stdout, default_color_depth=color_depth_from_env) ) else: return Win32Output(stdout, default_color_depth=color_depth_from_env) else: from .vt100 import Vt100_Output return Vt100_Output.from_pty( stdout, term=term_from_env, default_color_depth=color_depth_from_env )
def create_output(stdout=None, true_color=False): """ Return an :class:`~prompt_toolkit.output.Output` instance for the command line. :param true_color: When True, use 24bit colors instead of 256 colors. (`bool` or :class:`~prompt_toolkit.filters.SimpleFilter`.) Notes ----- This method was forked from the mainline prompt-toolkit repo. Copyright (c) 2014, Jonathan Slenders, All rights reserved. This is deprecated and slated for removal after a prompt-toolkit v0.57+ release. """ stdout = stdout or sys.__stdout__ true_color = to_simple_filter(true_color) if is_windows(): if is_conemu_ansi(): return ConEmuOutput(stdout) else: return Win32Output(stdout) else: term = os.environ.get('TERM', '') if PY2: term = term.decode('utf-8') return Vt100_Output.from_pty(stdout, true_color=true_color) #, term=term)
def create_output(stdout=None, true_color=False, ansi_colors_only=None): """ Return an :class:`~prompt_toolkit.output.Output` instance for the command line. :param true_color: When True, use 24bit colors instead of 256 colors. (`bool` or :class:`~prompt_toolkit.filters.Filter`.) :param ansi_colors_only: When True, restrict to 16 ANSI colors only. (`bool` or :class:`~prompt_toolkit.filters.Filter`.) """ stdout = stdout or sys.__stdout__ true_color = to_filter(true_color) if is_windows(): from .conemu import ConEmuOutput from .win32 import Win32Output from .windows10 import is_win_vt100_enabled, Windows10_Output if is_win_vt100_enabled(): return Windows10_Output(stdout) if is_conemu_ansi(): return ConEmuOutput(stdout) else: return Win32Output(stdout) else: from .vt100 import Vt100_Output term = os.environ.get('TERM', '') if PY2: term = term.decode('utf-8') return Vt100_Output.from_pty(stdout, true_color=true_color, ansi_colors_only=ansi_colors_only, term=term)
def create_output(stdout=None, true_color=False): """ Return an :class:`~prompt_toolkit.output.Output` instance for the command line. :param true_color: When True, use 24bit colors instead of 256 colors. (`bool` or :class:`~prompt_toolkit.filters.SimpleFilter`.) Notes ----- This method was forked from the mainline prompt-toolkit repo. Copyright (c) 2014, Jonathan Slenders, All rights reserved. This is deprecated and slated for removal after a prompt-toolkit v0.57+ release. """ stdout = stdout or sys.__stdout__ true_color = to_simple_filter(true_color) if is_windows(): if is_conemu_ansi(): return ConEmuOutput(stdout) else: return Win32Output(stdout) else: term = os.environ.get('TERM', '') if PY2: term = term.decode('utf-8') return Vt100_Output.from_pty(stdout, true_color=true_color)#, term=term)
def create_output(stdout: Optional[TextIO] = None) -> Output: """ Return an :class:`~prompt_toolkit.output.Output` instance for the command line. :param stdout: The stdout object """ if stdout is None: # By default, render to stdout. If the output is piped somewhere else, # render to stderr. The prompt_toolkit render output is not meant to be # consumed by something other then a terminal, so this is a reasonable # default. if sys.stdout.isatty(): stdout = sys.stdout else: stdout = sys.stderr if is_windows(): from .conemu import ConEmuOutput from .win32 import Win32Output from .windows10 import is_win_vt100_enabled, Windows10_Output if is_win_vt100_enabled(): return cast(Output, Windows10_Output(stdout)) if is_conemu_ansi(): return cast(Output, ConEmuOutput(stdout)) else: return Win32Output(stdout) else: from .vt100 import Vt100_Output return Vt100_Output.from_pty(stdout, term=get_term_environment_variable())
def win_ansi_support(): if ON_WINDOWS: try: from prompt_toolkit.utils import is_windows_vt100_supported, is_conemu_ansi except ImportError: return False return is_conemu_ansi() or is_windows_vt100_supported() else: return False
def create_output(stdout: Optional[TextIO] = None, always_prefer_tty: bool = True) -> Output: """ Return an :class:`~prompt_toolkit.output.Output` instance for the command line. :param stdout: The stdout object :param always_prefer_tty: When set, look for `sys.stderr` if `sys.stdout` is not a TTY. (The prompt_toolkit render output is not meant to be consumed by something other then a terminal, so this is a reasonable default.) """ if stdout is None: # By default, render to stdout. If the output is piped somewhere else, # render to stderr. stdout = sys.stdout if always_prefer_tty: for io in [sys.stdout, sys.stderr]: if io.isatty(): stdout = io break # If the patch_stdout context manager has been used, then sys.stdout is # replaced by this proxy. For prompt_toolkit applications, we want to use # the real stdout. while isinstance(stdout, StdoutProxy): stdout = stdout.original_stdout if is_windows(): from .conemu import ConEmuOutput from .win32 import Win32Output from .windows10 import is_win_vt100_enabled, Windows10_Output if is_win_vt100_enabled(): return cast(Output, Windows10_Output(stdout)) if is_conemu_ansi(): return cast(Output, ConEmuOutput(stdout)) else: return Win32Output(stdout) else: from .vt100 import Vt100_Output return Vt100_Output.from_pty(stdout, term=get_term_environment_variable())
def create_output(stdout=None): """ Return an :class:`~prompt_toolkit.output.Output` instance for the command line. :param stdout: The stdout object """ stdout = stdout or sys.__stdout__ if is_windows(): from .conemu import ConEmuOutput from .win32 import Win32Output from .windows10 import is_win_vt100_enabled, Windows10_Output if is_win_vt100_enabled(): return Windows10_Output(stdout) if is_conemu_ansi(): return ConEmuOutput(stdout) else: return Win32Output(stdout) else: from .vt100 import Vt100_Output return Vt100_Output.from_pty(stdout, term=get_term_environment_variable())
def create_output(stdout=None): """ Return an :class:`~prompt_toolkit.output.Output` instance for the command line. :param stdout: The stdout object """ stdout = stdout or sys.__stdout__ if is_windows(): from .conemu import ConEmuOutput from .win32 import Win32Output from .windows10 import is_win_vt100_enabled, Windows10_Output if is_win_vt100_enabled(): return Windows10_Output(stdout) if is_conemu_ansi(): return ConEmuOutput(stdout) else: return Win32Output(stdout) else: from .vt100 import Vt100_Output return Vt100_Output.from_pty( stdout, term=get_term_environment_variable())
# history.existinginput: '#888888', # Help Window. 'window-border': '#aaaaaa', 'window-title': 'bg:#bbbbbb #000000', # Meta-enter message. 'accept-message': 'bg:#ffff88 #444444', # Exit confirmation. 'exit-confirmation': 'bg:#884444 #ffffff', } # Some changes to get a bit more contrast on Windows consoles. # (They only support 16 colors.) if is_windows() and not is_conemu_ansi() and not is_windows_vt100_supported(): default_ui_style.update({ 'sidebar.title': 'bg:#00ff00 #ffffff', 'exitconfirmation': 'bg:#ff4444 #ffffff', 'toolbar.validation': 'bg:#ff4444 #ffffff', 'menu.completions.completion': 'bg:#ffffff #000000', 'menu.completions.completion.current': 'bg:#aaaaaa #000000', }) blue_ui_style = {} blue_ui_style.update(default_ui_style)
# Help Window. Token.Window.Border: '#bbbbbb', Token.Window.Title: 'bg:#bbbbbb #000000', # Meta-enter message. Token.AcceptMessage: 'bg:#ffff88 #444444', # Exit confirmation. Token.ExitConfirmation: 'bg:#884444 #ffffff', } # Some changes to get a bit more contrast on Windows consoles. # (They only support 16 colors.) if is_windows() and not is_conemu_ansi(): default_ui_style.update({ Token.Sidebar.Title: 'bg:#00ff00 #ffffff', Token.ExitConfirmation: 'bg:#ff4444 #ffffff', Token.Toolbar.Validation: 'bg:#ff4444 #ffffff', Token.Menu.Completions.Completion: 'bg:#ffffff #000000', Token.Menu.Completions.Completion.Current: 'bg:#aaaaaa #000000', }) blue_ui_style = {} blue_ui_style.update(default_ui_style) blue_ui_style.update({ # Line numbers. Token.LineNumber: '#aa6666',
def __init__(self, looper, basedirpath, nodeReg, cliNodeReg, output=None, debug=False, logFileName=None): self.curClientPort = None logging.root.addHandler(CliHandler(self.out)) self.looper = looper self.basedirpath = basedirpath self.nodeReg = nodeReg self.cliNodeReg = cliNodeReg # Used to store created clients self.clients = {} # clientName -> Client # To store the created requests self.requests = {} # To store the nodes created self.nodes = {} self.externalClientKeys = {} # type: Dict[str,str] self.cliCmds = {'status', 'new'} self.nodeCmds = self.cliCmds | {'keyshare'} self.helpablesCommands = self.cliCmds | self.nodeCmds self.simpleCmds = {'status', 'exit', 'quit', 'license'} self.commands = {'list', 'help'} | self.simpleCmds self.cliActions = {'send', 'show'} self.commands.update(self.cliCmds) self.commands.update(self.nodeCmds) self.node_or_cli = ['node', 'client'] self.nodeNames = list(self.nodeReg.keys()) + ["all"] self.debug = debug self.plugins = {} ''' examples: status new node Alpha new node all new client Joe client Joe send <msg> client Joe show 1 ''' psep = re.escape(os.path.sep) self.utilGrams = [ "(\s* (?P<simple>{}) \s*) |".format(self.relist(self.simpleCmds)), "(\s* (?P<load>load) \s+ (?P<file_name>[.a-zA-z0-9{}]+) \s*) |".format(psep), "(\s* (?P<command>help) (\s+ (?P<helpable>[a-zA-Z0-9]+) )? (\s+ (?P<node_or_cli>{}) )?\s*) |".format(self.relist(self.node_or_cli)), "(\s* (?P<command>list) \s*)" ] self.nodeGrams = [ "(\s* (?P<node_command>{}) \s+ (?P<node_or_cli>nodes?) \s+ (?P<node_name>[a-zA-Z0-9]+)\s*) |".format(self.relist(self.nodeCmds)), "(\s* (?P<load_plugins>load\s+plugins\s+from) \s+ (?P<plugin_dir>[a-zA-Z0-9-:{}]+) \s*)".format(psep), ] self.clientGrams = [ "(\s* (?P<client_command>{}) \s+ (?P<node_or_cli>clients?) \s+ (?P<client_name>[a-zA-Z0-9]+) \s*) |".format(self.relist(self.cliCmds)), "(\s* (?P<client>client) \s+ (?P<client_name>[a-zA-Z0-9]+) \s+ (?P<cli_action>send) \s+ (?P<msg>\{\s*.*\}) \s*) |", "(\s* (?P<client>client) \s+ (?P<client_name>[a-zA-Z0-9]+) \s+ (?P<cli_action>show) \s+ (?P<req_id>[0-9]+) \s*) |", "(\s* (?P<add_key>add\s+key) \s+ (?P<verkey>[a-fA-F0-9]+) \s+ (?P<for_client>for\s+client) \s+ (?P<identifier>[a-zA-Z0-9]+) \s*)", ] self.lexers = { 'node_command': SimpleLexer(Token.Keyword), 'command': SimpleLexer(Token.Keyword), 'helpable': SimpleLexer(Token.Keyword), 'load_plugins': SimpleLexer(Token.Keyword), 'load': SimpleLexer(Token.Keyword), 'node_or_cli': SimpleLexer(Token.Keyword), 'arg1': SimpleLexer(Token.Name), 'node_name': SimpleLexer(Token.Name), 'more_nodes': SimpleLexer(Token.Name), 'simple': SimpleLexer(Token.Keyword), 'client_command': SimpleLexer(Token.Keyword), 'add_key': SimpleLexer(Token.Keyword), 'verkey': SimpleLexer(Token.Literal), 'for_client': SimpleLexer(Token.Keyword), 'identifier': SimpleLexer(Token.Name), } self.clientWC = WordCompleter([]) self.completers = { 'node_command': WordCompleter(self.nodeCmds), 'client_command': WordCompleter(self.cliCmds), 'client': WordCompleter(['client']), 'command': WordCompleter(self.commands), 'node_or_cli': WordCompleter(self.node_or_cli), 'node_name': WordCompleter(self.nodeNames), 'more_nodes': WordCompleter(self.nodeNames), 'helpable': WordCompleter(self.helpablesCommands), 'load_plugins': WordCompleter(['load plugins from']), 'client_name': self.clientWC, 'cli_action': WordCompleter(self.cliActions), 'simple': WordCompleter(self.simpleCmds), 'add_key': WordCompleter(['add key']), 'for_client': WordCompleter(['for client']), } self.initializeGrammar() self.initializeGrammarLexer() self.initializeGrammarCompleter() self.style = PygmentsStyle.from_defaults({ Token.Operator: '#33aa33 bold', Token.Number: '#aa3333 bold', Token.Name: '#ffff00 bold', Token.Heading: 'bold', Token.TrailingInput: 'bg:#662222 #ffffff', Token.BoldGreen: '#33aa33 bold', Token.BoldOrange: '#ff4f2f bold', Token.BoldBlue: '#095cab bold'}) self.functionMappings = self.createFunctionMappings() self.voidMsg = "<none>" # Create an asyncio `EventLoop` object. This is a wrapper around the # asyncio loop that can be passed into prompt_toolkit. eventloop = create_asyncio_eventloop(looper.loop) pers_hist = FileHistory('.{}-cli-history'.format(self.name)) # Create interface. app = create_prompt_application('{}> '.format(self.name), lexer=self.grammarLexer, completer=self.grammarCompleter, style=self.style, history=pers_hist) if output: out = output else: if is_windows(): if is_conemu_ansi(): out = ConEmuOutput(sys.__stdout__) else: out = Win32Output(sys.__stdout__) else: out = CustomOutput.from_pty(sys.__stdout__, true_color=True) self.cli = CommandLineInterface( application=app, eventloop=eventloop, output=out) # Patch stdout in something that will always print *above* the prompt # when something is written to stdout. sys.stdout = self.cli.stdout_proxy() setupLogging(TRACE_LOG_LEVEL, Console.Wordage.mute, filename=logFileName) self.logger = getlogger("cli") self.print("\n{}-CLI (c) 2016 Evernym, Inc.".format(self.properName)) self.print("Node registry loaded.") self.print("None of these are created or running yet.") self.showNodeRegistry() self.print("Type 'help' for more information.")
def create_output(stdout: Optional[TextIO] = None, always_prefer_tty: bool = False) -> Output: """ Return an :class:`~prompt_toolkit.output.Output` instance for the command line. :param stdout: The stdout object :param always_prefer_tty: When set, look for `sys.stderr` if `sys.stdout` is not a TTY. Useful if `sys.stdout` is redirected to a file, but we still want user input and output on the terminal. By default, this is `False`. If `sys.stdout` is not a terminal (maybe it's redirected to a file), then a `PlainTextOutput` will be returned. That way, tools like `print_formatted_text` will write plain text into that file. """ # Consider TERM, PROMPT_TOOLKIT_BELL, and PROMPT_TOOLKIT_COLOR_DEPTH # environment variables. Notice that PROMPT_TOOLKIT_COLOR_DEPTH value is # the default that's used if the Application doesn't override it. term_from_env = get_term_environment_variable() bell_from_env = get_bell_environment_variable() color_depth_from_env = ColorDepth.from_env() if stdout is None: # By default, render to stdout. If the output is piped somewhere else, # render to stderr. stdout = sys.stdout if always_prefer_tty: for io in [sys.stdout, sys.stderr]: if io is not None and io.isatty(): # (This is `None` when using `pythonw.exe` on Windows.) stdout = io break # If the output is still `None`, use a DummyOutput. # This happens for instance on Windows, when running the application under # `pythonw.exe`. In that case, there won't be a terminal Window, and # stdin/stdout/stderr are `None`. if stdout is None: return DummyOutput() # If the patch_stdout context manager has been used, then sys.stdout is # replaced by this proxy. For prompt_toolkit applications, we want to use # the real stdout. from prompt_toolkit.patch_stdout import StdoutProxy while isinstance(stdout, StdoutProxy): stdout = stdout.original_stdout if is_windows(): from .conemu import ConEmuOutput from .win32 import Win32Output from .windows10 import Windows10_Output, is_win_vt100_enabled if is_win_vt100_enabled(): return cast( Output, Windows10_Output(stdout, default_color_depth=color_depth_from_env), ) if is_conemu_ansi(): return cast( Output, ConEmuOutput(stdout, default_color_depth=color_depth_from_env)) else: return Win32Output(stdout, default_color_depth=color_depth_from_env) else: from .vt100 import Vt100_Output # Stdout is not a TTY? Render as plain text. # This is mostly useful if stdout is redirected to a file, and # `print_formatted_text` is used. if not stdout.isatty(): return PlainTextOutput(stdout) return Vt100_Output.from_pty( stdout, term=term_from_env, default_color_depth=color_depth_from_env, enable_bell=bell_from_env, )
# Help Window. 'window-border': '#aaaaaa', 'window-title': 'bg:#bbbbbb #000000', # Meta-enter message. 'accept-message': 'bg:#ffff88 #444444', # Exit confirmation. 'exit-confirmation': 'bg:#884444 #ffffff', } # Some changes to get a bit more contrast on Windows consoles. # (They only support 16 colors.) if is_windows() and not is_conemu_ansi() and not is_windows_vt100_supported(): default_ui_style.update({ 'sidebar.title': 'bg:#00ff00 #ffffff', 'exitconfirmation': 'bg:#ff4444 #ffffff', 'toolbar.validation': 'bg:#ff4444 #ffffff', 'menu.completions.completion': 'bg:#ffffff #000000', 'menu.completions.completion.current': 'bg:#aaaaaa #000000', }) blue_ui_style = {} blue_ui_style.update(default_ui_style) #blue_ui_style.update({ # # Line numbers. # Token.LineNumber: '#aa6666',