diffutils icon indicating copy to clipboard operation
diffutils copied to clipboard

Create the side-by-side option (-y) feature for the diff command (Incomplete)

Open sami-daniel opened this issue 9 months ago • 4 comments

  • Create the function, in the utils package, limited_string that allows you to truncate a string based on a delimiter (May break the encoding of the character where it was cut)

  • Create tests for limited_string function

  • Add support for -y and --side-by-side flags that enables diff output for side-by-side mode

  • Create implementation of the diff -y (SideBySide) command, base command for sdiff, using the crate diff as engine. Currently it does not fully represent GNU diff -y, some flags (|, (, ), , /) could not be developed due to the limitation of the engine we currently use (crate diff), which did not allow perform logic around it. Only the use of '<' and '>' were enabled.

TL;DR

A new limited_string function was added to the utils package, allowing strings to be truncated based on a delimiter (note: this may break character encoding at the cut point). Unit tests were created to ensure correct behavior of the function.

Support for the -y and --side-by-side flags was added to enable side-by-side diff output. The SideBySide implementation, serving as the base for the sdiff command, was developed using the diff crate as the comparison engine. Due to limitations of the crate, only the < and > markers are currently supported—other markers like |, (, ),\ and / could not be implemented.

Clarification The main goal of the limited_string function is to simplify and standardize the process of truncating strings for everyone working on the project. The idea was to provide a utility that anyone could use without having to reimplement or worry too much about string manipulation logic. However, a known limitation of the current implementation is that it can break character encoding, especially when the cut happens mid-way through a multi-byte character (e.g., in UTF-8).

I am open to suggestions on how to improve or safely handle encoding, whether by detecting boundaries or using a safer string-splitting strategy that respects character integrity.

Regarding the SideBySide implementation: I recognize that the output and edit script provided by the diff crate is limited. These limitations currently prevent us from supporting additional characters such as |, (, ), , or / that are used in GNU diff -y to more accurately reflect changes in lines.

For this initial version, I think the use of < and >, are sufficient for a first pass of side-by-side comparison. Moving forward, I did like to open a follow-up issue where we can evaluate better strategies (possibly even considering another diff engine) to handle richer diff representations and support for the missing symbols.

Suggestions and ideas are very welcome!

sami-daniel avatar Apr 22 '25 16:04 sami-daniel

Codecov Report

Attention: Patch coverage is 96.34146% with 33 lines in your changes missing coverage. Please review.

Project coverage is 86.19%. Comparing base (5b791e8) to head (fce0881). Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
src/params.rs 33.33% 21 Missing and 1 partial :warning:
src/side_diff.rs 98.96% 8 Missing and 1 partial :warning:
src/diff.rs 0.00% 2 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #117      +/-   ##
==========================================
+ Coverage   84.50%   86.19%   +1.69%     
==========================================
  Files          12       13       +1     
  Lines        5318     6216     +898     
  Branches      476      509      +33     
==========================================
+ Hits         4494     5358     +864     
- Misses        809      839      +30     
- Partials       15       19       +4     
Flag Coverage Δ
macos_latest 86.12% <96.34%> (+1.74%) :arrow_up:
ubuntu_latest 86.21% <96.34%> (+1.72%) :arrow_up:
windows_latest 19.49% <0.55%> (-3.31%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Apr 22 '25 16:04 codecov[bot]

I'll fix the alerts before reopening the PR, 😁

sami-daniel avatar Apr 22 '25 17:04 sami-daniel

Is done : )

sami-daniel avatar Apr 22 '25 19:04 sami-daniel

Hi Sami. Thanks for your work on this feature! I don't have time to spare right now to review it, but please be assured that I will as soon as I can.

oSoMoN avatar May 06 '25 09:05 oSoMoN

@sylvestre I think there is now a cool version of the side. Can you take a look? : )

sami-daniel avatar May 26 '25 13:05 sami-daniel

excellent!

sylvestre avatar Jun 03 '25 12:06 sylvestre