Repository navigation
Create the side-by-side option (-y) feature for the diff command (Incomplete) #117
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8105420
Create the side-by-side option (-y) feature for the diff command (Inc…
sami-daniel dff98a2
fix(deps): update rust crate chrono to v0.4.41
renovate[bot] 1ef6923
Add side by side diff (partial)
sami-daniel a3e57c9
chore(deps): update rust crate tempfile to v3.20.0
renovate[bot] 45b3072
Configure CI fuzzer for fuzz_side
sami-daniel fce0881
Merge branch 'main' into main
sami-daniel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Create the side-by-side option (-y) feature for the diff command (Inc…
…omplete). - 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. - Create tests for SideBySide implementation
- Loading branch information
commit 8105420bb4b665763cca72ec0b9851906fc85ad1
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ mod ed_diff; | |
| mod macros; | ||
| mod normal_diff; | ||
| mod params; | ||
| mod side_diff; | ||
| mod unified_diff; | ||
| mod utils; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // This file is part of the uutils diffutils package. | ||
| // | ||
| // For the full copyright and license information, please view the LICENSE-* | ||
| // files that was distributed with this source code. | ||
|
|
||
| use crate::utils::limited_string; | ||
| use diff::Result; | ||
| use std::{ | ||
| io::{stdout, StdoutLock, Write}, | ||
| vec, | ||
| }; | ||
|
|
||
| fn push_output( | ||
| output: &mut StdoutLock, | ||
| left_ln: &[u8], | ||
| right_ln: &[u8], | ||
| symbol: &[u8], | ||
| tab_size: usize, | ||
| ) -> std::io::Result<()> { | ||
| // The reason why this function exists, is that we cannot | ||
| // assume a enconding for our left or right line, and the | ||
| // writeln!() macro obligattes us to do it. | ||
|
|
||
| // side-by-side diff usually prints the output like: | ||
| // {left_line}{tab}{space_char}{symbol(|, < or >)}{space_char}{right_line}{EOL} | ||
|
|
||
| // recalculate how many spaces are nescessary, cause we need to take into | ||
| // consideration the lenght of the word before print it. | ||
| let tab_size = (tab_size as isize - left_ln.len() as isize).max(0); | ||
| let ident = vec![b' '; tab_size as usize]; | ||
| output.write_all(left_ln)?; // {left_line} | ||
| output.write_all(&ident)?; // {tab} | ||
| output.write_all(b" ")?; // {space_char} | ||
| output.write_all(symbol)?; // {symbol} | ||
| output.write_all(b" ")?; // {space_char} | ||
| output.write_all(right_ln)?; // {right_line} | ||
|
|
||
| writeln!(output)?; // {EOL} | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| pub fn diff(from_file: &[u8], to_file: &[u8]) -> Vec<u8> { | ||
| // ^ The left file ^ The right file | ||
|
|
||
| let mut output = stdout().lock(); | ||
| let left_lines: Vec<&[u8]> = from_file.split(|&c| c == b'\n').collect(); | ||
| let right_lines: Vec<&[u8]> = to_file.split(|&c| c == b'\n').collect(); | ||
| let tab_size = 61; // for some reason the tab spaces are 61 not 60 | ||
| for result in diff::slice(&left_lines, &right_lines) { | ||
| match result { | ||
| Result::Left(left_ln) => { | ||
| push_output( | ||
| &mut output, | ||
| limited_string(left_ln, tab_size), | ||
| &[], | ||
| b"<", | ||
| tab_size, | ||
| ) | ||
| .unwrap(); | ||
| } | ||
| Result::Right(right_ln) => { | ||
| push_output( | ||
| &mut output, | ||
| &[], | ||
| limited_string(right_ln, tab_size), | ||
| b">", | ||
| tab_size, | ||
| ) | ||
| .unwrap(); | ||
| } | ||
| Result::Both(left_ln, right_ln) => { | ||
| push_output( | ||
| &mut output, | ||
| limited_string(left_ln, tab_size), | ||
| limited_string(right_ln, tab_size), | ||
| b" ", | ||
| tab_size, | ||
| ) | ||
| .unwrap(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| vec![] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.