Skip to content

Instantly share code, notes, and snippets.

@gypark
Created April 18, 2012 08:58
Show Gist options
  • Save gypark/2412176 to your computer and use it in GitHub Desktop.
Save gypark/2412176 to your computer and use it in GitHub Desktop.
git diff 의 출력을 받아서 cp949 인코딩으로 변환하여 출력
#!/usr/bin/env perl
# git diff 의 출력을 받아서 인코딩을 변환하여 출력
# 예: git diff --color | cat_encode.pl
use strict;
use warnings;
use Encode;
use Encode::Guess;
my $pat_color = qr/(?:\e\[[;\d]*?m)/;
my @ansi = ();
my $output_encoding = 'cp949';
while ( my $line = <> ) {
chomp $line;
# ANSI 색상지정 코드를 일단 제거
$line =~ s/($pat_color)/save_ansi($1)/eg;
my $enc = guess_encoding( $line, qw/cp949/ );
if ( not ref($enc) ) {
print "!!! ", $line, "\n";
next;
}
my $str = encode($output_encoding, $enc->decode($line));
# ANSI 코드 복원
$str =~ s/___ansicode\((\d+)\)___/$ansi[$1]/eg;
print $str, "\n";
}
sub save_ansi {
return '' unless ( -t STDOUT);
my $ansi = shift;
push @ansi, $ansi;
return "___ansicode($#ansi)___";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment