Esempio n. 1
0
def modified() -> str:
    """Modification date of the current image."""
    try:
        mtime = os.path.getmtime(api.current_path())
    except OSError:
        return "N/A"
    date_time = QDateTime.fromSecsSinceEpoch(int(mtime))
    return date_time.toString("yyyy-MM-dd HH:mm")
Esempio n. 2
0
 def _run(self, text, mode, count, reverse, incremental):
     """Implementation of running search."""
     paths = api.pathlist(mode)
     current_index = paths.index(api.current_path(mode))
     basenames = [os.path.basename(path) for path in paths]
     sorted_paths = _sort_for_search(basenames, current_index, reverse)
     next_match, matches = _get_next_match(text, count, sorted_paths)
     index = basenames.index(next_match)
     self.new_search.emit(index, matches, mode, incremental)
     api.status.update("new search")
Esempio n. 3
0
def copy_image(
    primary: bool = False,
    width: int = None,
    height: int = None,
    size: int = None,
    count: int = None,
) -> None:
    """Copy currently selected image to system clipboard.

    **syntax:** ``:copy-image [--primary] [--width=WIDTH] [--height=HEIGHT]
    [--size=SIZE]``

    optional arguments:
        * ``--primary``: Copy to primary selection.
        * ``--width``: Scale width to the specified value.
        * ``--height``: Scale height to the specified value.
        * ``--size``: Scale longer side to the specified value.

    **count:** Equivalent to the ``--size`` option
    """
    clipboard = QGuiApplication.clipboard()
    mode = QClipboard.Selection if primary else QClipboard.Clipboard
    path = api.current_path()

    try:
        reader = imagereader.get_reader(path)
        pixmap = reader.get_pixmap()
    except ValueError as e:
        log.error(str(e))
        return

    if size or count:
        pix_size = pixmap.size()

        size = count if count is not None else size

        if pix_size.height() >= pix_size.width():
            _logger.debug(f"Copy image with size {size} restricting height")
            pixmap = pixmap.scaledToHeight(size)  # type: ignore[arg-type]
        else:
            _logger.debug(f"Copy image with size {size} restricting width")
            pixmap = pixmap.scaledToWidth(size)  # type: ignore[arg-type]

    elif width:
        _logger.debug(f"Copy image with width {width}")
        pixmap = pixmap.scaledToWidth(width)

    elif height:
        _logger.debug(f"Copy image with height {height}")
        pixmap = pixmap.scaledToHeight(height)

    clipboard.setPixmap(pixmap, mode=mode)
Esempio n. 4
0
def copy_name(abspath: bool = False, primary: bool = False) -> None:
    """Copy name of current path to system clipboard.

    **syntax:** ``:copy-name [--abspath] [--primary]``

    optional arguments:
        * ``--abspath``: Copy absolute path instead of basename.
        * ``--primary``: Copy to primary selection.
    """
    clipboard = QGuiApplication.clipboard()
    mode = QClipboard.Selection if primary else QClipboard.Clipboard
    path = api.current_path()
    name = path if abspath else os.path.basename(path)
    clipboard.setText(name, mode=mode)
Esempio n. 5
0
def expand_percent(text: str, mode: api.modes.Mode) -> str:
    """Expand % to the corresponding path and %m to all marked paths.

    Args:
        text: The command in which the wildcards are expanded.
        mode: Mode the command is run in to get correct path(-list).
    """
    # Check first as the re substitutions are rather expensive
    if "%m" in text:
        text = re.sub(r"(?<!\\)%m", " ".join(api.mark.paths), text)
        text = text.replace("\\%m", "%")  # Remove escape characters
    if "%" in text:
        current = shlex.quote(api.current_path(mode))
        text = re.sub(r"(?<!\\)%", current, text)
        text = text.replace("\\%", "%")  # Remove escape characters
    return text
Esempio n. 6
0
def filesize() -> str:
    """Size of the current image in bytes."""
    return files.get_size(api.current_path())