summaryrefslogtreecommitdiff
path: root/pkgs/scripts/clip.nix
blob: 3e16e3ecdbdc64d01f4d478a5349f8ce95edc410 (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
{
  lib,
  writeShellScriptBin,
  gnome-screenshot,
  xclip,
  file,
  imagemagick,
  toPDF,
  ...
}:
with lib; let
  name =
    if toPDF
    then "clip-pdf"
    else "clip";

  pdfCmdline = optionalString toPDF ''
    if [ $# -lt 1 ]; then
      echo "Usage: $0 <out.pdf> [opts...]" >&2
      exit 1
    fi

    OUT="$1"
    shift
  '';

  copyOut =
    if toPDF
    then ''
      ${imagemagick}/bin/convert "$CLIP" "$OUT"
    ''
    else ''
      ${xclip}/bin/xclip -selection clipboard \
        -t $(${file}/bin/file -b --mime-type $CLIP) <"$CLIP"
    '';
in
  writeShellScriptBin name ''
    ${pdfCmdline}

    OPTIONS=-a
    if [ "x$@" != "x" ]; then
      OPTIONS="$@"
    fi

    CLIP="$HOME/vtmp/$$.png"
    ${gnome-screenshot}/bin/gnome-screenshot "$OPTIONS" -f "$CLIP"
    ${copyOut}

    rm "$CLIP"
  ''