make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / user-tools / fzf-preview
blob7f342a1c1d5725cc3d1032885012ac3f912e8deb
1 #!/bin/bash
3 inode=$1
5 if [ "$inode" = 1 ]
6 then
7 # it's a mountpoint.
8 filename=$2
9 path="$FZF_PREVIEW_DIRECTORY/$filename"
10 else
11 # note, possibly this `find` does not yield the same file which given to us,
12 # due to hardlinks, but generally it does not alter previewing much.
13 path=`find "$FZF_PREVIEW_DIRECTORY" -maxdepth 1 -inum "$inode" -print -quit`
15 # filename contains the "-> target" part if it's a symlink to "target"
16 #filename=${filename%% -> *}
19 mime=`file -bL --mime-type "$path"`
22 ornate_per_line()
24 local ansi_codes=$1
25 local ansi_reset=`tput sgr0`
26 local line
27 while read -r line
29 echo -n "$ansi_codes$line$ansi_reset"
30 echo
31 done
34 strip_ctrl_chars()
36 sed -e 's/\[\x00-\x1F\x7F\]//g'
39 concat_major_minor()
41 # concat char- and block-special file's major and minor device numbers
42 # to unite their respective columns.
43 # note, we keep all the spaces before the major and minor numbers,
44 # although this is one space more than needed to pad off the previous column,
45 # but other files (not char- or block-special ones) in the directory are
46 # also padded to account the one extra space after the comma between the major
47 # and the minor numbers.
48 sed -e 's/^\([cb]\S\+\s\+\S\+\s\+\)\([0-9]\+\),\(\s\+\)\([0-9]\+\)/\1\3\2:\4/'
51 reorder_ls_columns()
53 # remove permissions and n_links columns and
54 # put date and time before size.
55 sed -e "s/\(\S\+\s\+\S\+\)\(\s\+\S\+\)\s\+\(\S\+\s\+\S\+\)/\3\2/"
59 xattrs=`getfattr --physical --dump --encoding=text "$path" 2>/dev/null`
60 if [ -n "$xattrs" ]
61 then
62 ansi_codes=`tput bold; tput setaf 5`
63 echo "$xattrs" | ornate_per_line "$ansi_codes"
66 acls=`getfacl --physical --omit-header --skip-base --absolute-names "$path" 2>/dev/null`
67 if [ -n "$acls" ]
68 then
69 ansi_codes=`tput bold; tput setaf 3`
70 echo "$acls" | ornate_per_line "$ansi_codes"
74 case "$mime" in
75 application/pdf)
76 pdftotext -layout "$path" -
78 application/*)
79 echo $mime
80 file -bL "$path"
82 image/*)
83 identify "$path"
85 video/*|audio/*)
86 mediainfo "$path"
88 message/rfc822)
89 emlv "$path" | fold -w "$FZF_PREVIEW_COLUMNS" -s
92 if [ -d "$path" ]
93 then
94 ls -lAhgo ${LS_SORT_FLAGS[$LS_SORT_ORDER]} --time-style=long-iso --indicator-style=slash --color=always "$path/" |\
95 lines 2-EOF |\
96 concat_major_minor |\
97 reorder_ls_columns
98 elif [ -c "$path" -o -p "$path" ]
99 then
100 file "$path"
101 else
102 head -n ${FZF_PREVIEW_LINES:--0} "$path" | strip_ctrl_chars
105 esac