本文整理汇总了Python中prompt_toolkit.buffer.Buffer.reset方法的典型用法代码示例。如果您正苦于以下问题:Python Buffer.reset方法的具体用法?Python Buffer.reset怎么用?Python Buffer.reset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类prompt_toolkit.buffer.Buffer
的用法示例。
在下文中一共展示了Buffer.reset方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BufferTest
# 需要导入模块: from prompt_toolkit.buffer import Buffer [as 别名]
# 或者: from prompt_toolkit.buffer.Buffer import reset [as 别名]
class BufferTest(unittest.TestCase):
def setUp(self):
self.buffer = Buffer()
def test_initial(self):
self.assertEqual(self.buffer.text, '')
self.assertEqual(self.buffer.cursor_position, 0)
def test_insert_text(self):
self.buffer.insert_text('some_text')
self.assertEqual(self.buffer.text, 'some_text')
self.assertEqual(self.buffer.cursor_position, len('some_text'))
def test_cursor_movement(self):
self.buffer.insert_text('some_text')
self.buffer.cursor_left()
self.buffer.cursor_left()
self.buffer.cursor_left()
self.buffer.cursor_right()
self.buffer.insert_text('A')
self.assertEqual(self.buffer.text, 'some_teAxt')
self.assertEqual(self.buffer.cursor_position, len('some_teA'))
def test_backspace(self):
self.buffer.insert_text('some_text')
self.buffer.cursor_left()
self.buffer.cursor_left()
self.buffer.delete_before_cursor()
self.assertEqual(self.buffer.text, 'some_txt')
self.assertEqual(self.buffer.cursor_position, len('some_t'))
def test_cursor_up(self):
# Cursor up to a line thats longer.
self.buffer.insert_text('long line1\nline2')
self.buffer.cursor_up()
self.assertEqual(self.buffer.document.cursor_position, 5)
# Going up when already at the top.
self.buffer.cursor_up()
self.assertEqual(self.buffer.document.cursor_position, 5)
# Going up to a line that's shorter.
self.buffer.reset()
self.buffer.insert_text('line1\nlong line2')
self.buffer.cursor_up()
self.assertEqual(self.buffer.document.cursor_position, 5)
def test_cursor_down(self):
self.buffer.insert_text('line1\nline2')
self.buffer.cursor_position = 3
# Normally going down
self.buffer.cursor_down()
self.assertEqual(self.buffer.document.cursor_position, len('line1\nlin'))
# Going down to a line that's storter.
self.buffer.reset()
self.buffer.insert_text('long line1\na\nb')
self.buffer.cursor_position = 3
self.buffer.cursor_down()
self.assertEqual(self.buffer.document.cursor_position, len('long line1\na'))
def test_join_next_line(self):
self.buffer.insert_text('line1\nline2\nline3')
self.buffer.cursor_up()
self.buffer.join_next_line()
self.assertEqual(self.buffer.text, 'line1\nline2 line3')
# Test when there is no '\n' in the text
self.buffer.reset()
self.buffer.insert_text('line1')
self.buffer.cursor_position = 0
self.buffer.join_next_line()
self.assertEqual(self.buffer.text, 'line1')
def test_newline(self):
self.buffer.insert_text('hello world')
self.buffer.newline()
self.assertEqual(self.buffer.text, 'hello world\n')
def test_swap_characters_before_cursor(self):
self.buffer.insert_text('hello world')
self.buffer.cursor_left()
self.buffer.cursor_left()
self.buffer.swap_characters_before_cursor()
self.assertEqual(self.buffer.text, 'hello wrold')
示例2: SystemToolbar
# 需要导入模块: from prompt_toolkit.buffer import Buffer [as 别名]
# 或者: from prompt_toolkit.buffer.Buffer import reset [as 别名]
class SystemToolbar(object):
"""
Toolbar for a system prompt.
:param prompt: Prompt to be displayed to the user.
"""
def __init__(self, prompt='Shell command: ', enable_global_bindings=True):
self.prompt = prompt
self.enable_global_bindings = to_filter(enable_global_bindings)
self.system_buffer = Buffer(name=SYSTEM_BUFFER)
self._bindings = self._build_key_bindings()
self.buffer_control = BufferControl(
buffer=self.system_buffer,
lexer=SimpleLexer(style='class:system-toolbar.text'),
input_processors=[BeforeInput(
lambda: self.prompt, style='class:system-toolbar')],
key_bindings=self._bindings)
self.window = Window(
self.buffer_control, height=1,
style='class:system-toolbar')
self.container = ConditionalContainer(
content=self.window,
filter=has_focus(self.system_buffer))
def _get_display_before_text(self):
return [
('class:system-toolbar', 'Shell command: '),
('class:system-toolbar.text', self.system_buffer.text),
('', '\n'),
]
def _build_key_bindings(self):
focused = has_focus(self.system_buffer)
# Emacs
emacs_bindings = KeyBindings()
handle = emacs_bindings.add
@handle('escape', filter=focused)
@handle('c-g', filter=focused)
@handle('c-c', filter=focused)
def _(event):
" Hide system prompt. "
self.system_buffer.reset()
event.app.layout.focus_last()
@handle('enter', filter=focused)
def _(event):
" Run system command. "
event.app.run_system_command(
self.system_buffer.text,
display_before_text=self._get_display_before_text())
self.system_buffer.reset(append_to_history=True)
event.app.layout.focus_last()
# Vi.
vi_bindings = KeyBindings()
handle = vi_bindings.add
@handle('escape', filter=focused)
@handle('c-c', filter=focused)
def _(event):
" Hide system prompt. "
event.app.vi_state.input_mode = InputMode.NAVIGATION
self.system_buffer.reset()
event.app.layout.focus_last()
@handle('enter', filter=focused)
def _(event):
" Run system command. "
event.app.vi_state.input_mode = InputMode.NAVIGATION
event.app.run_system_command(
self.system_buffer.text,
display_before_text=self._get_display_before_text())
self.system_buffer.reset(append_to_history=True)
event.app.layout.focus_last()
# Global bindings. (Listen to these bindings, even when this widget is
# not focussed.)
global_bindings = KeyBindings()
handle = global_bindings.add
@handle(Keys.Escape, '!', filter= ~focused & emacs_mode, is_global=True)
def _(event):
" M-'!' will focus this user control. "
event.app.layout.focus(self.window)
@handle('!', filter=~focused & vi_mode & vi_navigation_mode, is_global=True)
def _(event):
" Focus. "
event.app.vi_state.input_mode = InputMode.INSERT
event.app.layout.focus(self.window)
return merge_key_bindings([
ConditionalKeyBindings(emacs_bindings, emacs_mode),
#.........这里部分代码省略.........
示例3: PythonInput
# 需要导入模块: from prompt_toolkit.buffer import Buffer [as 别名]
# 或者: from prompt_toolkit.buffer.Buffer import reset [as 别名]
#.........这里部分代码省略.........
# Boolean indicating whether we have a signatures thread running.
# (Never run more than one at the same time.)
self._get_signatures_thread_running = False
self.output = output or create_output()
self.input = input or create_input(sys.stdin)
self.style_transformation = merge_style_transformations([
ConditionalStyleTransformation(
SwapLightAndDarkStyleTransformation(),
filter=Condition(lambda: self.swap_light_and_dark)),
AdjustBrightnessStyleTransformation(
lambda: self.min_brightness,
lambda: self.max_brightness),
])
self.ptpython_layout = PtPythonLayout(
self,
lexer=DynamicLexer(
lambda: self._lexer if self.enable_syntax_highlighting else SimpleLexer()),
input_buffer_height=self._input_buffer_height,
extra_buffer_processors=self._extra_buffer_processors,
extra_body=self._extra_layout_body,
extra_toolbars=self._extra_toolbars)
self.app = self._create_application()
if vi_mode:
self.app.editing_mode = EditingMode.VI
def _accept_handler(self, buff):
app = get_app()
app.exit(result=buff.text)
app.pre_run_callables.append(buff.reset)
return True # Keep text, we call 'reset' later on.
@property
def option_count(self):
" Return the total amount of options. (In all categories together.) "
return sum(len(category.options) for category in self.options)
@property
def selected_option(self):
" Return the currently selected option. "
i = 0
for category in self.options:
for o in category.options:
if i == self.selected_option_index:
return o
else:
i += 1
def get_compiler_flags(self):
"""
Give the current compiler flags by looking for _Feature instances
in the globals.
"""
flags = 0
for value in self.get_globals().values():
if isinstance(value, __future__._Feature):
flags |= value.compiler_flag
return flags
@property
示例4: Editor
# 需要导入模块: from prompt_toolkit.buffer import Buffer [as 别名]
# 或者: from prompt_toolkit.buffer.Buffer import reset [as 别名]
class Editor(object):
"""
The main class. Containing the whole editor.
:param config_directory: Place where configuration is stored.
:param input: (Optionally) `prompt_toolkit.input.Input` object.
:param output: (Optionally) `prompt_toolkit.output.Output` object.
"""
def __init__(self, config_directory='~/.pyvim', input=None, output=None):
self.input = input
self.output = output
# Vi options.
self.show_line_numbers = True
self.highlight_search = True
self.paste_mode = False
self.show_ruler = True
self.show_wildmenu = True
self.expand_tab = True # Insect spaces instead of tab characters.
self.tabstop = 4 # Number of spaces that a tab character represents.
self.incsearch = True # Show matches while typing search string.
self.ignore_case = False # Ignore case while searching.
self.enable_mouse_support = True
self.display_unprintable_characters = True # ':set list'
self.enable_jedi = True # ':set jedi', for Python Jedi completion.
self.scroll_offset = 0 # ':set scrolloff'
self.relative_number = False # ':set relativenumber'
self.wrap_lines = True # ':set wrap'
self.break_indent = False # ':set breakindent'
self.cursorline = False # ':set cursorline'
self.cursorcolumn = False # ':set cursorcolumn'
self.colorcolumn = [] # ':set colorcolumn'. List of integers.
# Ensure config directory exists.
self.config_directory = os.path.abspath(os.path.expanduser(config_directory))
if not os.path.exists(self.config_directory):
os.mkdir(self.config_directory)
self.window_arrangement = WindowArrangement(self)
self.message = None
# Load styles. (Mapping from name to Style class.)
self.styles = generate_built_in_styles()
self.current_style = get_editor_style_by_name('vim')
# I/O backends.
self.io_backends = [
DirectoryIO(),
HttpIO(),
GZipFileIO(), # Should come before FileIO.
FileIO(),
]
# Create history and search buffers.
def handle_action(buff):
' When enter is pressed in the Vi command line. '
text = buff.text # Remember: leave_command_mode resets the buffer.
# First leave command mode. We want to make sure that the working
# pane is focussed again before executing the command handlers.
self.leave_command_mode(append_to_history=True)
# Execute command.
handle_command(self, text)
commands_history = FileHistory(os.path.join(self.config_directory, 'commands_history'))
self.command_buffer = Buffer(
accept_handler=handle_action,
enable_history_search=True,
completer=create_command_completer(self),
history=commands_history,
multiline=False)
search_buffer_history = FileHistory(os.path.join(self.config_directory, 'search_history'))
self.search_buffer = Buffer(
history=search_buffer_history,
enable_history_search=True,
multiline=False)
# Create key bindings registry.
self.key_bindings = create_key_bindings(self)
# Create layout and CommandLineInterface instance.
self.editor_layout = EditorLayout(self, self.window_arrangement)
self.application = self._create_application()
# Hide message when a key is pressed.
def key_pressed(_):
self.message = None
self.application.key_processor.before_key_press += key_pressed
# Command line previewer.
self.previewer = CommandPreviewer(self)
def load_initial_files(self, locations, in_tab_pages=False, hsplit=False, vsplit=False):
"""
Load a list of files.
"""
assert in_tab_pages + hsplit + vsplit <= 1 # Max one of these options.
#.........这里部分代码省略.........