summaryrefslogtreecommitdiff
path: root/cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-09-23 16:40:18 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2017-09-23 16:40:18 -0400
commit2aa1d9b7b4d7b9eb3e8ff078ab1893d4b5e95090 (patch)
tree9f5e3a1d3ef0ad54940775c3d1bdf34f0152d680 /cmd2.py
parent47424648d4c02d07abeb091ebe61240f2c6317d4 (diff)
downloadcmd2-git-2aa1d9b7b4d7b9eb3e8ff078ab1893d4b5e95090.tar.gz
Fixed a bug when edit is passed a large negative integer in quotes
Fixed a weird corner case. Also added some comments to do_edit to better explain what the code is doing.
Diffstat (limited to 'cmd2.py')
-rwxr-xr-xcmd2.py24
1 files changed, 21 insertions, 3 deletions
diff --git a/cmd2.py b/cmd2.py
index 9fc66634..d2cae8f7 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -1669,15 +1669,33 @@ Edited files are run on close if the ``autorun_on_edit`` settable parameter is T
filename = None
if arg and arg[0]:
try:
+ # Try to convert argument to an integer
history_idx = int(arg[0])
except ValueError:
+ # Argument passed is not convertible to an integer, so treat it as a file path
filename = arg[0]
history_item = ''
else:
- history_item = self._last_matching(history_idx)
- if history_item is None:
- self.perror('index {!r} does not exist within the history'.format(history_idx), traceback_war=False)
+ # Argument passed IS convertible to an integer, so treat it as a history index
+
+ # Save off original index for pringing
+ orig_indx = history_idx
+
+ # Convert negative index into equivalent positive one
+ if history_idx < 0:
+ history_idx += len(self.history) + 1
+
+ # Make sure the index is actually within the history
+ if 1 <= history_idx <= len(self.history):
+ history_item = self._last_matching(history_idx)
+ if history_item is None:
+ self.perror('index {!r} does not exist within the history'.format(orig_indx),
+ traceback_war=False)
+ return
+ else:
+ self.perror('index {!r} does not exist within the history'.format(orig_indx), traceback_war=False)
return
+
else:
try:
history_item = self.history[-1]