diff options
author | Florian Bruhin <me@the-compiler.org> | 2021-02-10 17:50:02 +0100 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2021-02-10 17:50:02 +0100 |
commit | 57651f8b10cd37b7ad9c71904b17db9057123c15 (patch) | |
tree | c7b2b19339df0c19d9e1f13796ccd35e25de32a5 /qutebrowser/components | |
parent | af667b8887b0312e725d4e31c7df357bdf5c6df3 (diff) | |
download | qutebrowser-57651f8b10cd37b7ad9c71904b17db9057123c15.tar.gz qutebrowser-57651f8b10cd37b7ad9c71904b17db9057123c15.zip |
Add :screenshot command
See #1841 and #2146
Diffstat (limited to 'qutebrowser/components')
-rw-r--r-- | qutebrowser/components/misccommands.py | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index b74998ae1..0ead38799 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -23,6 +23,7 @@ import os import signal import functools import logging +import pathlib from typing import Optional try: @@ -30,13 +31,14 @@ try: except ImportError: hunter = None -from PyQt5.QtCore import Qt +from PyQt5.QtCore import Qt, QRect from PyQt5.QtPrintSupport import QPrintPreviewDialog from qutebrowser.api import cmdutils, apitypes, message, config # FIXME should be part of qutebrowser.api? from qutebrowser.completion.models import miscmodels +from qutebrowser.utils import utils @cmdutils.register(name='reload') @@ -155,6 +157,37 @@ def debug_dump_page(tab: apitypes.Tab, dest: str, plain: bool = False) -> None: tab.dump_async(callback, plain=plain) +@cmdutils.register() +@cmdutils.argument('tab', value=cmdutils.Value.cur_tab) +def screenshot( + tab: apitypes.Tab, + filename: pathlib.Path, + *, + rect: str = None, + force: bool = False, +) -> None: + """Take a screenshot of the currently shown part of the page. + + The file format is automatically determined based on the given file extension. + + Args: + filename: The file to save the screenshot to (~ gets expanded). + rect: The rectangle to save, as a string like WxH+X+Y. + force: Overwrite existing files. + """ + expanded = filename.expanduser() + if expanded.exists() and not force: + raise cmdutils.CommandError( + f"File {filename} already exists (use --force to overwrite)") + + qrect = QRect() if rect is None else utils.parse_rect(rect) + + pic = tab.grab_pixmap(qrect) + ok = pic.save(str(expanded)) + if not ok: + raise cmdutils.CommandError(f"Saving to {filename} failed") + + @cmdutils.register(maxsplit=0) @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) def insert_text(tab: apitypes.Tab, text: str) -> None: |