summaryrefslogtreecommitdiff
path: root/pkgs/scripts/clip.nix
blob: 862d82e6350d85dfba3337aff2b1bdad0f34bcd8 (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
{ 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"
''