@@ -51,168 +51,192 @@ PREVIEW_HEIGHT=10
51
51
52
52
while [ " $# " -gt 0 ]; do
53
53
case " $1 " in
54
- " --path" )
55
- shift
56
- FILE_PATH=" $1 "
57
- ;;
58
- " --preview-width" )
59
- shift
60
- PREVIEW_WIDTH=" $1 "
61
- ;;
62
- " --preview-height" )
63
- shift
64
- PREVIEW_HEIGHT=" $1 "
65
- ;;
54
+ " --path" )
55
+ shift
56
+ FILE_PATH=" $1 "
57
+ ;;
58
+ " --preview-width" )
59
+ shift
60
+ PREVIEW_WIDTH=" $1 "
61
+ ;;
62
+ " --preview-height" )
63
+ shift
64
+ PREVIEW_HEIGHT=" $1 "
65
+ ;;
66
66
esac
67
67
shift
68
68
done
69
69
70
70
handle_extension () {
71
71
case " ${FILE_EXTENSION_LOWER} " in
72
- # # Archive
73
- a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
74
- rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
75
- atool --list -- " ${FILE_PATH} " && exit 0
76
- bsdtar --list --file " ${FILE_PATH} " && exit 0
77
- exit 1 ;;
78
- rar)
79
- # # Avoid password prompt by providing empty password
80
- unrar lt -p- -- " ${FILE_PATH} " && exit 0
81
- exit 1 ;;
82
- 7z)
83
- # # Avoid password prompt by providing empty password
84
- 7z l -p -- " ${FILE_PATH} " && exit 0
85
- exit 1 ;;
86
-
87
- # # PDF
88
- pdf)
89
- # # Preview as text conversion
90
- pdftotext -l 10 -nopgbrk -q -- " ${FILE_PATH} " - | \
91
- fmt -w " ${PREVIEW_WIDTH} " && exit 0
92
- mutool draw -F txt -i -- " ${FILE_PATH} " 1-10 | \
93
- fmt -w " ${PREVIEW_WIDTH} " && exit 0
94
- exiftool " ${FILE_PATH} " && exit 0
95
- exit 1 ;;
96
-
97
- # # BitTorrent
98
- torrent)
99
- transmission-show -- " ${FILE_PATH} " && exit 0
100
- exit 1 ;;
101
-
102
- # # OpenDocument
103
- odt|sxw)
104
- # # Preview as text conversion
105
- odt2txt " ${FILE_PATH} " && exit 0
106
- # # Preview as markdown conversion
107
- pandoc -s -t markdown -- " ${FILE_PATH} " && exit 0
108
- exit 1 ;;
109
- ods|odp)
110
- # # Preview as text conversion (unsupported by pandoc for markdown)
111
- odt2txt " ${FILE_PATH} " && exit 0
112
- exit 1 ;;
113
-
114
- # # XLSX
115
- xlsx)
116
- # # Preview as csv conversion
117
- # # Uses: https://github.com/dilshod/xlsx2csv
118
- xlsx2csv -- " ${FILE_PATH} " && exit 0
119
- exit 1 ;;
120
-
121
- # # HTML
122
- htm|html|xhtml)
123
- # # Preview as text conversion
124
- w3m -dump " ${FILE_PATH} " && exit 0
125
- lynx -dump -- " ${FILE_PATH} " && exit 0
126
- elinks -dump " ${FILE_PATH} " && exit 0
127
- pandoc -s -t markdown -- " ${FILE_PATH} " && exit 0
128
- ;;
129
-
130
- # # JSON
131
- json|ipynb)
132
- jq --color-output . " ${FILE_PATH} " && exit 0
133
- python -m json.tool -- " ${FILE_PATH} " && exit 0
134
- ;;
135
-
136
- # # Direct Stream Digital/Transfer (DSDIFF) and wavpack aren't detected
137
- # # by file(1).
138
- dff|dsf|wv|wvc)
139
- mediainfo " ${FILE_PATH} " && exit 0
140
- exiftool " ${FILE_PATH} " && exit 0
141
- ;; # Continue with next handler on failure
72
+ # # Archive
73
+ a | ace | alz | arc | arj | bz | bz2 | cab | cpio | deb | gz | jar | lha | lz | lzh | lzma | lzo | \
74
+ rpm | rz | t7z | tar | tbz | tbz2 | tgz | tlz | txz | tZ | tzo | war | xpi | xz | Z | zip)
75
+ atool --list -- " ${FILE_PATH} " && exit 0
76
+ bsdtar --list --file " ${FILE_PATH} " && exit 0
77
+ exit 1
78
+ ;;
79
+ rar)
80
+ # # Avoid password prompt by providing empty password
81
+ unrar lt -p- -- " ${FILE_PATH} " && exit 0
82
+ exit 1
83
+ ;;
84
+ 7z)
85
+ # # Avoid password prompt by providing empty password
86
+ 7z l -p -- " ${FILE_PATH} " && exit 0
87
+ exit 1
88
+ ;;
89
+
90
+ # # PDF
91
+ pdf)
92
+ # # Preview as text conversion
93
+ pdftotext -l 10 -nopgbrk -q -- " ${FILE_PATH} " - |
94
+ fmt -w " ${PREVIEW_WIDTH} " && exit 0
95
+ mutool draw -F txt -i -- " ${FILE_PATH} " 1-10 |
96
+ fmt -w " ${PREVIEW_WIDTH} " && exit 0
97
+ exiftool " ${FILE_PATH} " && exit 0
98
+ exit 1
99
+ ;;
100
+
101
+ # # BitTorrent
102
+ torrent)
103
+ transmission-show -- " ${FILE_PATH} " && exit 0
104
+ exit 1
105
+ ;;
106
+
107
+ # # OpenDocument
108
+ odt | sxw)
109
+ # # Preview as text conversion
110
+ odt2txt " ${FILE_PATH} " && exit 0
111
+ # # Preview as markdown conversion
112
+ pandoc -s -t markdown -- " ${FILE_PATH} " && exit 0
113
+ exit 1
114
+ ;;
115
+ ods | odp)
116
+ # # Preview as text conversion (unsupported by pandoc for markdown)
117
+ odt2txt " ${FILE_PATH} " && exit 0
118
+ exit 1
119
+ ;;
120
+
121
+ # # XLSX
122
+ xlsx)
123
+ # # Preview as csv conversion
124
+ # # Uses: https://github.com/dilshod/xlsx2csv
125
+ xlsx2csv -- " ${FILE_PATH} " && exit 0
126
+ exit 1
127
+ ;;
128
+
129
+ # # HTML
130
+ htm | html | xhtml)
131
+ # # Preview as text conversion
132
+ w3m -dump " ${FILE_PATH} " && exit 0
133
+ lynx -dump -- " ${FILE_PATH} " && exit 0
134
+ elinks -dump " ${FILE_PATH} " && exit 0
135
+ pandoc -s -t markdown -- " ${FILE_PATH} " && exit 0
136
+ bat --color=always --paging=never \
137
+ --style=plain \
138
+ --terminal-width=" ${PREVIEW_WIDTH} " \
139
+ " ${FILE_PATH} " && exit 0
140
+ ;;
141
+
142
+ # # JSON
143
+ json | ipynb)
144
+ jq --color-output . " ${FILE_PATH} " && exit 0
145
+ python -m json.tool -- " ${FILE_PATH} " && exit 0
146
+ bat --color=always --paging=never \
147
+ --style=plain \
148
+ --terminal-width=" ${PREVIEW_WIDTH} " \
149
+ " ${FILE_PATH} " && exit 0
150
+ ;;
151
+
152
+ # # Direct Stream Digital/Transfer (DSDIFF) and wavpack aren't detected
153
+ # # by file(1).
154
+ dff | dsf | wv | wvc)
155
+ mediainfo " ${FILE_PATH} " && exit 0
156
+ exiftool " ${FILE_PATH} " && exit 0
157
+ ;; # Continue with next handler on failure
142
158
esac
143
159
}
144
160
145
161
handle_mime () {
146
162
local mimetype=" ${1} "
147
163
148
164
case " ${mimetype} " in
149
- # # RTF and DOC
150
- text/rtf|* msword)
151
- # # Preview as text conversion
152
- # # note: catdoc does not always work for .doc files
153
- # # catdoc: http://www.wagner.pp.ru/~vitus/software/catdoc/
154
- catdoc -- " ${FILE_PATH} " && exit 0
155
- exit 1 ;;
156
-
157
- # # DOCX, ePub, FB2 (using markdown)
158
- # # You might want to remove "|epub" and/or "|fb2" below if you have
159
- # # uncommented other methods to preview those formats
160
- * wordprocessingml.document|* /epub+zip|* /x-fictionbook+xml)
161
- # # Preview as markdown conversion
162
- pandoc -s -t markdown -- " ${FILE_PATH} " | bat -l markdown \
163
- --color=always --paging=never \
164
- --style=plain \
165
- --terminal-width=" ${PREVIEW_WIDTH} " && exit 0
166
- exit 1 ;;
167
-
168
- # # E-mails
169
- message/rfc822)
170
- # # Parsing performed by mu: https://github.com/djcb/mu
171
- mu view -- " ${FILE_PATH} " && exit 0
172
- exit 1 ;;
173
-
174
- # # XLS
175
- * ms-excel)
176
- # # Preview as csv conversion
177
- # # xls2csv comes with catdoc:
178
- # # http://www.wagner.pp.ru/~vitus/software/catdoc/
179
- xls2csv -- " ${FILE_PATH} " && exit 0
180
- exit 1 ;;
181
-
182
- # # Text
183
- text/* | * /xml)
184
- bat --color=always --paging=never \
185
- --style=plain \
186
- --terminal-width=" ${PREVIEW_WIDTH} " \
187
- " ${FILE_PATH} " && exit 0
188
- cat " ${FILE_PATH} " && exit 0
189
- exit 1 ;;
190
-
191
- # # DjVu
192
- image/vnd.djvu)
193
- # # Preview as text conversion (requires djvulibre)
194
- djvutxt " ${FILE_PATH} " | fmt -w " ${PREVIEW_WIDTH} " && exit 0
195
- exiftool " ${FILE_PATH} " && exit 0
196
- exit 1 ;;
197
-
198
- # # Image
199
- image/* )
200
- # # Preview as text conversion
201
- exiftool " ${FILE_PATH} " && exit 0
202
- exit 1 ;;
203
-
204
- # # Video and audio
205
- video/* | audio/* )
206
- mediainfo " ${FILE_PATH} " && exit 0
207
- exiftool " ${FILE_PATH} " && exit 0
208
- exit 1 ;;
165
+ # # RTF and DOC
166
+ text/rtf | * msword)
167
+ # # Preview as text conversion
168
+ # # note: catdoc does not always work for .doc files
169
+ # # catdoc: http://www.wagner.pp.ru/~vitus/software/catdoc/
170
+ catdoc -- " ${FILE_PATH} " && exit 0
171
+ exit 1
172
+ ;;
173
+
174
+ # # DOCX, ePub, FB2 (using markdown)
175
+ # # You might want to remove "|epub" and/or "|fb2" below if you have
176
+ # # uncommented other methods to preview those formats
177
+ * wordprocessingml.document | * /epub+zip | * /x-fictionbook+xml)
178
+ # # Preview as markdown conversion
179
+ pandoc -s -t markdown -- " ${FILE_PATH} " | bat -l markdown \
180
+ --color=always --paging=never \
181
+ --style=plain \
182
+ --terminal-width=" ${PREVIEW_WIDTH} " && exit 0
183
+ exit 1
184
+ ;;
185
+
186
+ # # E-mails
187
+ message/rfc822)
188
+ # # Parsing performed by mu: https://github.com/djcb/mu
189
+ mu view -- " ${FILE_PATH} " && exit 0
190
+ exit 1
191
+ ;;
192
+
193
+ # # XLS
194
+ * ms-excel)
195
+ # # Preview as csv conversion
196
+ # # xls2csv comes with catdoc:
197
+ # # http://www.wagner.pp.ru/~vitus/software/catdoc/
198
+ xls2csv -- " ${FILE_PATH} " && exit 0
199
+ exit 1
200
+ ;;
201
+
202
+ # # Text
203
+ text/* | * /xml)
204
+ bat --color=always --paging=never \
205
+ --style=plain \
206
+ --terminal-width=" ${PREVIEW_WIDTH} " \
207
+ " ${FILE_PATH} " && exit 0
208
+ cat " ${FILE_PATH} " && exit 0
209
+ exit 1
210
+ ;;
211
+
212
+ # # DjVu
213
+ image/vnd.djvu)
214
+ # # Preview as text conversion (requires djvulibre)
215
+ djvutxt " ${FILE_PATH} " | fmt -w " ${PREVIEW_WIDTH} " && exit 0
216
+ exiftool " ${FILE_PATH} " && exit 0
217
+ exit 1
218
+ ;;
219
+
220
+ # # Image
221
+ image/* )
222
+ # # Preview as text conversion
223
+ exiftool " ${FILE_PATH} " && exit 0
224
+ exit 1
225
+ ;;
226
+
227
+ # # Video and audio
228
+ video/* | audio/* )
229
+ mediainfo " ${FILE_PATH} " && exit 0
230
+ exiftool " ${FILE_PATH} " && exit 0
231
+ exit 1
232
+ ;;
209
233
esac
210
234
}
211
235
212
236
FILE_EXTENSION=" ${FILE_PATH##* .} "
213
237
FILE_EXTENSION_LOWER=" $( printf " %s" " ${FILE_EXTENSION} " | tr ' [:upper:]' ' [:lower:]' ) "
214
238
handle_extension
215
- MIMETYPE=" $( file --dereference --brief --mime-type -- " ${FILE_PATH} " ) "
239
+ MIMETYPE=" $( file --dereference --brief --mime-type -- " ${FILE_PATH} " ) "
216
240
handle_mime " ${MIMETYPE} "
217
241
218
242
exit 1
0 commit comments