Skip to content

Commit

Permalink
Use state for static variables
Browse files Browse the repository at this point in the history
  • Loading branch information
scottchiefbaker committed Apr 14, 2024
1 parent 72638b7 commit 9318c4a
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions diff-so-fancy
Original file line number Diff line number Diff line change
Expand Up @@ -849,41 +849,39 @@ sub color {
}

# Get colors used for various output sections (memoized)
{
my $static_config;
sub get_config_color {
my $str = shift();

sub get_config_color {
my $str = shift();
state $static_config;

my $ret = "";
if ($static_config->{$str}) {
return $static_config->{$str};
}
my $ret = "";
if ($static_config->{$str}) {
return $static_config->{$str};
}

#print color(15) . "Shelling out for color: '$str'\n" . color('reset');

if ($str eq "meta") {
# Default ANSI yellow
$ret = git_ansi_color(git_config('color.diff.meta')) || color(11);
} elsif ($str eq "reset") {
$ret = color("reset");
} elsif ($str eq "add_line") {
# Default ANSI green
$ret = git_ansi_color(git_config('color.diff.new')) || color("2_bold");
} elsif ($str eq "remove_line") {
# Default ANSI red
$ret = git_ansi_color(git_config('color.diff.old')) || color("1_bold");
} elsif ($str eq "fragment") {
$ret = git_ansi_color(git_config('color.diff.frag')) || color("13_bold");
} elsif ($str eq "last_function") {
$ret = git_ansi_color(git_config('color.diff.func')) || color("146_bold");
}
#print color(15) . "Shelling out for color: '$str'\n" . color('reset');

if ($str eq "meta") {
# Default ANSI yellow
$ret = git_ansi_color(git_config('color.diff.meta')) || color(11);
} elsif ($str eq "reset") {
$ret = color("reset");
} elsif ($str eq "add_line") {
# Default ANSI green
$ret = git_ansi_color(git_config('color.diff.new')) || color("2_bold");
} elsif ($str eq "remove_line") {
# Default ANSI red
$ret = git_ansi_color(git_config('color.diff.old')) || color("1_bold");
} elsif ($str eq "fragment") {
$ret = git_ansi_color(git_config('color.diff.frag')) || color("13_bold");
} elsif ($str eq "last_function") {
$ret = git_ansi_color(git_config('color.diff.func')) || color("146_bold");
}

# Cache (memoize) the entry for later
$static_config->{$str} = $ret;
# Cache (memoize) the entry for later
$static_config->{$str} = $ret;

return $ret;
}
return $ret;
}

# https://www.git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#_colors_in_git
Expand Down

0 comments on commit 9318c4a

Please sign in to comment.