summaryrefslogtreecommitdiff
path: root/pkgs/scripts/clip.nix
blob: 501bb3b21fc7175e74a7f5f249ce693a477bd8f0 (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
{ lib
, writeShellScriptBin
, gnome
, 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.gnome-screenshot}/bin/gnome-screenshot "$OPTIONS" -f "$CLIP"
  ${copyOut}

  rm "$CLIP"
''