Skip to content

Instantly share code, notes, and snippets.

@aaron-em
Created December 2, 2016 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaron-em/a5120ffe4f200be1554df045d5292aab to your computer and use it in GitHub Desktop.
Save aaron-em/a5120ffe4f200be1554df045d5292aab to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use constant EOT => chr(4);
use constant BEL => chr(7);
use constant ESC => chr(27);
use constant FONTSCALE => 1.3;
# FIXME this is not idempotent - calling it a second time hoses up the
# terminal
sub get_term_fontsize {
local $|=1;
my $oldstty = `stty -g`;
my $resp;
my $size;
system('stty raw -echo min 0');
print ESC . "]7770;?" . BEL;
system("stty $oldstty");
sysread(STDIN, $resp, 9);
$resp =~ m@;(\d+)@;
$size = $1;
return $size;
};
sub print_sample_sixel_image {
print ESC . "Pq";
print "#0;2;0;0;0#1;2;100;100;100#2;2;80;80;80\n";
print "#1??}}GG}}??}}??--\n";
for (my $j = 0; $j < 2; $j++) {
my $last = 0;
for (my $i = 0; $i <= 12; $i++) {
my $reg = rand(2) + 1;
print "#" . ($last % 2 ? 1 : 2);
print chr(63 + (1 << $last));
$last += 1;
$last = $last % 6;
};
print "-\n"; # end with '-' to break the line
};
print ESC . "\\";
};
sub print_sample_sixel_of_height {
my $height = shift() * FONTSCALE;
my $width = $height;
my $grid = [];
for my $i (1..$height) {
my $row = [];
my $val = ($i % 2);
for (1..$width) {
push @$row, $val;
$val = ++$val % 2;
};
push @$grid, $row;
};
my $sixel_strips = [];
my $n_strips = 0;
my $leftover = $height;
while ($leftover > 6) {
$n_strips += 1;
$leftover -= 6;
};
if ($leftover > 0) {
$n_strips += 1;
};
for my $stripnum (0..$n_strips-1) {
my $sixel_strip = [];
my $minrow = $stripnum * 6;
my $maxrow = $minrow + 5;
if ($maxrow > ($height-1)) {
$maxrow = $height - 1;
};
for my $col (0..$width-1) {
my $sixel_val = 0;
my $shl = 0;
for my $row ($minrow..$maxrow) {
my $pixel = $grid->[$row]->[$col];
if ($pixel == 1) {
$sixel_val += (1 << $shl);
};
$shl += 1;
};
push @$sixel_strip, $sixel_val;
};
push @$sixel_strips, $sixel_strip;
};
print ESC . "Pq";
print "#1;2;100;100;100\n";
# foreach my $sixel_row (@$sixel_strips) {
for (my $i = 0; $i < scalar(@$sixel_strips); $i++) {
my $sixel_row = $sixel_strips->[$i];
print "#1";
foreach my $sixel_col (@$sixel_row) {
print chr(63+$sixel_col);
};
print '-' if ($i < (scalar(@$sixel_strips))-1);
print "\n";
};
print ESC . "\\";
};
# print_sample_sixel_image();
$term_fontsize = get_term_fontsize();
print_sample_sixel_of_height($term_fontsize);
print "lol!";
print_sample_sixel_of_height($term_fontsize);
print "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment