Last active
January 2, 2021 20:28
-
-
Save AlexRogalskiy/e1f2a1087f7f0184b0c4f4406df975db to your computer and use it in GitHub Desktop.
Perl sed emulator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$line1="COLOR_RES(\"%d\","; | |
$line2="\tscreen.Acolors[%d],"; | |
$line3="\tDFT_COLOR(\"rgb:%2.2x/%2.2x/%2.2x\")),\n"; | |
# colors 16-231 are a 6x6x6 color cube | |
for ($red = 0; $red < 6; $red++) { | |
for ($green = 0; $green < 6; $green++) { | |
for ($blue = 0; $blue < 6; $blue++) { | |
$code = 16 + ($red * 36) + ($green * 6) + $blue; | |
printf($line1, $code); | |
printf($line2, $code); | |
printf($line3, | |
($red ? ($red * 40 + 55) : 0), | |
($green ? ($green * 40 + 55) : 0), | |
($blue ? ($blue * 40 + 55) : 0)); | |
} | |
} | |
} | |
# colors 232-255 are a grayscale ramp, intentionally leaving out | |
# black and white | |
$code=232; | |
for ($gray = 0; $gray < 24; $gray++) { | |
$level = ($gray * 10) + 8; | |
$code = 232 + $gray; | |
printf($line1, $code); | |
printf($line2, $code); | |
printf($line3, | |
$level, $level, $level); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
================================================================================ | |
#!/usr/bin/perl | |
use strict; | |
sub min { | |
my ($a, $b) = @_; | |
if($a < $b) { return $a; } | |
else { return $b; } | |
} | |
sub conv { | |
my ($col, $alpha) = @_; | |
return min(1, (($alpha)*$col/255 + (1-$alpha))); | |
} | |
sub convert { | |
my ($r,$g,$b,$a) = @_; | |
return sprintf('#%x%x%x', | |
255*conv($r, $a), | |
255*conv($g, $a), | |
255*conv($b, $a)); | |
} | |
while(<>) { | |
chomp; | |
my $line = $_; | |
if($line =~ /(background(-color)?: ?rgba\(([0-9]+), ?([0-9]+), ?([0-9]+), ?([0-9.]+)\);)/) { | |
my $hex = convert($3, $4, $5, $6); | |
my $pos = index($line, $1); | |
$line= substr($line, 0, $pos)."background-color: $hex; ".substr($line, $pos); | |
} | |
if($line =~ /(border(-[^:]*)?:([^;]*)?rgba\(([0-9]+), ?([0-9]+), ?([0-9]+), ?([0-9.]+)\);)/) { | |
my $hex = convert($4, $5, $6, $7); | |
my $pos = index($line, $1); | |
$line = substr($line, 0, $pos)."border$2: $3 $hex; ".substr($line, $pos); | |
} | |
print $line . "\n"; | |
} | |
================================================================================ | |
use strict; | |
use warnings; | |
use DBI; | |
my $link = DBI->connect( "dbi:Pg:dbname=test;host=localhost;port=5432", "root", "***", {RaiseError => 1, AutoCommit => 1}); | |
my $ds = $link->prepare("CREATE TEMP TABLE tmp_temp ( data bigint ); COPY tmp_temp FROM STDIN"); | |
$ds->execute(); | |
$link->func("1\n", "putline"); | |
$link->func("2\n", "putline"); | |
$link->func("3\n", "putline"); | |
$link->func("4\n", "putline"); | |
$link->func("endcopy"); | |
$ds = $link->prepare("SELECT * FROM tmp_temp"); | |
$ds->execute(); | |
while(my $line = $ds->fetchrow()){ | |
print $line."\n"; | |
} | |
================================================================================ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment