tools: cleanup vwhere (#21350)

This commit is contained in:
Turiiya 2024-04-25 13:14:45 +02:00 committed by GitHub
parent 32f7ec8cea
commit 903a585e9e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 80 additions and 117 deletions

View file

@ -1,7 +1,7 @@
module main
import os
import term
import term { blue, bright_cyan, bright_green, bright_magenta, bright_red, bright_yellow, colorize }
import regex
import os.cmdline
@ -19,41 +19,45 @@ mut:
}
fn (mut fdr Finder) configure_from_arguments(args []string) {
match args.len {
1 {
fdr.name = args[0]
}
else {
fdr.symbol.set_from_str(args[0])
if fdr.symbol == .method && !args[1].contains('.') {
make_and_print_error('method require a special notation:', [
'Receiver.method',
], '${args[1]}')
} else if fdr.symbol == .method {
temp_args := args[1].split('.')
fdr.receiver = temp_args[0]
fdr.name = temp_args[1]
} else {
fdr.name = args[1]
}
if fdr.name.contains('-') {
make_and_print_error('It seems you forgot positional arg name:', [], fdr.name)
}
fdr.visib.set_from_str(cmdline.option(args, '-vis', '${Visibility.all}'))
if fdr.symbol == .var && fdr.visib != .all {
make_and_print_error('-vis ${fdr.visib} just can be set with symbol_type:',
['fn', 'method', 'const', 'struct', 'enum', 'interface', 'regexp'],
'${fdr.symbol}')
}
fdr.mutab.set_from_str(cmdline.option(args, '-mut', '${Mutability.any}'))
if fdr.symbol != .var && fdr.mutab != .any {
make_and_print_error('-mut ${fdr.mutab} just can be set with symbol_type:',
['var'], '${fdr.symbol}')
}
fdr.modul = cmdline.option(args, '-mod', '')
fdr.dirs = cmdline.options(args, '-dir')
}
if args.len == 1 {
fdr.name = args[0]
return
}
fdr.symbol.set_from_str(args[0])
if fdr.symbol == .method && !args[1].contains('.') {
make_and_print_error('method require a special notation:', [
'Receiver.method',
], '${args[1]}')
} else if fdr.symbol == .method {
temp_args := args[1].split('.')
fdr.receiver = temp_args[0]
fdr.name = temp_args[1]
} else {
fdr.name = args[1]
}
if fdr.name.contains('-') {
make_and_print_error('It seems you forgot positional arg name:', [], fdr.name)
}
fdr.visib.set_from_str(cmdline.option(args, '-vis', '${Visibility.all}'))
if fdr.symbol == .var && fdr.visib != .all {
make_and_print_error('-vis ${fdr.visib} just can be set with symbol_type:', [
'fn',
'method',
'const',
'struct',
'enum',
'interface',
'regexp',
], '${fdr.symbol}')
}
fdr.mutab.set_from_str(cmdline.option(args, '-mut', '${Mutability.any}'))
if fdr.symbol != .var && fdr.mutab != .any {
make_and_print_error('-mut ${fdr.mutab} just can be set with symbol_type:', [
'var',
], '${fdr.symbol}')
}
fdr.modul = cmdline.option(args, '-mod', '')
fdr.dirs = cmdline.options(args, '-dir')
}
fn (mut fdr Finder) search_for_matches() {
@ -78,16 +82,13 @@ fn (mut fdr Finder) search_for_matches() {
panic(err)} }
paths_to_search << fdr.dirs.map(resolve_module(it) or { panic(err) })
}
// for p in paths_to_search {
// println(p)
// }
// dump(paths_to_search)
mut files_to_search := []string{}
for path in paths_to_search {
files_to_search << collect_v_files(path, recursive) or { panic(err) }
for p in paths_to_search {
files_to_search << collect_v_files(p, recursive) or { panic(err) }
}
// for f in files_to_search {
// println(f)
// }
// dump(files_to_search)
// Auxiliary rgx
sp := r'\s*'
@ -104,26 +105,14 @@ fn (mut fdr Finder) search_for_matches() {
na := '${fdr.name}'
query := match fdr.symbol {
.@fn {
'.*${sy}${sp}${na}${sp}${op}.*${cp}.*'
}
.method {
'.*fn${st}${na}${sp}${op}.*${cp}.*'
}
.var {
'.*${na}${sp}:=.*'
}
.@const {
'.*${na}${sp} = .*'
}
.regexp {
'${na}'
}
else {
'.*${sy}${sp}${na}${sp}.*' // for struct, enum and interface
}
.@fn { '.*${sy}${sp}${na}${sp}${op}.*${cp}.*' }
.method { '.*fn${st}${na}${sp}${op}.*${cp}.*' }
.var { '.*${na}${sp}:=.*' }
.@const { '.*${na}${sp} = .*' }
.regexp { '${na}' }
else { '.*${sy}${sp}${na}${sp}.*' } // struct, enum, interface
}
// println(query)
// dump(query)
for file in files_to_search {
fdr.search_within_file(file, query)
}
@ -192,10 +181,10 @@ fn (mut fdr Finder) search_within_file(file string, query string) {
fn (fdr Finder) show_results() {
if fdr.matches.len < 1 && (verbose || header) {
print(fdr)
println(maybe_color(term.bright_yellow, 'No Matches found'))
println(colorize(bright_yellow, 'No Matches found'))
} else if verbose || header {
print(fdr)
println(maybe_color(term.bright_green, '${fdr.matches.len} matches Found\n'))
println(colorize(bright_green, '${fdr.matches.len} matches Found\n'))
for result in fdr.matches {
result.show()
}
@ -207,15 +196,15 @@ fn (fdr Finder) show_results() {
}
fn (fdr Finder) str() string {
v := maybe_color(term.bright_red, '${fdr.visib}')
m := maybe_color(term.bright_red, '${fdr.mutab}')
v := colorize(bright_red, '${fdr.visib}')
m := colorize(bright_red, '${fdr.mutab}')
st := if fdr.receiver != '' { ' ( _ ${fdr.receiver})' } else { '' }
s := maybe_color(term.bright_magenta, '${fdr.symbol}')
n := maybe_color(term.bright_cyan, '${fdr.name}')
s := colorize(bright_magenta, '${fdr.symbol}')
n := colorize(bright_cyan, '${fdr.name}')
mm := if fdr.modul != '' { maybe_color(term.blue, '${fdr.modul}') } else { '' }
mm := if fdr.modul != '' { colorize(blue, '${fdr.modul}') } else { '' }
dd := if fdr.dirs.len != 0 {
fdr.dirs.map(maybe_color(term.blue, it))
fdr.dirs.map(colorize(blue, it))
} else {
fdr.dirs
}
@ -241,9 +230,9 @@ struct Match {
}
fn (mtc Match) show() {
path := maybe_color(term.bright_magenta, mtc.path)
line := maybe_color(term.bright_yellow, '${mtc.line}')
text := maybe_color(term.bright_green, '${mtc.text}')
path := colorize(bright_magenta, mtc.path)
line := colorize(bright_yellow, '${mtc.line}')
text := colorize(bright_green, '${mtc.text}')
if verbose || format {
println('${path}\n${line} : [ ${text} ]\n')
} else {

View file

@ -1,7 +1,7 @@
module main
import os
import term
import term { bright_green, bright_red, bright_yellow, ecolorize }
import os.cmdline
// Symbol type to search
@ -59,7 +59,6 @@ const vlib_dir = os.join_path(os.dir(vexe), 'vlib')
const vmod_dir = os.vmodules_dir()
const vmod_paths = os.vmodules_paths()[1..]
const current_dir = os.abs_path('.')
const color_out = term.can_show_color_on_stdout()
fn (mut cfg Symbol) set_from_str(str_in string) {
if str_in !in symbols {
@ -105,49 +104,26 @@ fn invalid_option(invalid ParamOption, arg string) {
}
}
fn valid_args_quantity_or_show_help(args []string) {
if true in [
(args.len < 1),
'-help' in args,
'--help' in args,
args == ['help'],
] {
os.system('${os.quoted_path(vexe)} help where')
exit(0)
}
}
fn make_and_print_error(msg string, opts []string, arg string) {
if verbose || format {
eprintln('\n' + maybe_color(term.bright_yellow, msg))
eprintln('\n' + ecolorize(bright_yellow, msg))
if opts.len > 0 {
eprint(opts.map(maybe_color(term.bright_green, it)).join(' | '))
eprint(opts.map(ecolorize(bright_green, it)).join(' | '))
}
eprintln(' ...can not be ${maybe_color(term.bright_red, arg)}')
eprintln(' ...can not be ${ecolorize(bright_red, arg)}')
} else {
eprint(maybe_color(term.bright_yellow, msg) + ' ')
eprint(ecolorize(bright_yellow, msg) + ' ')
if opts.len > 0 {
eprint(opts.map(maybe_color(term.bright_green, it)).join(' | '))
eprint(opts.map(ecolorize(bright_green, it)).join(' | '))
}
eprintln(' ...can not be ${maybe_color(term.bright_red, arg)}')
eprintln(' ...can not be ${ecolorize(bright_red, arg)}')
}
exit(1)
}
fn maybe_color(term_color fn (string) string, str string) string {
if color_out {
return term_color(str)
} else {
return str
}
}
fn collect_v_files(path string, recursive bool) ![]string {
if path == '' {
return error('path cannot be empty')
}
if !os.is_dir(path) {
return error('path does not exist or is not a directory')
if path == '' || !os.is_dir(path) {
return error('path `${path}` does not exist or is not a directory')
}
mut all_files := []string{}
mut entries := os.ls(path)!
@ -167,13 +143,10 @@ fn collect_v_files(path string, recursive bool) ![]string {
}
fn resolve_module(path string) !string {
if os.is_dir(path) {
return path
} else if os.is_dir(os.join_path(vmod_dir, path)) {
return os.join_path(vmod_dir, path)
} else if os.is_dir(os.join_path(vlib_dir, path)) {
return os.join_path(vlib_dir, path)
} else {
return error('Path: ${path} not found')
return match true {
os.is_dir(path) { path }
os.is_dir(os.join_path(vmod_dir, path)) { os.join_path(vmod_dir, path) }
os.is_dir(os.join_path(vlib_dir, path)) { os.join_path(vlib_dir, path) }
else { error('Path: ${path} not found') }
}
}

View file

@ -4,9 +4,10 @@ import os
fn main() {
args := os.args[2..]
valid_args_quantity_or_show_help(args)
if args.len == 0 || args == ['help'] || '-help' in args || '--help' in args {
os.system('${os.quoted_path(vexe)} help where')
exit(0)
}
mut fdr := Finder{}
fdr.configure_from_arguments(args)
fdr.search_for_matches()