Skip to content

Commit 800cf91

Browse files
Clean up an if/else to make it more readable
1 parent e526f6b commit 800cf91

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

diff-so-fancy

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -86,42 +86,52 @@ if (!$has_stdin) {
8686
} else {
8787
die("Missing input on STDIN\n");
8888
}
89-
} else {
90-
# Check to see if were using default settings
91-
check_first_run();
92-
93-
my @lines;
94-
local $DiffHighlight::line_cb = sub {
95-
push(@lines,@_);
96-
97-
my $last_line = $lines[-1];
98-
99-
# Buffer X lines before we try and output anything
100-
# Also make sure we're sending enough data to d-s-f to do it's magic.
101-
# Certain things require a look-ahead line or two to function so
102-
# we make sure we don't break on those sections prematurely
103-
if (@lines > 24 && ($last_line !~ /^${ansi_color_regex}(---|index|old mode|similarity index|rename (from|to))/)) {
104-
do_dsf_stuff(\@lines);
105-
@lines = ();
106-
}
107-
};
89+
}
10890

109-
my $line_count = 0;
110-
while (my $line = <STDIN>) {
111-
# If the very first line of the diff doesn't start with ANSI color we're assuming
112-
# it's a raw patch file, and we have to color the added/removed lines ourself
113-
if (!$color_forced && $line_count == 0 && starts_with_ansi($line)) {
114-
$manually_color_lines = 1;
115-
}
91+
#################################################################################
92+
#################################################################################
93+
94+
# Check to see if were using default settings
95+
check_first_run();
11696

117-
my $ok = DiffHighlight::handle_line($line);
118-
$line_count++;
97+
# The logic here is that we run all the lines through DiffHighlight first. This
98+
# highlights all the intra-word changes. Then we take those lines and send them
99+
# to do_dsf_stuff() to convert the diff to human readable d-s-f output and add
100+
# appropriate fanciness
101+
102+
my @lines;
103+
local $DiffHighlight::line_cb = sub {
104+
push(@lines,@_);
105+
106+
my $last_line = $lines[-1];
107+
108+
# Buffer X lines before we try and output anything
109+
# Also make sure we're sending enough data to d-s-f to do it's magic.
110+
# Certain things require a look-ahead line or two to function so
111+
# we make sure we don't break on those sections prematurely
112+
if (@lines > 24 && ($last_line !~ /^${ansi_color_regex}(---|index|old mode|similarity index|rename (from|to))/)) {
113+
do_dsf_stuff(\@lines);
114+
@lines = ();
115+
}
116+
};
117+
118+
my $line_count = 0;
119+
while (my $line = <STDIN>) {
120+
# If the very first line of the diff doesn't start with ANSI color we're assuming
121+
# it's a raw patch file, and we have to color the added/removed lines ourself
122+
if (!$color_forced && $line_count == 0 && starts_with_ansi($line)) {
123+
$manually_color_lines = 1;
119124
}
120125

121-
DiffHighlight::flush();
122-
do_dsf_stuff(\@lines);
126+
my $ok = DiffHighlight::handle_line($line);
127+
$line_count++;
123128
}
124129

130+
# If we're mid hunk above process anything still pending
131+
DiffHighlight::flush();
132+
do_dsf_stuff(\@lines);
133+
134+
#################################################################################
125135
#################################################################################
126136

127137
sub do_dsf_stuff {

0 commit comments

Comments
 (0)