nix/bin/xurls.nix (view raw)
1{ pkgs, ... }:
2
3let
4 name = "xurls";
5 fzy = "${pkgs.fzy}/bin/fzy";
6 copy = "${pkgs.wl-clipboard}/bin/wl-copy";
7in
8pkgs.writeShellScriptBin name
9 ''
10 content="$(tmux capture-pane -J -p)"
11
12 mapfile -t urls < <(echo "$content" | grep -oE '(https?|ftp|file):/?//[-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]')
13 mapfile -t wwws < <(echo "$content" | grep -oE '(http?s://)?www\.[a-zA-Z](-?[a-zA-Z0-9])+\.[a-zA-Z]{2,}(/\S+)*' | grep -vE '^https?://' |sed 's/^\(.*\)$/http:\/\/\1/')
14 mapfile -t ips < <(echo "$content" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(:[0-9]{1,5})?(/\S+)*' |sed 's/^\(.*\)$/http:\/\/\1/')
15 mapfile -t gits < <(echo "$content" | grep -oE '(ssh://)?git@\S*' | sed 's/:/\//g' | sed 's/^\(ssh\/\/\/\)\{0,1\}git@\(.*\)$/https:\/\/\2/')
16
17 items="$(printf '%s\n' "''${urls[@]}" "''${wwws[@]}" "''${ips[@]}" "''${gits[@]}" |
18 grep -v '^$' |
19 sort -u |
20 nl -w3 -s ' '
21 )"
22
23 [ -z "$items" ] && exit
24
25 u="$(${fzy} <<< "$items" | awk '{ print $2 }')"
26 ${copy} "$u"
27 ''
28