vlib,tools: add an arrays.diff module, implement a simple platform independent tool v diff file1.txt file2.txt using it (#24428)

This commit is contained in:
kbkpbot 2025-05-08 16:09:36 +08:00 committed by GitHub
parent 1070378a46
commit 3291372c57
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 505 additions and 0 deletions

37
cmd/tools/vdiff.v Normal file
View file

@ -0,0 +1,37 @@
import os
import flag
import term
import arrays.diff
fn main() {
mut fp := flag.new_flag_parser(os.args[1..])
fp.application('v diff')
fp.version('0.0.1')
fp.description('Compare files line by line. Example: `v diff examples/hello_world.v examples/log.v`')
fp.arguments_description('file1 file2')
fp.skip_executable()
fp.limit_free_args_to_at_least(2)!
if fp.bool('help', `h`, false, 'Show this help screen.') {
println(fp.usage())
exit(0)
}
args := fp.finalize() or {
eprintln('Argument error: ${err}')
exit(1)
}
src := os.read_lines(args[0])!
dst := os.read_lines(args[1])!
mut ctx := diff.diff(src, dst)
patch := ctx.generate_patch(
colorful: term.can_show_color_on_stdout()
block_header: true
unified: 3
)
if patch.len > 0 {
print(patch)
exit(1)
}
}

View file

@ -24,6 +24,7 @@ const external_tools = [
'complete',
'compress',
'cover',
'diff',
'doc',
'doctor',
'download',