summaryrefslogtreecommitdiff
path: root/qutebrowser/completion/models/util.py
blob: 224004d1baaeb82b94dca17dcd039cff0e4ad4f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:

# Copyright 2017-2021 Ryan Roden-Corrent (rcorre) <ryan@rcorre.net>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser.  If not, see <https://www.gnu.org/licenses/>.

"""Utility functions for completion models."""

from typing import Callable, Sequence

from qutebrowser.utils import usertypes
from qutebrowser.misc import objects


DeleteFuncType = Callable[[Sequence[str]], None]


def get_cmd_completions(info, include_hidden, include_aliases, prefix=''):
    """Get a list of completions info for commands, sorted by name.

    Args:
        info: The CompletionInfo.
        include_hidden: Include commands which are not in normal mode.
        include_aliases: True to include command aliases.
        prefix: String to append to the command name.

    Return: A list of tuples of form (name, description, bindings).
    """
    assert objects.commands
    cmdlist = []
    cmd_to_keys = info.keyconf.get_reverse_bindings_for('normal')
    for obj in set(objects.commands.values()):
        hide_debug = obj.debug and not objects.args.debug
        hide_mode = (usertypes.KeyMode.normal not in obj.modes and
                     not include_hidden)
        if not (hide_debug or hide_mode or obj.deprecated):
            bindings = ', '.join(cmd_to_keys.get(obj.name, []))
            cmdlist.append((prefix + obj.name, obj.desc, bindings))

    if include_aliases:
        for name, cmd in info.config.get('aliases').items():
            bindings = ', '.join(cmd_to_keys.get(name, []))
            cmdlist.append((name, "Alias for '{}'".format(cmd), bindings))

    return sorted(cmdlist)