]> git.nbdom.net Git - nb.git/commitdiff
ENCODE
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 22 Jan 2024 23:11:56 +0000 (00:11 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 22 Jan 2024 23:11:56 +0000 (00:11 +0100)
bin/csv2human

index 3186aa18415588003c6bf7ace2072aaa3844497e..284242963f340bee0d72907c5bc4cb429ee85006 100755 (executable)
@@ -51,15 +51,29 @@ binmode( STDOUT, "raw:" );
 my @len = ();
 my @lines = (); 
 
+use Encode;
+my $ENCODE = 1;
+binmode STDOUT, ":utf8" if $ENCODE;
+#binmode STDOUT;
+sub realLength() {
+       #return scalar( map $_, $_[0] =~ /(.)/gs );
+       #my $l = length($_[0]);
+       #my $l = bytes::length($_[0]);
+       $_[0] = Encode::decode_utf8($_[0]) if $ENCODE;
+       #return $l;
+       return length($_[0]);
+               #$F[$i] = Encode::encode_utf8($F[$i]);
+       #return bytes::length($_[0]);
+}
+
 while (<>) {
 
        my @F = $Opt{format} ? split(/\s*$Opt{sep}\s*/) : split(/$Opt{sep}/);
 
        for (my $i=0;$i<@F;$i++) {
                chomp($F[$i]);
-               # NB 13.12.23 $F[$i] = Encode::decode_utf8($F[$i]);
                my $l;
-               $len[$i] = $l if ( $l = length($F[$i]) ) >= ($len[$i] || 0);
+               $len[$i] = $l if ( $l = &realLength($F[$i]) ) >= ($len[$i] || 0);
        }
 
        push @lines, [@F];
@@ -78,10 +92,16 @@ my $format = "│ ".join(" │ ",map {$t+=$_+3; "\%-".$_."s"} @len)." │".chr(1
 my $sep_line_first = "┌".join("┬",map {("─"x($_+2))} @len)."┐".chr(10);
 my $sep_line = "├".join("┼",map {("─"x($_+2))} @len)."┤".chr(10);
 my $sep_line_last = "└".join("┴",map {("─"x($_+2))} @len)."┘".chr(10);
-# NB 13.12.23 my $format = "| ".join(" | ",map {$t+=$_+3; "\%-".$_."s"} @len)." |".chr(10);
-# NB 13.12.23 my $sep_line = "+".join("+",map {("-"x($_+2))} @len)."+".chr(10);
-# NB 13.12.23 my $sep_line_first = $sep_line;
-# NB 13.12.23 my $sep_line_last = $sep_line;
+if ($ENCODE) {
+       $format = Encode::decode("UTF-8",$format);
+       $sep_line_first = Encode::decode("UTF-8",$sep_line_first);
+       $sep_line = Encode::decode("UTF-8",$sep_line);
+       $sep_line_last = Encode::decode("UTF-8",$sep_line_last);
+}
+# NB 23.01.24 my $format = "| ".join(" | ",map {$t+=$_+3; "\%-".$_."s"} @len)." |".chr(10);
+# NB 23.01.24 my $sep_line = "+".join("+",map {("-"x($_+2))} @len)."+".chr(10);
+# NB 23.01.24 my $sep_line_first = $sep_line;
+# NB 23.01.24 my $sep_line_last = $sep_line;
 
 #
 # Format / align 
@@ -94,7 +114,7 @@ if ($Opt{format}) {
        $format = join($sep,map
                {
                        $i++;
-                       $t+=$_+length($sep);
+                       $t+=$_+&realLength($sep);
                        $i == @len ? '%s' : "\%-".$_."s";
                } @len
        ).chr(10);
@@ -121,6 +141,7 @@ while (my $line = shift @lines)
        }
 
        printf $format,@$line;
+       # NB 23.01.24 printf $format,map{$ENCODE ? Encode::encode_utf8($_) : $_} @$line;
        print $sep_line if $Opt{header} and !$i++;
 }