Skip to content

Instantly share code, notes, and snippets.

@rlerdorf
Created December 21, 2020 16:47
Show Gist options
  • Save rlerdorf/081be15e1b5a3da241b70e73a5289812 to your computer and use it in GitHub Desktop.
Save rlerdorf/081be15e1b5a3da241b70e73a5289812 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
function rgb($hex) {
$r = substr($hex,0,2);
$g = substr($hex,2,2);
$b = substr($hex,4,2);
return [hexdec($r), hexdec($g), hexdec($b)];
}
function preview($str, $cols) {
[$r,$g,$b] = rgb($cols['foreground']);
[$br,$bg,$bb] = rgb($cols['background']);
[$sr,$sg,$sb] = rgb($cols['selection_background']);
[$cr,$cg,$cb] = rgb($cols['cursor']);
$ret = "\x1b[38;2;{$cr};{$cg};{$cb}m\u{2588}\x1b[38;2;{$r};{$g};{$b}m\x1b[48;2;{$br};{$bg};{$bb}m{$str} ";
$c = 'a';
$len = strlen($str)+2;
foreach($cols as $name=>$rgb) {
if(substr($name,0,5) != 'color') continue;
[$r,$g,$b] = rgb($rgb);
$ret .= "\x1b[38;2;{$r};{$g};{$b}m{$c}";
$c++;
$len++;
}
$sel = substr(implode('',range('A','z')),0,52-$len);
if(empty($cols['selection_foreground'])) {
$ret .= "\x1b[38;2;0;0;0m\x1b[48;2;{$sr};{$sg};{$sb}m{$sel} \x1b[0m";
} else {
[$r,$g,$b] = rgb($cols['selection_foreground']);
$ret .= "\x1b[38;2;{$r};{$g};{$b}m\x1b[48;2;{$sr};{$sg};{$sb}m{$sel} \x1b[0m";
}
return $ret;
}
function themes($dir) {
$themes = [];
foreach (new DirectoryIterator($dir) as $f) {
if($f->isDot()) continue;
$fn = $f->getFilename();
$cl = [];
$cols = file("$dir/$fn");
foreach($cols as $line) {
if(preg_match('/([^\s]+)\s*#(.*)$/', $line, $ma)) {
$cl[$ma[1]] = $ma[2];
}
}
$themes[substr($fn,0,-5)] = $cl;
}
return $themes;
}
$themes = themes("{$_ENV['HOME']}/.config/kitty/kitty-themes/themes");
ksort($themes);
$sel = [];
$i = 1;
foreach($themes as $name=>$cols) {
$pre = preview($name,$cols);
$h = str_pad(dechex($i),2,'0',STR_PAD_LEFT);
if($argc==1) {
echo "$h:$pre ";
if(!($i%2)) echo "\n";
}
$sel[$h] = $name;
$i++;
}
if($argc>1) {
system("kitty @ set-colors -a ~/.config/kitty/kitty-themes/themes/{$sel[$argv[1]]}.conf");
}
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment