~asimpson/rofi-clipster

26ac63f4f9f6295e3f64ad1a868c093268dd8ca1 — Adam Simpson 2 years ago 9d997f1 master
feat: fetch from github
2 files changed, 7 insertions(+), 71 deletions(-)

D Clippy.py
M default.nix
D Clippy.py => Clippy.py +0 -69
@@ 1,69 0,0 @@
#!/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()

M default.nix => default.nix +7 -2
@@ 3,11 3,16 @@ with import <nixpkgs>{};
pkgs.stdenv.mkDerivation {
  pname = "rofi-clipster";
  version = "latest";
  unpackPhase = "true";
  buildInputs =  [ pkgs.python3 ];
  src = pkgs.fetchFromGitHub {
    owner = "fdw";
    repo = "rofi-clipster";
    rev = "main";
    sha256 = "1k4rw1yxl5vnlzg7j9k3ll0ywcc6jv93s3y2rnr1nnf36ybsihnw";
  };
  installPhase = ''
    mkdir -p $out/bin
    cp ${./Clippy.py} $out/bin/rofi-clipster
    cp clippy/Clippy.py $out/bin/rofi-clipster
    chmod +x $out/bin/rofi-clipster
  '';
}