mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
v.utils: allow to set the names of the compared items, when diffing strings (#21650)
This commit is contained in:
parent
096226bf05
commit
7c8b4b8970
2 changed files with 19 additions and 4 deletions
|
@ -24,6 +24,14 @@ pub:
|
|||
env_overwrite_var ?string = 'VDIFF_CMD'
|
||||
}
|
||||
|
||||
@[params]
|
||||
pub struct CompareTextOptions {
|
||||
CompareOptions
|
||||
pub:
|
||||
base_name string = 'base'
|
||||
target_name string = 'target'
|
||||
}
|
||||
|
||||
// Default options for `diff` and `colordiff`.
|
||||
// Short `diff` args are supported more widely (e.g. on OpenBSD, ref. https://man.openbsd.org/diff.1).
|
||||
// `-d -a -U 2` ^= `--minimal --text --unified=2`
|
||||
|
@ -79,15 +87,15 @@ pub fn compare_files(path1 string, path2 string, opts CompareOptions) !string {
|
|||
}
|
||||
|
||||
// compare_text returns a string displaying the differences between two strings.
|
||||
pub fn compare_text(text1 string, text2 string, opts CompareOptions) !string {
|
||||
pub fn compare_text(text1 string, text2 string, opts CompareTextOptions) !string {
|
||||
ctime := time.sys_mono_now()
|
||||
tmp_dir := os.join_path_single(os.vtmp_dir(), ctime.str())
|
||||
os.mkdir(tmp_dir)!
|
||||
defer {
|
||||
os.rmdir_all(tmp_dir) or {}
|
||||
}
|
||||
path1 := os.join_path_single(tmp_dir, 'text1.txt')
|
||||
path2 := os.join_path_single(tmp_dir, 'text2.txt')
|
||||
path1 := os.join_path_single(tmp_dir, opts.base_name)
|
||||
path2 := os.join_path_single(tmp_dir, opts.target_name)
|
||||
// When comparing strings and not files, prevent `\ No newline at end of file` in the output.
|
||||
if !text1.ends_with('\n') || !text2.ends_with('\n') {
|
||||
os.write_file(path1, text1 + '\n')!
|
||||
|
@ -96,7 +104,7 @@ pub fn compare_text(text1 string, text2 string, opts CompareOptions) !string {
|
|||
os.write_file(path1, text1)!
|
||||
os.write_file(path2, text2)!
|
||||
}
|
||||
return compare_files(path1, path2, opts)!
|
||||
return compare_files(path1, path2, opts.CompareOptions)!
|
||||
}
|
||||
|
||||
fn (opts CompareOptions) find_tool() !(DiffTool, string) {
|
||||
|
|
|
@ -107,6 +107,13 @@ fn test_compare_string() {
|
|||
assert res.contains('-abc'), res
|
||||
assert res.contains('+abcd'), res
|
||||
assert !res.contains('No newline at end of file'), res
|
||||
// Default base and target name.
|
||||
assert res.match_glob('*---*base*'), res
|
||||
assert res.match_glob('*+++*target*'), res
|
||||
// Custom base and target name.
|
||||
res = diff.compare_text('abc', 'abcd', tool: .diff, base_name: 'old.v', target_name: 'new.v')!
|
||||
assert res.match_glob('*---*old.v*'), res
|
||||
assert res.match_glob('*+++*new.v*'), res
|
||||
}
|
||||
|
||||
fn test_coloring() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue