92 lines
1.6 KiB
Odin
92 lines
1.6 KiB
Odin
package findr
|
|
|
|
import "core:bufio"
|
|
import "core:fmt"
|
|
import "core:os"
|
|
import "core:strings"
|
|
|
|
main :: proc() {
|
|
prof_init()
|
|
defer prof_destroy()
|
|
|
|
args := os.args
|
|
|
|
opts: WalkOptions
|
|
opts.include_hidden = false
|
|
opts.ignore_mode = .Respected
|
|
|
|
excludes := make([dynamic]string)
|
|
defer delete(excludes)
|
|
|
|
pattern := ""
|
|
paths := make([dynamic]string)
|
|
defer delete(paths)
|
|
|
|
i := 1
|
|
for i < len(args) {
|
|
arg := args[i]
|
|
switch {
|
|
case arg == "--ignored":
|
|
opts.ignore_mode = .Ignored
|
|
case arg == "-E":
|
|
i += 1
|
|
if i < len(args) {
|
|
append(&excludes, args[i])
|
|
}
|
|
case strings.has_prefix(arg, "-E"):
|
|
append(&excludes, arg[2:])
|
|
case len(arg) > 1 && arg[0] == '-':
|
|
for c, j in arg[1:] {
|
|
switch c {
|
|
case 'H':
|
|
opts.include_hidden = true
|
|
case 'I':
|
|
opts.ignore_mode = .All
|
|
case 'a':
|
|
// no-op: accepted for fd compatibility
|
|
}
|
|
}
|
|
case:
|
|
if pattern == "" {
|
|
pattern = arg
|
|
} else {
|
|
append(&paths, arg)
|
|
}
|
|
}
|
|
i += 1
|
|
}
|
|
|
|
if len(paths) == 0 && pattern != "" && os.exists(pattern) {
|
|
append(&paths, pattern)
|
|
pattern = ""
|
|
}
|
|
|
|
opts.pattern = pattern
|
|
if len(excludes) > 0 {
|
|
opts.excludes = excludes[:]
|
|
}
|
|
|
|
if len(paths) == 0 {
|
|
append(&paths, ".")
|
|
}
|
|
|
|
results := make([dynamic]string)
|
|
defer {
|
|
for r in results {delete(r)}
|
|
delete(results)
|
|
}
|
|
|
|
thread_count := os.get_processor_core_count()
|
|
walk(paths[:], &results, opts, thread_count)
|
|
|
|
w: bufio.Writer
|
|
bufio.writer_init(&w, os.to_stream(os.stdout), 1 << 13)
|
|
defer bufio.writer_destroy(&w)
|
|
|
|
for r in results {
|
|
bufio.writer_write_string(&w, r)
|
|
bufio.writer_write_byte(&w, '\n')
|
|
}
|
|
bufio.writer_flush(&w)
|
|
}
|