Skip to content

Commit 0abcb70

Browse files
committed
expanded history searches with string or regex for #545
history -v with a string or regex search now searches both the entered command as well as the expanded command
1 parent e174e23 commit 0abcb70

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

cmd2/history.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,14 @@ def isin(hi):
154154
:param hi: HistoryItem
155155
:return: bool - True if search matches
156156
"""
157-
return finder.search(hi)
157+
return finder.search(hi) or finder.search(hi.expanded)
158158
else:
159159
def isin(hi):
160160
"""Listcomp filter function for doing a case-insensitive string search of History.
161161
162162
:param hi: HistoryItem
163163
:return: bool - True if search matches
164164
"""
165-
return utils.norm_fold(getme) in utils.norm_fold(hi)
165+
srch = utils.norm_fold(getme)
166+
return srch in utils.norm_fold(hi) or srch in utils.norm_fold(hi.expanded)
166167
return [itm for itm in self if isin(itm)]

tests/test_history.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,31 @@ def test_history_with_string_argument(base_app):
123123
""")
124124
assert out == expected
125125

126+
def test_history_expanded_with_string_argument(base_app):
127+
run_cmd(base_app, 'alias create sc shortcuts')
128+
run_cmd(base_app, 'help')
129+
run_cmd(base_app, 'help history')
130+
run_cmd(base_app, 'sc')
131+
out = run_cmd(base_app, 'history -v shortcuts')
132+
expected = normalize("""
133+
1 alias create sc shortcuts
134+
4 sc
135+
4x shortcuts
136+
""")
137+
assert out == expected
138+
139+
def test_history_expanded_with_regex_argument(base_app):
140+
run_cmd(base_app, 'alias create sc shortcuts')
141+
run_cmd(base_app, 'help')
142+
run_cmd(base_app, 'help history')
143+
run_cmd(base_app, 'sc')
144+
out = run_cmd(base_app, 'history -v /sh.*cuts/')
145+
expected = normalize("""
146+
1 alias create sc shortcuts
147+
4 sc
148+
4x shortcuts
149+
""")
150+
assert out == expected
126151

127152
def test_history_with_integer_argument(base_app):
128153
run_cmd(base_app, 'help')

0 commit comments

Comments
 (0)