summaryrefslogtreecommitdiff
path: root/cmd2
diff options
context:
space:
mode:
authorkotfu <kotfu@kotfu.net>2019-05-24 20:00:16 -0600
committerkotfu <kotfu@kotfu.net>2019-05-24 20:00:16 -0600
commit69308f455193d5b5f89a6edf7c7d3d052bdcd5ce (patch)
tree1651a0670678aabb9e573c740d2037a783c1ccd6 /cmd2
parentba6c5ed5cc006b156093f977856afc03d6c12763 (diff)
downloadcmd2-git-69308f455193d5b5f89a6edf7c7d3d052bdcd5ce.tar.gz
Refactor HistoryItem to not subclass str
Diffstat (limited to 'cmd2')
-rw-r--r--cmd2/cmd2.py14
-rw-r--r--cmd2/history.py52
2 files changed, 36 insertions, 30 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 18a9d5d9..bfc59f3e 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -3410,9 +3410,9 @@ class Cmd(cmd.Cmd):
traceback_war=False)
else:
for runme in history:
- self.pfeedback(runme)
+ self.pfeedback(runme.statement.raw)
if runme:
- self.onecmd_plus_hooks(runme)
+ self.onecmd_plus_hooks(runme.statement.raw)
elif args.edit:
import tempfile
fd, fname = tempfile.mkstemp(suffix='.txt', text=True)
@@ -3432,11 +3432,11 @@ class Cmd(cmd.Cmd):
elif args.output_file:
try:
with open(os.path.expanduser(args.output_file), 'w') as fobj:
- for command in history:
- if command.statement.multiline_command:
- fobj.write('{}\n'.format(command.expanded.rstrip()))
+ for item in history:
+ if item.statement.multiline_command:
+ fobj.write('{}\n'.format(item.statement.expanded_command_line.rstrip()))
else:
- fobj.write('{}\n'.format(command))
+ fobj.write('{}\n'.format(item.statement.raw))
plural = 's' if len(history) > 1 else ''
self.pfeedback('{} command{} saved to {}'.format(len(history), plural, args.output_file))
except Exception as e:
@@ -3547,6 +3547,8 @@ class Cmd(cmd.Cmd):
# the command from the output
first = True
command = ''
+ if isinstance(history_item, HistoryItem):
+ history_item = history_item.statement.raw
for line in history_item.splitlines():
if first:
command += '{}{}\n'.format(self.prompt, line)
diff --git a/cmd2/history.py b/cmd2/history.py
index 3b246f95..dec2e08c 100644
--- a/cmd2/history.py
+++ b/cmd2/history.py
@@ -11,21 +11,25 @@ from . import utils
from .parsing import Statement
-class HistoryItem(str):
+class HistoryItem():
"""Class used to represent one command in the History list"""
- listformat = ' {:>4} {}\n'
- ex_listformat = ' {:>4}x {}\n'
+ _listformat = ' {:>4} {}\n'
+ _ex_listformat = ' {:>4}x {}\n'
- def __new__(cls, statement: Statement):
- """Create a new instance of HistoryItem
+ # def __new__(cls, statement: Statement):
+ # """Create a new instance of HistoryItem
- We must override __new__ because we are subclassing `str` which is
- immutable and takes a different number of arguments as Statement.
- """
- hi = super().__new__(cls, statement.raw)
- hi.statement = statement
- hi.idx = None
- return hi
+ # We must override __new__ because we are subclassing `str` which is
+ # immutable and takes a different number of arguments as Statement.
+ # """
+ # hi = super().__new__(cls, statement.raw)
+ # hi.statement = statement
+ # hi.idx = None
+ # return hi
+
+ def __init__(self, statement: Statement, idx: int):
+ self.statement = statement
+ self.idx = idx
@property
def expanded(self) -> str:
@@ -39,23 +43,24 @@ class HistoryItem(str):
:return: pretty print string version of a HistoryItem
"""
+ expanded_command = self.statement.expanded_command_line
if verbose:
- ret_str = self.listformat.format(self.idx, str(self).rstrip())
- if self != self.expanded:
- ret_str += self.ex_listformat.format(self.idx, self.expanded.rstrip())
+ ret_str = self._listformat.format(self.idx, self.statement.raw)
+ if self.statement.raw != expanded_command.rstrip():
+ ret_str += self._ex_listformat.format(self.idx, expanded_command)
else:
if script:
# display without entry numbers
if expanded or self.statement.multiline_command:
- ret_str = self.expanded.rstrip()
+ ret_str = expanded_command.rstrip()
else:
- ret_str = str(self)
+ ret_str = self.statement.raw
else:
# display a numbered list
if expanded or self.statement.multiline_command:
- ret_str = self.listformat.format(self.idx, self.expanded.rstrip())
+ ret_str = self._listformat.format(self.idx, expanded_command.rstrip())
else:
- ret_str = self.listformat.format(self.idx, str(self).rstrip())
+ ret_str = self._listformat.format(self.idx, self.statement.raw.rstrip())
return ret_str
@@ -85,9 +90,8 @@ class History(list):
:param new: command line to convert to HistoryItem and add to the end of the History list
"""
- new = HistoryItem(new)
- list.append(self, new)
- new.idx = len(self)
+ history_item = HistoryItem(new, len(self)+1)
+ list.append(self, history_item)
def get(self, index: Union[int, str]) -> HistoryItem:
"""Get item from the History list using 1-based indexing.
@@ -206,7 +210,7 @@ class History(list):
def isin(history_item):
"""filter function for string search of history"""
sloppy = utils.norm_fold(search)
- return sloppy in utils.norm_fold(history_item) or sloppy in utils.norm_fold(history_item.expanded)
+ return sloppy in utils.norm_fold(history_item.statement.raw) or sloppy in utils.norm_fold(history_item.statement.expanded_command_line)
return [item for item in self if isin(item)]
def regex_search(self, regex: str) -> List[HistoryItem]:
@@ -222,7 +226,7 @@ class History(list):
def isin(hi):
"""filter function for doing a regular expression search of history"""
- return finder.search(hi) or finder.search(hi.expanded)
+ return finder.search(hi.statement.raw) or finder.search(hi.statement.expanded_command_line)
return [itm for itm in self if isin(itm)]
def truncate(self, max_length:int) -> None: