summaryrefslogtreecommitdiff
path: root/qutebrowser/completion/models/miscmodels.py
blob: d8ebafb297909e6824099dd7fe39e818f85d1302 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:

# Copyright 2014-2021 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# 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/>.

"""Functions that return miscellaneous completion models."""

import datetime
import itertools
from typing import List, Sequence, Tuple

from qutebrowser.config import config, configdata
from qutebrowser.utils import objreg, log, utils
from qutebrowser.completion.models import completionmodel, listcategory, util
from qutebrowser.browser import inspector


def command(*, info):
    """A CompletionModel filled with non-hidden commands and descriptions."""
    model = completionmodel.CompletionModel(column_widths=(20, 60, 20))
    cmdlist = util.get_cmd_completions(info, include_aliases=True,
                                       include_hidden=False)
    model.add_category(listcategory.ListCategory("Commands", cmdlist))
    return model


def helptopic(*, info):
    """A CompletionModel filled with help topics."""
    model = completionmodel.CompletionModel(column_widths=(20, 70, 10))

    cmdlist = util.get_cmd_completions(info, include_aliases=False,
                                       include_hidden=True, prefix=':')
    settings = ((opt.name, opt.description, info.config.get_str(opt.name))
                for opt in configdata.DATA.values())

    model.add_category(listcategory.ListCategory("Commands", cmdlist))
    model.add_category(listcategory.ListCategory("Settings", settings))
    return model


def quickmark(*, info=None):
    """A CompletionModel filled with all quickmarks."""
    def delete(data: Sequence[str]) -> None:
        """Delete a quickmark from the completion menu."""
        name = data[0]
        quickmark_manager = objreg.get('quickmark-manager')
        log.completion.debug('Deleting quickmark {}'.format(name))
        quickmark_manager.delete(name)

    utils.unused(info)
    model = completionmodel.CompletionModel(column_widths=(30, 70, 0))
    marks = objreg.get('quickmark-manager').marks.items()
    model.add_category(listcategory.ListCategory('Quickmarks', marks,
                                                 delete_func=delete,
                                                 sort=False))
    return model


def bookmark(*, info=None):
    """A CompletionModel filled with all bookmarks."""
    def delete(data: Sequence[str]) -> None:
        """Delete a bookmark from the completion menu."""
        urlstr = data[0]
        log.completion.debug('Deleting bookmark {}'.format(urlstr))
        bookmark_manager = objreg.get('bookmark-manager')
        bookmark_manager.delete(urlstr)

    utils.unused(info)
    model = completionmodel.CompletionModel(column_widths=(30, 70, 0))
    marks = objreg.get('bookmark-manager').marks.items()
    model.add_category(listcategory.ListCategory('Bookmarks', marks,
                                                 delete_func=delete,
                                                 sort=False))
    return model


def session(*, info=None):
    """A CompletionModel filled with session names."""
    from qutebrowser.misc import sessions
    utils.unused(info)
    model = completionmodel.CompletionModel()
    try:
        sess = ((name,) for name
                in sessions.session_manager.list_sessions()
                if not name.startswith('_'))
        model.add_category(listcategory.ListCategory("Sessions", sess))
    except OSError:
        log.completion.exception("Failed to list sessions!")
    return model


def _tabs(*, win_id_filter=lambda _win_id: True, add_win_id=True):
    """Helper to get the completion model for tabs/other_tabs.

    Args:
        win_id_filter: A filter function for window IDs to include.
                       Should return True for all included windows.
        add_win_id: Whether to add the window ID to the completion items.
    """
    def delete_tab(data):
        """Close the selected tab."""
        win_id, tab_index = data[0].split('/')
        tabbed_browser = objreg.get('tabbed-browser', scope='window',
                                    window=int(win_id))
        tabbed_browser.on_tab_close_requested(int(tab_index) - 1)

    model = completionmodel.CompletionModel(column_widths=(6, 40, 46, 8))

    tabs_are_windows = config.val.tabs.tabs_are_windows
    # list storing all single-tabbed windows when tabs_are_windows
    windows: List[Tuple[str, str, str, str]] = []

    for win_id in objreg.window_registry:
        if not win_id_filter(win_id):
            continue

        tabbed_browser = objreg.get('tabbed-browser', scope='window',
                                    window=win_id)
        if tabbed_browser.is_shutting_down:
            continue
        tab_entries: List[Tuple[str, str, str, str]] = []
        for idx in range(tabbed_browser.widget.count()):
            tab = tabbed_browser.widget.widget(idx)
            tab_str = ("{}/{}".format(win_id, idx + 1) if add_win_id
                       else str(idx + 1))

            pid = tab.renderer_process_pid()

            tab_entries.append((
                tab_str,
                tab.url().toDisplayString(),
                tabbed_browser.widget.page_title(idx),
                "" if pid is None else f"PID {pid}",
            ))

        if tabs_are_windows:
            windows += tab_entries
        else:
            title = str(win_id) if add_win_id else "Tabs"
            cat = listcategory.ListCategory(
                title, tab_entries, delete_func=delete_tab, sort=False)
            model.add_category(cat)

    if tabs_are_windows:
        win = listcategory.ListCategory(
            "Windows", windows, delete_func=delete_tab, sort=False)
        model.add_category(win)

    return model


def tabs(*, info=None):
    """A model to complete on open tabs across all windows.

    Used for the tab-select command (and others).
    """
    utils.unused(info)
    return _tabs()


def other_tabs(*, info):
    """A model to complete on open tabs across all windows except the current.

    Used for the tab-take command.
    """
    return _tabs(win_id_filter=lambda win_id: win_id != info.win_id)


def tab_focus(*, info):
    """A model to complete on open tabs in the current window."""
    model = _tabs(win_id_filter=lambda win_id: win_id == info.win_id,
                  add_win_id=False)

    special = [
        ("last", "Focus the last-focused tab"),
        ("stack-next", "Go forward through a stack of focused tabs"),
        ("stack-prev", "Go backward through a stack of focused tabs"),
    ]
    model.add_category(listcategory.ListCategory("Special", special))

    return model


def window(*, info):
    """A model to complete on all open windows."""
    model = completionmodel.CompletionModel(column_widths=(6, 30, 64))

    windows = []

    for win_id in objreg.window_registry:
        if win_id == info.win_id:
            continue
        tabbed_browser = objreg.get('tabbed-browser', scope='window',
                                    window=win_id)
        tab_titles = (tab.title() for tab in tabbed_browser.widgets())
        windows.append(("{}".format(win_id),
                        objreg.window_registry[win_id].windowTitle(),
                        ", ".join(tab_titles)))

    model.add_category(listcategory.ListCategory("Windows", windows))

    return model


def inspector_position(*, info):
    """A model for possible inspector positions."""
    utils.unused(info)
    model = completionmodel.CompletionModel(column_widths=(100, 0, 0))
    positions = [(e.name,) for e in inspector.Position]
    category = listcategory.ListCategory("Position (optional)", positions)
    model.add_category(category)
    return model


def _qdatetime_to_completion_format(qdate):
    if not qdate.isValid():
        ts = 0
    else:
        ts = max(qdate.toMSecsSinceEpoch(), 0)
    pydate = datetime.datetime.fromtimestamp(ts / 1000)
    return pydate.strftime(config.val.completion.timestamp_format)


def _back_forward(info, go_forward):
    history = info.cur_tab.history
    current_idx = history.current_idx()
    model = completionmodel.CompletionModel(column_widths=(5, 36, 50, 9))

    if go_forward:
        start = current_idx + 1
        items = history.forward_items()
    else:
        start = 0
        items = history.back_items()

    entries = [
        (
            str(idx),
            entry.url().toDisplayString(),
            entry.title(),
            _qdatetime_to_completion_format(entry.lastVisited())
        )
        for idx, entry in enumerate(items, start)
    ]
    if not go_forward:
        # make sure the most recent is at the top for :back
        entries.reverse()

    cat = listcategory.ListCategory("History", entries, sort=False)
    model.add_category(cat)
    return model


def forward(*, info):
    """A model to complete on history of the current tab.

    Used for the :forward command.
    """
    return _back_forward(info, go_forward=True)


def back(*, info):
    """A model to complete on history of the current tab.

    Used for the :back command.
    """
    return _back_forward(info, go_forward=False)


def undo(*, info):
    """A model to complete undo entries."""
    tabbed_browser = objreg.get('tabbed-browser', scope='window',
                                window=info.win_id)
    model = completionmodel.CompletionModel(column_widths=(6, 84, 10))
    timestamp_format = config.val.completion.timestamp_format

    entries = [
        (
            str(idx),
            ', '.join(entry.url.toDisplayString() for entry in group),
            group[-1].created_at.strftime(timestamp_format)
        )
        for idx, group in
        enumerate(reversed(tabbed_browser.undo_stack), start=1)
    ]

    cat = listcategory.ListCategory("Closed tabs", entries, sort=False)
    model.add_category(cat)
    return model


def process(*, info):
    """A CompletionModel filled with processes."""
    utils.unused(info)
    from qutebrowser.misc import guiprocess
    model = completionmodel.CompletionModel(column_widths=(10, 10, 80))
    for what, processes in itertools.groupby(
            (p for p in guiprocess.all_processes.values() if p is not None),
            lambda proc: proc.what):

        # put successful processes last
        sorted_processes = sorted(
            processes,
            key=lambda proc: proc.outcome.state_str() == 'successful',
        )

        entries = [(str(proc.pid), proc.outcome.state_str(), str(proc))
                   for proc in sorted_processes]
        cat = listcategory.ListCategory(what.capitalize(), entries, sort=False)
        model.add_category(cat)
    return model