v2 / vlib / v / util / diff / diff.v
89 lines · 78 sloc · 3.0 KB · 3ffc951cf555dc4818309a507ccb9d0da4de748f
Raw
1module diff
2
3import os
4// import term
5import arrays.diff as arrays_diff
6
7// compare_files returns a string displaying the differences between two files.
8pub fn compare_files(path1 string, path2 string, _ CompareOptions) !string {
9 src := os.read_lines(path1)!
10 dst := os.read_lines(path2)!
11 mut ctx := arrays_diff.diff(src, dst)
12 patch := ctx.generate_patch(
13 colorful: true // term.can_show_color_on_stdout()
14 block_header: true
15 unified: 3
16 )
17 return patch
18}
19
20// compare_text returns a string displaying the differences between two strings.
21pub fn compare_text(text1 string, text2 string, _ CompareTextOptions) !string {
22 src := text1.split_into_lines()
23 dst := text2.split_into_lines()
24 mut ctx := arrays_diff.diff(src, dst)
25 patch := ctx.generate_patch(
26 colorful: true // term.can_show_color_on_stdout()
27 block_header: true
28 unified: 3
29 )
30 return patch
31}
32
33// deprecated code :
34
35pub enum DiffTool {
36 auto
37 diff // core package on Unix-like systems.
38 colordiff // `diff` wrapper.
39 delta // viewer for git and diff output.
40 // fc // built-in tool on windows. // TODO: enable when its command output can be read.
41}
42
43@[params]
44pub struct CompareOptions {
45pub:
46 tool DiffTool @[deprecated: 'use compare_files or compare_text'; deprecated_after: '2025-12-31']
47 // Custom args used with the diff command.
48 args string @[deprecated: 'use compare_files or compare_text'; deprecated_after: '2025-12-31']
49 // Sets the environment variable whose value can overwrite a diff command passed to a compare function.
50 // It also enables the use of commands that are not in the list of known diff tools.
51 // Set it to `none` to disable it.
52 env_overwrite_var ?string = 'VDIFF_CMD' @[deprecated: 'use compare_files or compare_text'; deprecated_after: '2025-12-31']
53}
54
55@[params]
56pub struct CompareTextOptions {
57 CompareOptions
58pub:
59 base_name string = 'base' @[deprecated: 'use compare_files or compare_text'; deprecated_after: '2025-12-31']
60 target_name string = 'target' @[deprecated: 'use compare_files or compare_text'; deprecated_after: '2025-12-31']
61}
62
63// Allows public checking for the available tools and prevents repeated searches
64// when using compare functions with automatic diff tool detection.
65@[deprecated: 'use compare_files or compare_text']
66@[deprecated_after: '2025-12-31']
67pub fn available_tools() []DiffTool {
68 return []
69}
70
71@[deprecated: 'use compare_files or compare_text']
72@[deprecated_after: '2025-12-31']
73pub fn find_working_diff_command() !string {
74 return error('deprecated')
75}
76
77// color_compare_files returns a colored diff between two files.
78@[deprecated: 'use compare_files instead']
79@[deprecated_after: '2025-12-31']
80pub fn color_compare_files(_ string, path1 string, path2 string) string {
81 return compare_files(path1, path2) or { '' }
82}
83
84// color_compare_strings returns a colored diff between two strings.
85@[deprecated: 'use compare_text instead']
86@[deprecated_after: '2025-12-31']
87pub fn color_compare_strings(_ string, _ string, expected string, found string) string {
88 return compare_text(expected, found) or { '' }
89}
90