align sqlite history backend with public attrs

This commit is contained in:
Hugo Wang 2016-11-30 22:49:13 +08:00
parent 88a5c8332b
commit 1ce4552d0d
2 changed files with 44 additions and 2 deletions

View file

@ -28,6 +28,36 @@ def test_hist_append(hist, xonsh_builtins):
assert 1 == items[0]['rtn']
def test_hist_attrs(hist, xonsh_builtins):
xonsh_builtins.__xonsh_env__['HISTCONTROL'] = set()
hf = hist.append({'inp': 'ls foo', 'rtn': 1})
assert hf is None
assert 'ls foo' == hist.inps[0]
assert 'ls foo' == hist.inps[-1]
assert 1 == hist.rtns[0]
assert 1 == hist.rtns[-1]
assert None is hist.outs[-1]
assert [1] == hist.rtns[:]
hist.append({'inp': 'ls bar', 'rtn': 0})
assert 'ls bar' == hist.inps[1]
assert 'ls bar' == hist.inps[-1]
assert 0 == hist.rtns[1]
assert 0 == hist.rtns[-1]
assert None is hist.outs[-1]
assert [1, 0] == hist.rtns[:]
assert len(hist.tss) == 2
assert len(hist.tss[0]) == 2
def test_histcontrol_options(hist, xonsh_builtins):
# HISTCONTROL options tests
xonsh_builtins.__xonsh_env__['HISTCONTROL'] = 'ignoredups,ignoreerr'
hist.append({'inp': 'ls foo', 'rtn': 0})
hist.append({'inp': 'ls foo', 'rtn': 0})
hist.append({'inp': 'ls bar', 'rtn': 1})
assert ['ls foo'] == hist.inps[:]
CMDS = ['ls', 'cat hello kitty', 'abc', 'def', 'touch me', 'grep from me']

View file

@ -176,18 +176,30 @@ class SqliteHistory(HistoryBase):
self.filename = filename
self.last_cmd_inp = None
self.gc = SqliteHistoryGC() if gc else None
self.inps = []
self.rtns = []
self.outs = []
self.tss = []
def append(self, cmd):
envs = builtins.__xonsh_env__
opts = envs.get('HISTCONTROL')
if 'ignoredups' in opts and cmd['inp'].rstrip() == self.last_cmd_inp:
inp = cmd['inp'].rstrip()
if 'ignoredups' in opts and inp == self.last_cmd_inp:
# Skipping dup cmd
return
if 'ignoreerr' in opts and cmd['rtn'] != 0:
# Skipping failed cmd
return
self.inps.append(inp)
store_stdout = envs.get('XONSH_STORE_STDOUT', False)
self.last_cmd_inp = cmd['inp'].rstrip()
if store_stdout:
self.outs.append(cmd.get('out'))
else:
self.outs.append(None)
self.last_cmd_inp = inp
self.rtns.append(cmd['rtn'])
self.tss.append(cmd.get('ts', (None, None)))
xh_sqlite_append_history(
cmd, str(self.sessionid), store_stdout,
filename=self.filename)