本文整理汇总了Python中prompt_toolkit.patch_stdout.patch_stdout方法的典型用法代码示例。如果您正苦于以下问题:Python patch_stdout.patch_stdout方法的具体用法?Python patch_stdout.patch_stdout怎么用?Python patch_stdout.patch_stdout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类prompt_toolkit.patch_stdout
的用法示例。
在下文中一共展示了patch_stdout.patch_stdout方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from prompt_toolkit import patch_stdout [as 别名]
# 或者: from prompt_toolkit.patch_stdout import patch_stdout [as 别名]
def main():
# Create user interface.
hello_world_window()
# Enable threading in GTK. (Otherwise, GTK will keep the GIL.)
gtk.gdk.threads_init()
# Read input from the command line, using an event loop with this hook.
# We use `patch_stdout`, because clicking the button will print something;
# and that should print nicely 'above' the input line.
with patch_stdout():
session = PromptSession(
"Python >>> ", inputhook=inputhook, lexer=PygmentsLexer(PythonLexer)
)
result = session.prompt()
print("You said: %s" % result)
示例2: main
# 需要导入模块: from prompt_toolkit import patch_stdout [as 别名]
# 或者: from prompt_toolkit.patch_stdout import patch_stdout [as 别名]
def main():
# Print a counter every second in another thread.
running = True
def thread():
i = 0
while running:
i += 1
print("i=%i" % i)
time.sleep(1)
t = threading.Thread(target=thread)
t.daemon = True
t.start()
# Now read the input. The print statements of the other thread
# should not disturb anything.
with patch_stdout():
result = prompt("Say something: ")
print("You said: %s" % result)
# Stop thread.
running = False
示例3: run
# 需要导入模块: from prompt_toolkit import patch_stdout [as 别名]
# 或者: from prompt_toolkit.patch_stdout import patch_stdout [as 别名]
def run(self):
self._thread.start()
self._bot_status_monitor.start()
with patch_stdout(raw=True):
while True:
# Get our text
try:
text = self.prompt(rprompt=self._get_rprompt_tokens)
# KeyboardInterrupt continues
except KeyboardInterrupt:
continue
# End of file returns
except EOFError:
return
# Clean text
text = text.lstrip()
# Handle empty text
if text == '':
self._logger.debug(' empty string found')
continue
# Program specific handeling. Currently either first word
# or second word can be commands
for string in text.split('&&'):
self._handle_text(string)
示例4: prompt
# 需要导入模块: from prompt_toolkit import patch_stdout [as 别名]
# 或者: from prompt_toolkit.patch_stdout import patch_stdout [as 别名]
def prompt(*args, **kwargs):
prompt_init()
with patch_stdout():
try:
while True:
tmp = await prompt_toolkit.prompt(*args, async_=True, **kwargs)
if tmp:
break
return tmp
except EOFError:
return None
示例5: run
# 需要导入模块: from prompt_toolkit import patch_stdout [as 别名]
# 或者: from prompt_toolkit.patch_stdout import patch_stdout [as 别名]
def run(self):
if self._ignore_sigint and sys.platform != "win32":
asyncio.get_event_loop().add_signal_handler(signal.SIGINT, self._sigint_handler)
self.session = PromptSession(enable_history_search=True, key_bindings=self._get_bindings())
try:
with patch_stdout():
await self._run_prompt_forever()
finally:
if self._ignore_sigint and sys.platform != "win32":
asyncio.get_event_loop().remove_signal_handler(signal.SIGINT)
self._on_close()
示例6: main
# 需要导入模块: from prompt_toolkit import patch_stdout [as 别名]
# 或者: from prompt_toolkit.patch_stdout import patch_stdout [as 别名]
def main():
bottom_toolbar = HTML(
' <b>[f]</b> Print "f" <b>[q]</b> Abort <b>[x]</b> Send Control-C.'
)
# Create custom key bindings first.
kb = KeyBindings()
cancel = [False]
@kb.add("f")
def _(event):
print("You pressed `f`.")
@kb.add("q")
def _(event):
" Quit by setting cancel flag. "
cancel[0] = True
@kb.add("x")
def _(event):
" Quit by sending SIGINT to the main thread. "
os.kill(os.getpid(), signal.SIGINT)
# Use `patch_stdout`, to make sure that prints go above the
# application.
with patch_stdout():
with ProgressBar(key_bindings=kb, bottom_toolbar=bottom_toolbar) as pb:
for i in pb(range(800)):
time.sleep(0.01)
if cancel[0]:
break
示例7: main
# 需要导入模块: from prompt_toolkit import patch_stdout [as 别名]
# 或者: from prompt_toolkit.patch_stdout import patch_stdout [as 别名]
def main():
with patch_stdout():
background_task = asyncio.create_task(print_counter())
try:
await interactive_shell()
finally:
background_task.cancel()
print("Quitting event loop. Bye.")
示例8: cmdloop
# 需要导入模块: from prompt_toolkit import patch_stdout [as 别名]
# 或者: from prompt_toolkit.patch_stdout import patch_stdout [as 别名]
def cmdloop(self):
use_asyncio_event_loop()
self.db = await aiosqlite.connect(self.db_path)
try:
while True:
#with patch_stdout():
text = await self.prompt_session.prompt(async_=True)
command = shlex.split(text)
if len(command):
# Apperently you can't call await on a coroutine retrieved via getattr() ??
# So this sucks now but thankfully we don't have a lot of commands
try:
if command[0] == 'exit':
await self.exit()
break
elif command[0] == 'show':
await self.show(command[1:])
elif command[0] == 'open':
await self.open(command[1:])
elif command[0] == 'hosts':
await self.hosts(command[1:])
elif command[0] == 'servers':
await self.servers(command[1:])
elif command[0] == 'scan':
await self.scan()
except Exception as e:
import traceback
traceback.print_exc()
print(f"Error calling command '{command[0]}': {e}")
finally:
await self.db.close()
示例9: prompt
# 需要导入模块: from prompt_toolkit import patch_stdout [as 别名]
# 或者: from prompt_toolkit.patch_stdout import patch_stdout [as 别名]
def prompt(self, text=None):
'''
Prompt for user input from stdin.
'''
if self.sess is None:
hist = FileHistory(s_common.getSynPath('cmdr_history'))
self.sess = PromptSession(history=hist)
if text is None:
text = self.cmdprompt
with patch_stdout():
retn = await self.sess.prompt_async(text, vi_mode=self.vi_mode, enable_open_in_editor=True)
return retn
示例10: repl
# 需要导入模块: from prompt_toolkit import patch_stdout [as 别名]
# 或者: from prompt_toolkit.patch_stdout import patch_stdout [as 别名]
def repl(commands:Dict, mode="", help_command=None):
commandCompleter = WordCompleter(
[c for c in commands],
sentence=True # allows hyphens
)
oldSession = state.Session
state.Session = PromptSession(key_bindings=Bindings,
bottom_toolbar=bottom_toolbar,
refresh_interval=0.1)
state.Wallet.shards.interface.options = {'bottom_toolbar': bottom_toolbar}
done = None
with patch_stdout():
while not done:
try:
unlocked = ' '
if state.Wallet.unlocked():
unlocked = '*'
command_line = state.Session.prompt(HTML('<b>{}{}></b> '.format(unlocked, mode)),
completer=commandCompleter,
).split()
if len(command_line) == 0:
continue
if command_line[0] in commands:
command_fn = commands[command_line[0]]
try:
done = command_fn(*(command_line[1:]))
except TypeError as err:
if state.Debug:
raise err
if help_command is not None:
help_command(command_line[0])
else:
raise HermitError("Unknown command")
except KeyboardInterrupt:
continue
except HermitError as e:
print(e)
if state.Debug:
traceback.print_exc()
continue
except EOFError:
break
except Exception as err:
print(err)
if state.Debug:
traceback.print_exc()
break
state.Session = oldSession