~asimpson/rofi-clipster

9d997f1b7136249ec01730a04383cc5368d2c779 — Adam Simpson 2 years ago
feat: first stab at it
2 files changed, 82 insertions(+), 0 deletions(-)

A Clippy.py
A default.nix
A  => Clippy.py +69 -0
@@ 1,69 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from subprocess import run
from typing import Tuple, Dict


class Clippy:

    def __init__(self) -> None:
        self.entries = self.get_entries_from_clipster()

        return_code, stdout = self.open_main_rofi_window()

        actual_entry = self.fetch_actual_entry(stdout[:-1])

        if return_code == 1:
            sys.exit()
        else:
            self.save_to_clipboard(actual_entry)

    def open_main_rofi_window(self) -> Tuple[int, str]:
        rofi = run(
            [
                'rofi',
                '-dmenu',
                '-i',
                '-sep',
                'nul'
            ],
            input='\n'.join(self.entries.keys()),
            capture_output=True,
            encoding='utf-8'
        )

        return rofi.returncode, rofi.stdout

    def get_entries_from_clipster(self) -> Dict[str, str]:
        lines = run(
            [
                'clipster',
                '-n',
                '0',
                '-o',
                '-0'
            ],
            capture_output=True,
            encoding='utf-8'
        ).stdout.split('\0')

        return {line.replace('\n', ' ')[0:250]: line for line in lines}

    def fetch_actual_entry(self, view: str) -> str:
        return self.entries[view]

    def save_to_clipboard(self, chosen_text: str):
        run(
            ['clipster'],
            input=chosen_text,
            encoding='utf=8'
        )


def main():
    Clippy()


if __name__ == "__main__":
    main()

A  => default.nix +13 -0
@@ 1,13 @@
with import <nixpkgs>{};

pkgs.stdenv.mkDerivation {
  pname = "rofi-clipster";
  version = "latest";
  unpackPhase = "true";
  buildInputs =  [ pkgs.python3 ];
  installPhase = ''
    mkdir -p $out/bin
    cp ${./Clippy.py} $out/bin/rofi-clipster
    chmod +x $out/bin/rofi-clipster
  '';
}