-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlsext
More file actions
executable file
·36 lines (32 loc) · 1.69 KB
/
lsext
File metadata and controls
executable file
·36 lines (32 loc) · 1.69 KB
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
#!/usr/bin/env racket
#lang racket/base
(require racket/cmdline
(only-in racket/file fold-files)
(only-in racket/path path-get-extension)
racket/set)
(define recursive #f)
(define dir (current-directory))
(define follow-links? #f)
(define exts
(command-line
#:usage-help "if you provide no extensions, then lsext will list all extensions in the directory; if you give extensions, then it'll list all files matching any of the provided extensions. DO NOT PREFIX THE EXTENSION WITH A PERIOD."
#:once-each [("-r" "--recursive") "recurse through directory" (set! recursive #t)]
[("-d" "--dir") d "set directory to a value other than current working directory" (set! dir (string->path d))]
[("--follow-links") "" (set! follow-links? #t)]
#:args extensions (list->set extensions)))
(unless (directory-exists? dir)
(eprintf "directory ~a does not exist~n" dir)
(exit 1))
(let ([s (fold-files (λ (path type s)
(let* ([fext (path-get-extension path)]
[fext-str (and fext (string-downcase (bytes->string/utf-8 (subbytes fext 1))))])
(values (if (and fext-str (eq? type 'file))
(cond [(set-empty? exts) (set-add s fext-str)]
[(and fext-str (set-member? exts fext-str)) (set-add s (path->string path))]
[else s])
s)
(and (member 'read (file-or-directory-permissions path)) (or recursive (eq? dir path))))))
(set)
dir
follow-links?)])
(for-each displayln (sort (set->list s) string<=?)))