Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ctrl + w shortcut to delete a word #11

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Keyboard Shortcuts
| expression, or nothing. This is discussed in the "Output" section above.
``Ctrl + ]``
| Clear the current expression.
``Ctrl + w``
| Delete the word at the cursor position from the current expression.

Mouse Clicks
------------
Expand Down
15 changes: 15 additions & 0 deletions jpterm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""JMESPath text terminal."""
import io
import os
import re
import sys
import json
import argparse
Expand Down Expand Up @@ -73,6 +74,7 @@ def __init__(self, input_data, output_mode='result'):
self.output_mode = output_mode
self.last_result = None
self.last_expression = None
self.del_word_pattern = re.compile(r"(\w+|[^\w\s]+)\s*$")

def _create_colorized_json(self, json_string):
tokens = self.lexer.get_tokens(json_string)
Expand Down Expand Up @@ -163,6 +165,19 @@ def unhandled_input(self, key):
(OUTPUT_MODES.index(self.output_mode) + 1) % len(OUTPUT_MODES)]
self.output_mode = new_mode
self.footer.set_text("Status: output mode set to %s" % new_mode)
elif key == "ctrl w":
self._del_word_at_cursor()

def _del_word_at_cursor(self):
edit_text = [
self.input_expr.edit_text[:self.input_expr.edit_pos],
self.input_expr.edit_text[self.input_expr.edit_pos:]
]
edit_text[0] = self.del_word_pattern.sub("", edit_text[0])
new_text = "".join(edit_text)
new_pos = self.input_expr.edit_pos - (len(self.input_expr.edit_text) - len(new_text))
self.input_expr.edit_text = new_text
self.input_expr.edit_pos = new_pos

def display_output(self, filename):
if self.output_mode == 'result' and \
Expand Down