From 9318c4aae03674fdacb635e56f34fbe69c958d45 Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Sat, 13 Apr 2024 20:21:39 -0700 Subject: [PATCH] Use state for static variables --- diff-so-fancy | 58 +++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/diff-so-fancy b/diff-so-fancy index 4dbd7d9..d63ade0 100755 --- a/diff-so-fancy +++ b/diff-so-fancy @@ -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