mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-05 00:41:00 +01:00
docs/tests
This commit is contained in:
parent
494e0dc81f
commit
ffb4e5e035
2 changed files with 52 additions and 22 deletions
|
@ -189,15 +189,18 @@ def test_parser_show(args, exp):
|
||||||
|
|
||||||
# CMDS = ['ls', 'cat hello kitty', 'abc', 'def', 'touch me', 'grep from me']
|
# CMDS = ['ls', 'cat hello kitty', 'abc', 'def', 'touch me', 'grep from me']
|
||||||
|
|
||||||
def test_history_getitem(hist, xonsh_builtins):
|
|
||||||
|
@pytest.mark.parametrize('index, exp',[
|
||||||
|
(-1, 'grep from me'),
|
||||||
|
('hello', 'cat hello kitty'),
|
||||||
|
((-1, -1), 'me'),
|
||||||
|
(('hello', 0), 'cat'),
|
||||||
|
((-1, 0:2), 'grep from'),
|
||||||
|
(('kitty', 1:), 'hello kitty')
|
||||||
|
])
|
||||||
|
def test_history_getitem(index, exp, hist, xonsh_builtins):
|
||||||
xonsh_builtins.__xonsh_env__['HISTCONTROL'] = set()
|
xonsh_builtins.__xonsh_env__['HISTCONTROL'] = set()
|
||||||
for ts,cmd in enumerate(CMDS): # populate the shell history
|
for ts,cmd in enumerate(CMDS): # populate the shell history
|
||||||
hist.append({'inp': cmd, 'rtn': 0, 'ts':(ts+1, ts+1.5)})
|
hist.append({'inp': cmd, 'rtn': 0, 'ts':(ts+1, ts+1.5)})
|
||||||
|
|
||||||
# indexing
|
assert hist[index] == exp
|
||||||
assert hist[-1] == 'grep from me'
|
|
||||||
assert hist['hello'] == 'cat hello kitty'
|
|
||||||
|
|
||||||
# word parts
|
|
||||||
assert hist[-1, -1] == 'me'
|
|
||||||
assert hist['hello', 1] == 'hello'
|
|
||||||
|
|
|
@ -290,7 +290,7 @@ def _curr_session_parser(hist=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
if hist is None:
|
if hist is None:
|
||||||
hist = builtins.__xonsh_history__
|
hist = builtins.__xonsh_history__
|
||||||
return hist._get()
|
return iter(hist)
|
||||||
|
|
||||||
|
|
||||||
def _zsh_hist_parser(location=None, **kwargs):
|
def _zsh_hist_parser(location=None, **kwargs):
|
||||||
|
@ -486,17 +486,36 @@ def _hist_show(ns, *args, **kwargs):
|
||||||
class History(object):
|
class History(object):
|
||||||
"""Xonsh session history.
|
"""Xonsh session history.
|
||||||
|
|
||||||
History object supports indexing with some rules for extra functionality:
|
Indexing
|
||||||
|
--------
|
||||||
|
History object acts like a sequence that can be indexed in a special way
|
||||||
|
that adds extra functionality. At the moment only history from the
|
||||||
|
current session can be retrieved. Note that the most recent command
|
||||||
|
is the last item in history.
|
||||||
|
|
||||||
- index must be one of string, int or tuple of length two
|
The index acts as a filter with two parts, command and argument,
|
||||||
- if the index is an int the appropriate command in order is returned
|
separated by comma. Based on the type of each part different
|
||||||
- if the index is a string the last command that contains
|
filtering can be achieved,
|
||||||
the string is returned
|
|
||||||
- if the index is a tuple:
|
|
||||||
|
|
||||||
- the first item follows the previous
|
for the command part:
|
||||||
two rules.
|
|
||||||
- the second item is the slice of the arguments to be returned
|
- an int returns the command in that position.
|
||||||
|
- a slice returns a list of commands.
|
||||||
|
- a string returns the most recent command containing the string.
|
||||||
|
|
||||||
|
for the argument part:
|
||||||
|
|
||||||
|
- an int returns the argument of the command in that position.
|
||||||
|
- a slice returns a part of the command based on the argument
|
||||||
|
position.
|
||||||
|
|
||||||
|
The argument part of the filter can be omitted but the command part is
|
||||||
|
required.
|
||||||
|
|
||||||
|
Command arguments are separated by white space.
|
||||||
|
|
||||||
|
If the command filter produces a list then the argument filter
|
||||||
|
will be applied to each element of that list.
|
||||||
|
|
||||||
Attributes
|
Attributes
|
||||||
----------
|
----------
|
||||||
|
@ -615,7 +634,7 @@ class History(object):
|
||||||
self.buffer.clear()
|
self.buffer.clear()
|
||||||
return hf
|
return hf
|
||||||
|
|
||||||
def _get(self):
|
def __iter__(self):
|
||||||
"""Get current session history.
|
"""Get current session history.
|
||||||
|
|
||||||
Yields
|
Yields
|
||||||
|
@ -629,13 +648,15 @@ class History(object):
|
||||||
yield (c, t, ind)
|
yield (c, t, ind)
|
||||||
|
|
||||||
def __getitem__(self, item):
|
def __getitem__(self, item):
|
||||||
|
"""Retrieve history parts based on filtering rules,
|
||||||
|
look at ``History`` docs for more info."""
|
||||||
# accept only one of str, int, tuple of length two
|
# accept only one of str, int, tuple of length two
|
||||||
if isinstance(item, tuple):
|
if isinstance(item, tuple):
|
||||||
pattern, part = item
|
pattern, part = item
|
||||||
else:
|
else:
|
||||||
pattern, part = item, None
|
pattern, part = item, None
|
||||||
# find command
|
# find command
|
||||||
hist = [c for c, *_ in self._get()]
|
hist = [c for c, *_ in self]
|
||||||
command = None
|
command = None
|
||||||
if isinstance(pattern, str):
|
if isinstance(pattern, str):
|
||||||
for command in reversed(hist):
|
for command in reversed(hist):
|
||||||
|
@ -649,10 +670,16 @@ class History(object):
|
||||||
'str, int, tuple of length two')
|
'str, int, tuple of length two')
|
||||||
# get command part
|
# get command part
|
||||||
if command and part:
|
if command and part:
|
||||||
part = ensure_slice(part)
|
s = ensure_slice(part)
|
||||||
command = ' '.join(command.split()[part])
|
print('SLICE:', s)
|
||||||
|
command = ' '.join(command.split()[s])
|
||||||
return command
|
return command
|
||||||
|
|
||||||
|
def __setitem__(self, *args):
|
||||||
|
raise PermissionError('You cannot change history! '
|
||||||
|
'you can create new though.')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _hist_info(ns, hist):
|
def _hist_info(ns, hist):
|
||||||
"""Display information about the shell history."""
|
"""Display information about the shell history."""
|
||||||
|
|
Loading…
Add table
Reference in a new issue