Merge pull request #2768 from xonsh/fix_ptk2_reverse_history

Fix recent history item ordering in ptk2 shell
This commit is contained in:
Anthony Scopatz 2018-08-16 15:22:53 -04:00 committed by GitHub
commit d69b4814d4
Failed to generate hash of commit
7 changed files with 42 additions and 22 deletions

View file

@ -0,0 +1,14 @@
**Added:** None
**Changed:** None
**Deprecated:** None
**Removed:** None
**Fixed:**
* Recent command history in ptk2 prompt now returns most recently executed
commands first (as expected)
**Security:** None

View file

@ -114,11 +114,11 @@ class History:
"""Flush the history items to disk from a buffer."""
pass
def items(self):
def items(self, newest_first=False):
"""Get history items of current session."""
raise NotImplementedError
def all_items(self):
def all_items(self, newest_first=False):
"""Get all history items."""
raise NotImplementedError

View file

@ -9,11 +9,11 @@ class DummyHistory(History):
def append(self, cmd):
pass
def items(self):
def items(self, newest_first=False):
yield {'inp': 'dummy in action', 'ts': 1464652800, 'ind': 0}
def all_items(self):
return self.items()
def all_items(self, newest_first=False):
return self.items(newest_first=newest_first)
def info(self):
data = collections.OrderedDict()

View file

@ -379,12 +379,16 @@ class JsonHistory(History):
self.buffer.clear()
return hf
def items(self):
def items(self, newest_first=False):
"""Display history items of current session."""
for item, tss in zip(self.inps, self.tss):
if newest_first:
items = zip(reversed(self.inps), reversed(self.tss))
else:
items = zip(self.inps, self.tss)
for item, tss in items:
yield {'inp': item.rstrip(), 'ts': tss[0]}
def all_items(self, **kwargs):
def all_items(self, newest_first=False, **kwargs):
"""
Returns all history as found in XONSH_DATA_DIR.
@ -392,7 +396,7 @@ class JsonHistory(History):
"""
while self.gc and self.gc.is_alive():
time.sleep(0.011) # gc sleeps for 0.01 secs, sleep a beat longer
for f in _xhj_get_history_files():
for f in _xhj_get_history_files(newest_first=newest_first):
try:
json_file = xlj.LazyJSON(f, reopen=False)
except ValueError:
@ -406,6 +410,8 @@ class JsonHistory(History):
msg = 'xonsh history file {0!r} is not valid JSON'
print(msg.format(f), file=sys.stderr)
continue
if newest_first:
commands = reversed(commands)
for c in commands:
yield {'inp': c['inp'].rstrip(), 'ts': c['ts'][0]}
# all items should also include session items

View file

@ -40,18 +40,18 @@ def construct_history(**kwargs):
return kls_history(**kwargs)
def _xh_session_parser(hist=None, **kwargs):
def _xh_session_parser(hist=None, newest_first=False, **kwargs):
"""Returns history items of current session."""
if hist is None:
hist = builtins.__xonsh_history__
return hist.items()
return hist.items(newest_first=newest_first)
def _xh_all_parser(hist=None, **kwargs):
def _xh_all_parser(hist=None, newest_first=False, **kwargs):
"""Returns all history items."""
if hist is None:
hist = builtins.__xonsh_history__
return hist.all_items()
return hist.all_items(newest_first=newest_first)
def _xh_find_histfile_var(file_list, default=None):

View file

@ -78,14 +78,14 @@ def _xh_sqlite_get_count(cursor, sessionid=None):
return cursor.fetchone()[0]
def _xh_sqlite_get_records(cursor, sessionid=None, limit=None, reverse=False):
def _xh_sqlite_get_records(cursor, sessionid=None, limit=None, newest_first=False):
sql = 'SELECT inp, tsb, rtn FROM xonsh_history '
params = []
if sessionid is not None:
sql += 'WHERE sessionid = ? '
params.append(sessionid)
sql += 'ORDER BY tsb '
if reverse:
if newest_first:
sql += 'DESC '
if limit is not None:
sql += 'LIMIT %d ' % limit
@ -121,11 +121,11 @@ def xh_sqlite_get_count(sessionid=None, filename=None):
return _xh_sqlite_get_count(c, sessionid=sessionid)
def xh_sqlite_items(sessionid=None, filename=None):
def xh_sqlite_items(sessionid=None, filename=None, newest_first=False):
with _xh_sqlite_get_conn(filename=filename) as conn:
c = conn.cursor()
_xh_sqlite_create_history_table(c)
return _xh_sqlite_get_records(c, sessionid=sessionid)
return _xh_sqlite_get_records(c, sessionid=sessionid, newest_first=newest_first)
def xh_sqlite_delete_items(size_to_keep, filename=None):
@ -208,15 +208,15 @@ class SqliteHistory(History):
cmd, str(self.sessionid), store_stdout,
filename=self.filename)
def all_items(self):
def all_items(self, newest_first=False):
"""Display all history items."""
for item in xh_sqlite_items(filename=self.filename):
for item in xh_sqlite_items(filename=self.filename, newest_first=newest_first):
yield {'inp': item[0], 'ts': item[1], 'rtn': item[2]}
def items(self):
def items(self, newest_first=False):
"""Display history items of current session."""
for item in xh_sqlite_items(
sessionid=str(self.sessionid), filename=self.filename):
sessionid=str(self.sessionid), filename=self.filename, newest_first=newest_first):
yield {'inp': item[0], 'ts': item[1], 'rtn': item[2]}
def info(self):

View file

@ -25,7 +25,7 @@ class PromptToolkitHistory(prompt_toolkit.history.History):
hist = builtins.__xonsh_history__
if hist is None:
return
for cmd in hist.all_items():
for cmd in hist.all_items(newest_first=True):
line = cmd['inp'].rstrip()
strs = self.get_strings()
if len(strs) == 0 or line != strs[-1]: