Revision ddc97a10 kamaki/cli/commands/history.py

b/kamaki/cli/commands/history.py
34 34
# or implied, of GRNET S.A.
35 35

  
36 36
from kamaki.cli.command_tree import CommandTree
37
from kamaki.cli.argument import IntArgument, ValueArgument
37
from kamaki.cli.argument import ValueArgument
38 38
from kamaki.cli.argument import ArgumentParseManager
39 39
from kamaki.cli.history import History
40 40
from kamaki.cli import command
......
83 83

  
84 84
@command(history_cmds)
85 85
class history_show(_init_history):
86
    """Show intersession command history
87
    ---
88
    - With no parameters : pick all commands in history records
89
    - With:
90
    .   1.  <order-id> : pick the <order-id>th command
91
    .   2.  <order-id-1>-<order-id-2> : pick all commands ordered in the range
92
    .       [<order-id-1> - <order-id-2>]
93
    .   - the above can be mixed and repeated freely, separated by spaces
94
    .       e.g., pick 2 4-7 -3
95
    .   - Use negative integers to count from the end of the list, e.g.,:
96
    .       -2 means : the command before the last one
97
    .       -2-5 means : last 2 commands + the first 5
98
    .       -5--2 means : the last 5 commands except the last 2
86
    """Show history
87
        Featutes:
88
        - slice notation (cmd numbers --> N or :N or N: or N1:N2)
89
        - text matching (--match)
99 90
    """
100 91

  
101 92
    arguments = dict(
102
        limit=IntArgument(
103
            'number of lines to show',
104
            ('-n', '--numner'),
105
            default=0),
106
        match=ValueArgument('show lines that match given terms', '--match')
93
        match=ValueArgument('Show lines matching this', '--match'),
107 94
    )
108 95

  
109 96
    @errors.generic.all
110
    def _run(self, *cmd_ids):
111
        ret = self.history.get(match_terms=self['match'], limit=self['limit'])
112

  
113
        if not cmd_ids:
114
            self.print_list(ret)
115
            return
116

  
117
        num_list = []
118
        for num_str in cmd_ids:
119
            num_list += _get_num_list(num_str)
120

  
121
        for cmd_id in num_list:
122
            try:
123
                cur_id = int(cmd_id)
124
                if cur_id:
125
                    self.writeln(ret[cur_id - (1 if cur_id > 0 else 0)][:-1])
126
            except IndexError as e2:
127
                raiseCLIError(e2, 'Command id out of 1-%s range' % len(ret))
128

  
129
    def main(self, *cmd_ids):
97
    def _run(self, cmd_slice):
98
        lines = ['%s.\t%s' % (i, l) for i, l in enumerate(self.history[:])][
99
            cmd_slice]
100
        if not isinstance(cmd_slice, slice):
101
            lines = [lines, ]
102
        if self['match']:
103
            lines = [l for l in lines if self._match(l, self['match'])]
104
        self.print_items([l[:-1] for l in lines])
105

  
106
    def main(self, cmd_numbers=''):
130 107
        super(self.__class__, self)._run()
131
        self._run(*cmd_ids)
108
        sl_args = [
109
            int(x) if x else None for x in cmd_numbers.split(':')] if (
110
                cmd_numbers) else [None, None]
111
        slice_cmds = slice(*sl_args) if len(sl_args) > 1 else sl_args[0]
112
        self._run(slice_cmds)
132 113

  
133 114

  
134 115
@command(history_cmds)

Also available in: Unified diff