Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v.utils: allow to set names of compared items when diffing strings #21650

Merged
merged 4 commits into from
Jun 7, 2024

Conversation

ttytm
Copy link
Member

@ttytm ttytm commented Jun 6, 2024

Allows further customization when comparing strings.

It adds the ability to set the names that are used for the temporary files when comparing strings directly.

Two main reason:

  • Some names in the output can fit better
  • Several diff viewers colorize based on on the file extension. E.g. when using delta or piping output like v fmt -diff file.v | diff-so-fancy or using other viewers set via VDIFF_CMD.

Filename samples:

Current default (not customizable) Updated default Updated names passed
Screenshot_20240606_045034 Screenshot_20240606_035334 Screenshot_20240606_035502

E.g. using vs not using file extensions:

import v.util.diff

fn main() {
	txt1 := '
sum = num1 + num2
print("The sum of {0} and {1} is {2}".format(num1, num2, sum))
	'
	txt2 := '
sum = num1+num2
print("The sum of {0} and {1} is {2}".format(num1, num2, sum))
	'
	println(diff.compare_text(txt1, txt2)!)
	// println(diff.compare_text(txt1, txt2, base_name: 'old.py', target_name: 'new.py')!)
}
filename not set colored based on filename
Screenshot_20240606_035942 Screenshot_20240606_040000

Suggestions for other new default names than base and target are welcome. Those should be general names similar to text1 and text2 since it's not always a comparison of e.g. expected and output.

@spytheman
Copy link
Member

Can you please make a test for it?

@spytheman spytheman merged commit 7c8b4b8 into vlang:master Jun 7, 2024
76 checks passed
@ttytm ttytm deleted the diff/tmp-file-names branch June 7, 2024 10:44
raw-bin pushed a commit to raw-bin/v that referenced this pull request Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants