-
Notifications
You must be signed in to change notification settings - Fork 13
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
Open
sami-daniel
wants to merge
7
commits into
uutils:main
Choose a base branch
from
sami-daniel:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
463f5e2
Create the side-by-side option (-y) feature for the diff command (Inc…
sami-daniel dc6e8fb
Fix cargo
sami-daniel e2318a1
Apply format with cargo fmt --all
sami-daniel 7b7a6b6
Merge branch 'uutils:main' into main
sami-daniel 196cfb9
Update src/utils.rs
sami-daniel add9ec2
Update src/lib.rs
sami-daniel 1374279
Add license header
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
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,81 @@ | ||
use crate::utils::limited_string; | ||
sami-daniel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
Oops, something went wrong.
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.