##// END OF EJS Templates
Merge pull request #2609 from tkf/history-unique...
Thomas Kluyver -
r9030:751e3470 merge
parent child Browse files
Show More
@@ -307,7 +307,7 b' class HistoryAccessor(Configurable):'
307
307
308 @catch_corrupt_db
308 @catch_corrupt_db
309 def search(self, pattern="*", raw=True, search_raw=True,
309 def search(self, pattern="*", raw=True, search_raw=True,
310 output=False, n=None):
310 output=False, n=None, unique=False):
311 """Search the database using unix glob-style matching (wildcards
311 """Search the database using unix glob-style matching (wildcards
312 * and ?).
312 * and ?).
313
313
@@ -322,6 +322,8 b' class HistoryAccessor(Configurable):'
322 n : None or int
322 n : None or int
323 If an integer is given, it defines the limit of
323 If an integer is given, it defines the limit of
324 returned entries.
324 returned entries.
325 unique : bool
326 When it is true, return only unique entries.
325
327
326 Returns
328 Returns
327 -------
329 -------
@@ -333,9 +335,13 b' class HistoryAccessor(Configurable):'
333 self.writeout_cache()
335 self.writeout_cache()
334 sqlform = "WHERE %s GLOB ?" % tosearch
336 sqlform = "WHERE %s GLOB ?" % tosearch
335 params = (pattern,)
337 params = (pattern,)
338 if unique:
339 sqlform += ' GROUP BY {0}'.format(tosearch)
336 if n is not None:
340 if n is not None:
337 sqlform += " ORDER BY session DESC, line DESC LIMIT ?"
341 sqlform += " ORDER BY session DESC, line DESC LIMIT ?"
338 params += (n,)
342 params += (n,)
343 elif unique:
344 sqlform += " ORDER BY session, line"
339 cur = self._run_sql(sqlform, params, raw=raw, output=output)
345 cur = self._run_sql(sqlform, params, raw=raw, output=output)
340 if n is not None:
346 if n is not None:
341 return reversed(list(cur))
347 return reversed(list(cur))
@@ -89,6 +89,11 b' class HistoryMagics(Magics):'
89 get the last n lines from all sessions. Specify n as a single
89 get the last n lines from all sessions. Specify n as a single
90 arg, or the default is the last 10 lines.
90 arg, or the default is the last 10 lines.
91 """)
91 """)
92 @argument(
93 '-u', dest='unique', action='store_true',
94 help="""
95 when searching history using `-g`, show only unique history.
96 """)
92 @argument('range', nargs='*')
97 @argument('range', nargs='*')
93 @skip_doctest
98 @skip_doctest
94 @line_magic
99 @line_magic
@@ -165,7 +170,7 b' class HistoryMagics(Magics):'
165 else:
170 else:
166 pattern = "*"
171 pattern = "*"
167 hist = history_manager.search(pattern, raw=raw, output=get_output,
172 hist = history_manager.search(pattern, raw=raw, output=get_output,
168 n=limit)
173 n=limit, unique=args.unique)
169 print_nums = True
174 print_nums = True
170 elif args.limit is not _unspecified:
175 elif args.limit is not _unspecified:
171 n = 10 if limit is None else limit
176 n = 10 if limit is None else limit
@@ -61,7 +61,10 b' def test_history():'
61
61
62 # New session
62 # New session
63 ip.history_manager.reset()
63 ip.history_manager.reset()
64 newcmds = ["z=5","class X(object):\n pass", "k='p'"]
64 newcmds = [u"z=5",
65 u"class X(object):\n pass",
66 u"k='p'",
67 u"z=5"]
65 for i, cmd in enumerate(newcmds, start=1):
68 for i, cmd in enumerate(newcmds, start=1):
66 ip.history_manager.store_inputs(i, cmd)
69 ip.history_manager.store_inputs(i, cmd)
67 gothist = ip.history_manager.get_range(start=1, stop=4)
70 gothist = ip.history_manager.get_range(start=1, stop=4)
@@ -70,35 +73,53 b' def test_history():'
70 gothist = ip.history_manager.get_range(-1, 1, 4)
73 gothist = ip.history_manager.get_range(-1, 1, 4)
71 nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist))
74 nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist))
72
75
76 newhist = [(2, i, c) for (i, c) in enumerate(newcmds, 1)]
77
73 # Check get_hist_tail
78 # Check get_hist_tail
74 gothist = ip.history_manager.get_tail(4, output=True,
79 gothist = ip.history_manager.get_tail(5, output=True,
75 include_latest=True)
80 include_latest=True)
76 expected = [(1, 3, (hist[-1], "spam")),
81 expected = [(1, 3, (hist[-1], "spam"))] \
77 (2, 1, (newcmds[0], None)),
82 + [(s, n, (c, None)) for (s, n, c) in newhist]
78 (2, 2, (newcmds[1], None)),
79 (2, 3, (newcmds[2], None)),]
80 nt.assert_equal(list(gothist), expected)
83 nt.assert_equal(list(gothist), expected)
81
84
82 gothist = ip.history_manager.get_tail(2)
85 gothist = ip.history_manager.get_tail(2)
83 expected = [(2, 1, newcmds[0]),
86 expected = newhist[-3:-1]
84 (2, 2, newcmds[1])]
85 nt.assert_equal(list(gothist), expected)
87 nt.assert_equal(list(gothist), expected)
86
88
87 # Check get_hist_search
89 # Check get_hist_search
88 gothist = ip.history_manager.search("*test*")
90 gothist = ip.history_manager.search("*test*")
89 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
91 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
92
90 gothist = ip.history_manager.search("*=*")
93 gothist = ip.history_manager.search("*=*")
91 nt.assert_equal(list(gothist),
94 nt.assert_equal(list(gothist),
92 [(1, 1, hist[0]),
95 [(1, 1, hist[0]),
93 (1, 2, hist[1]),
96 (1, 2, hist[1]),
94 (1, 3, hist[2]),
97 (1, 3, hist[2]),
95 (2, 1, newcmds[0]),
98 newhist[0],
96 (2, 3, newcmds[2])])
99 newhist[2],
97 gothist = ip.history_manager.search("*=*", n=3)
100 newhist[3]])
101
102 gothist = ip.history_manager.search("*=*", n=4)
103 nt.assert_equal(list(gothist),
104 [(1, 3, hist[2]),
105 newhist[0],
106 newhist[2],
107 newhist[3]])
108
109 gothist = ip.history_manager.search("*=*", unique=True)
110 nt.assert_equal(list(gothist),
111 [(1, 1, hist[0]),
112 (1, 2, hist[1]),
113 (1, 3, hist[2]),
114 newhist[2],
115 newhist[3]])
116
117 gothist = ip.history_manager.search("*=*", unique=True, n=3)
98 nt.assert_equal(list(gothist),
118 nt.assert_equal(list(gothist),
99 [(1, 3, hist[2]),
119 [(1, 3, hist[2]),
100 (2, 1, newcmds[0]),
120 newhist[2],
101 (2, 3, newcmds[2])])
121 newhist[3]])
122
102 gothist = ip.history_manager.search("b*", output=True)
123 gothist = ip.history_manager.search("b*", output=True)
103 nt.assert_equal(list(gothist), [(1,3,(hist[2],"spam"))] )
124 nt.assert_equal(list(gothist), [(1,3,(hist[2],"spam"))] )
104
125
General Comments 0
You need to be logged in to leave comments. Login now