Skip to content
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

Add support for hex colors #446

Open
carlfriedrich opened this issue Sep 17, 2022 · 4 comments
Open

Add support for hex colors #446

carlfriedrich opened this issue Sep 17, 2022 · 4 comments

Comments

@carlfriedrich
Copy link

Follow-up for #423.

Please support hex color codes like git does in diff-so-fancy, so that we can have smoothly integrated, unobtrusive background colors like this:

grafik

@webstech
Copy link
Contributor

webstech commented Jan 26, 2023

Did some work on this to avoid working on other things. 😉 @scottchiefbaker Does this look like a good direction for this? May need some more error checks but it appears to handle issue #438. Not really sure what default is supposed to be. Maybe it should just be ignored like normal. Both of these do not make much sense for diff output.

sub git_ansi_color {
	my $str   = shift();
	my @parts = split(' ', $str);

	if (!@parts) {
		return '';
	}
	my $colors = {
		'black'   => 0,
		'red'     => 1,
		'green'   => 2,
		'yellow'  => 3,
		'blue'    => 4,
		'magenta' => 5,
		'cyan'    => 6,
		'white'   => 7,
	};

	my @ansi_part = ();

	if (grep { /bold/ } @parts) {
		push(@ansi_part, "1");
		@parts = grep { !/bold/ } @parts; # Remove from array
	}

	if (grep { /reverse/ } @parts) {
		push(@ansi_part, "7");
		@parts = grep { !/reverse/ } @parts; # Remove from array
	}

        # strip off the attributes, including 'no' and 'no-' prefixes
	@parts = grep { !/^n*o*-*(bold|dim|ul|blink|reverse|italic|strike)$/g } @parts;

	my $fg  = $parts[0] // "";
	my $bg  = $parts[1] // "";

	set_ansi_color( $fg, 0, \@ansi_part, $colors ) if $fg;
	set_ansi_color( $bg, 10, \@ansi_part, $colors ) if $bg;

	#############################################

	my $ansi_str = join(";", @ansi_part);
	my $ret      = "\e[" . $ansi_str . "m";

	return $ret;
}

sub set_ansi_color {
	my ($color, $increment, $ansi_part, $colors) = @_;
	my $base_code = 30 + $increment;
	my $base8_code = 38 + $increment;
	my $default_code = 39 + $increment;
	my $ext_code = 82 + $increment;

	if (is_numeric($color)) {
		if ($color < 8) {
			push(@$ansi_part, $color + $base_code);
		} elsif ($color < 16) {
			push(@$ansi_part, $color + $ext_code);
		} else {
			push(@$ansi_part, "$base8_code;5;$color");
		}
	# It's a full rgb code
	} elsif ($color =~ /^#/) {
		my ($rgbr, $rgbg, $rgbb) = $color =~ /.(..)(..)(..)/;
		push(@$ansi_part, "$base8_code;2;" . hex($rgbr) . ";" . hex($rgbg) . ";" . hex($rgbb));
	# Reset default (whatever that is)
	} elsif ($color =~ /default/) {
		push(@$ansi_part, "$default_code");
	# It's a simple 16 color OG ansi
	} elsif ($color ne "normal") {
		my $bright    = $color =~ s/bright//;
		my $color_num = $colors->{$color} + $base_code;

		if ($bright) { $color_num += 60; } # Set bold

		push(@$ansi_part, $color_num);
	}
}

Edits updated code.

@scottchiefbaker
Copy link
Contributor

@webstech this looks like a sensible start yes. I like your code style it's easy to read and understand. I'd need a little help (more comments) on exactly what the variables in set_ansi_color() do but I think it's all workable. Does this pass the existing unit tests?

Do you have it on a branch other people can play with?

@webstech
Copy link
Contributor

Do you have it on a branch other people can play with?

Hopefully tomorrow. Adding some tests for git-config. The changes will be at this branch. It has the first change in the series and is based on next.

Does this pass the existing unit tests?

Yes it does. I use Windows, so installing the sub-modules was best done under wsl but of course I did know that until after having installed them. The bats .gitattributes is not complete for eol settings. dsf also has issues here (or at least perceived while trying to get bats to run). Lots of fun getting bats to run.

@webstech
Copy link
Contributor

Do you have it on a branch other people can play with?

Hopefully tomorrow. Adding some tests for git-config. The changes will be at this branch. It has the first change in the series and is based on next.

PR #453 has been opened as a first step in this change series. Follow-up patches will implement the hex colors support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants