v / cmd / tools / vdiff.v
37 lines · 33 sloc · 803 bytes · 3291372c571d8ae654589f1ffe65c42e8c5b1c63
Raw
1import os
2import flag
3import term
4import arrays.diff
5
6fn main() {
7 mut fp := flag.new_flag_parser(os.args[1..])
8 fp.application('v diff')
9 fp.version('0.0.1')
10 fp.description('Compare files line by line. Example: `v diff examples/hello_world.v examples/log.v`')
11 fp.arguments_description('file1 file2')
12 fp.skip_executable()
13 fp.limit_free_args_to_at_least(2)!
14
15 if fp.bool('help', `h`, false, 'Show this help screen.') {
16 println(fp.usage())
17 exit(0)
18 }
19
20 args := fp.finalize() or {
21 eprintln('Argument error: ${err}')
22 exit(1)
23 }
24
25 src := os.read_lines(args[0])!
26 dst := os.read_lines(args[1])!
27 mut ctx := diff.diff(src, dst)
28 patch := ctx.generate_patch(
29 colorful: term.can_show_color_on_stdout()
30 block_header: true
31 unified: 3
32 )
33 if patch.len > 0 {
34 print(patch)
35 exit(1)
36 }
37}
38