-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.v
More file actions
91 lines (80 loc) · 2.01 KB
/
Copy pathmain.v
File metadata and controls
91 lines (80 loc) · 2.01 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
module main
import os
import flag
import v.vmod
fn main() {
vmodfile := vmod.decode(@VMOD_FILE)!
mut fp := flag.new_flag_parser(os.args)
fp.application(vmodfile.name)
fp.version(vmodfile.version)
fp.description(vmodfile.description)
fp.skip_executable()
fp.arguments_description('[files...]')
max_line_len := fp.int('max-line-len', `m`, 100, 'maximum line length')
indent_style_str := fp.string('indent', 0, 'tabs', 'indentation style (tabs|spaces)')
indent_width := fp.int('indent-width', `w`, 8, 'indentation width')
do_sort_includes := fp.bool('sort-includes', 0, true, 'sort include directives')
in_place := fp.bool('in-place', `i`, false, 'format file in-place')
recursive := fp.bool('recursive', `r`, false,
'recursively format .c and .h files in directories')
extra_args := fp.finalize() or {
eprintln(fp.usage())
exit(1)
}
cfg := Config{
max_line_len: max_line_len
sort_includes: do_sort_includes
indent_style: if indent_style_str == 'spaces' {
.spaces
} else {
.tabs
}
indent_width: indent_width
}
mut paths := extra_args.clone()
if recursive {
mut expanded := []string{}
for arg in extra_args {
expanded << if os.is_dir(arg) {
collect_c_files(arg)
} else {
[arg]
}
}
paths = expanded.clone()
}
if paths.len == 0 {
source_lines := os.get_lines()
source := source_lines.join('\n') + '\n'
print(format(source, cfg))
return
}
for path in paths {
source := os.read_file(path) or {
eprintln('error: failed to read ${path}')
exit(1)
}
formatted := format(source, cfg)
if in_place {
os.write_file(path, formatted) or {
eprintln('error: failed to write ${path}')
exit(1)
}
} else {
print(formatted)
}
}
}
fn collect_c_files(path string) []string {
mut result := []string{}
items := os.ls(path) or { return result }
for item in items {
full := os.join_path(path, item)
if os.is_dir(full) {
result << collect_c_files(full)
} else if full.ends_with('.c') || full.ends_with('.h') {
result << full
}
}
return result
}