From 33e6204c35c6e370b2e3135222d5c9d242029903 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Thu, 15 Dec 2016 21:47:38 +0100 Subject: [PATCH] Solarized theme with 256-color mode --- bin/phpl-edit | 18 ++++++++++-------- bin/phpl-reload | 26 +++++++++++++++++++++----- lib/bootstrap.php | 2 +- lib/config.php | 8 +++++++- lib/generate.php | 39 ++++++++++++++++++++++++++++++++++++--- 5 files changed, 75 insertions(+), 18 deletions(-) diff --git a/bin/phpl-edit b/bin/phpl-edit index 94c19c5..f49a79b 100755 --- a/bin/phpl-edit +++ b/bin/phpl-edit @@ -138,20 +138,22 @@ if (cmdl_get($opts,'list')) { config_item_edit($edit,$attr); config_write(); - if (RELOAD) passthru("phpl-reload --prompt"); + if (RELOAD) passthru("phpl-reload --all"); } elseif (cmdl_get($opts,'E')) { passthru("editor ".escapeshellarg(PHPL_CONFIG)); if (RELOAD) passthru("phpl-reload --prompt"); -} else { - print_help(); -} - - -if (($theme = cmdl_get($opts,'theme'))) { +} elseif (($theme = cmdl_get($opts,'theme'))) { if (!file_exists(PHPL_THEMES."/{$theme}.theme")) { printf("Error: No such theme, %s\n", $theme); exit(1); } $_CONFIG['theme'] = $theme; config_write(); -} \ No newline at end of file + if (RELOAD) passthru("phpl-reload --theme"); +} else { + print_help(); + return; +} + +config_write(); + diff --git a/bin/phpl-reload b/bin/phpl-reload index 7f47860..dea1d91 100755 --- a/bin/phpl-reload +++ b/bin/phpl-reload @@ -97,6 +97,7 @@ function reload_theme() { $style = null; $theme = $_CONFIG['theme']; + $theme_file = __DIR__."/../themes/{$theme}.theme"; if (!file_exists($theme_file)) { printf("Error: The theme %s could not be found at %s\n", $theme, $theme_file); @@ -106,19 +107,34 @@ function reload_theme() { $vars = []; $rules = []; $rule = null; + $pragma_256color = false; foreach ($data as $line) { - if (strpos('$pragma(',$line)!==false) { - printf("pragma: %s\n", $line); - } elseif (strpos('$set(',$line)!==false) { - printf("set: %s\n", $line); + if (substr($line,0,2)=="//") { + continue; + } elseif (preg_match("/\\\$pragma\((.+?)\)/", $line, $match)) { + switch ($match[1]) { + case '256color': + $pragma_256color = true; + break; + } + } elseif (preg_match("/\\\$set\((.+?),(.+?)\)/", $line, $match)) { + $vars[$match[1]] = $match[2]; } elseif (strpos($line,'{')!==false) { $rule = trim($line,'{ '); $rules[$rule] = []; } elseif ($line == '}') { $rule = null; } else { + if (!$rule) { + printf("Unexpected %s\n", $line); + continue; + } list($k,$v) = array_map('trim',explode(":",$line)); - $rules[$rule][$k] = trim($v,'; '); + $val = trim($v,'; '); + if ($val[0]=='"') { $val = trim($val,'"'); } + elseif ($val[0]=="'") { $val = trim($val,"'"); } + elseif ($val[0]=="%") { $val = $vars[substr($val,1)]; } + $rules[$rule][$k] = $val; } } diff --git a/lib/bootstrap.php b/lib/bootstrap.php index c194454..6c98793 100644 --- a/lib/bootstrap.php +++ b/lib/bootstrap.php @@ -1,7 +1,7 @@ getChildrenByTagName('items'); + $items = $root->getChildrenByTagName('items')[0]; if (!$items) { fprintf(STDERR, "Error: No items defined in configuration file!\n"); exit(1); @@ -51,6 +51,12 @@ function config_read() { $name, $type, $attr ]; } + + $theme = $root->getChildrenByTagName('theme'); + $theme = end($theme); + if ($theme) { + $_CONFIG['theme'] = $theme->getValue(); + } } function config_write() { diff --git a/lib/generate.php b/lib/generate.php index 19c6328..e8aac39 100644 --- a/lib/generate.php +++ b/lib/generate.php @@ -115,11 +115,13 @@ function generate(...$items) { "BR_CYAN" => 14, "BR_WHITE" => 15, "BOLD" => 1, + "COLOR256" => 256, + "COLOR24M" => 1024, ]; foreach ($colors as $name=>$index) define($name, $index); function color($string) { - + if (is_int($string)) return $string; switch ($string) { case 'none': return NONE; case 'black': return BLACK; @@ -138,6 +140,15 @@ function color($string) { case 'bright-magenta': return BR_MAGENTA; case 'bright-cyan': return BR_CYAN; case 'bright-white': return BR_WHITE; + default: + if ($string[0]=='#') { + $r = floor(hexdec(substr($string,1,2)) / 42); + $g = floor(hexdec(substr($string,3,2)) / 42); + $b = floor(hexdec(substr($string,5,2)) / 42); + $a = COLOR256 + 16 + 36*$r + 6*$g + $b; + // $a = COLOR24M + $r<<16 + $g<<8 + $b; + return $a; + } } return NONE; } @@ -145,10 +156,32 @@ function color($string) { function style($fg=NONE,$bg=NONE,$attr=NONE) { $sgr=[]; if ($fg>NONE) { - $sgr[] = ($fg>=8)?82+$fg:30+$fg; + if ($fg>=COLOR24M) { + $sgr[] = 38; $sgr[] = 2; + $fg = $fg - COLOR24M; + $sgr[] = $fg>>16 & 0xFF; + $sgr[] = $fg>>8 & 0xFF; + $sgr[] = $fg & 0xFF; + } elseif ($fg>=COLOR256) { + $sgr[] = 38; $sgr[] = 5; + $sgr[] = $fg - COLOR256; + } else { + $sgr[] = ($fg>=8)?82+$fg:30+$fg; + } } if ($bg>NONE) { - $sgr[] = ($bg>=8)?92+$bg:40+$bg; + if ($fg>=COLOR24M) { + $sgr[] = 48; $sgr[] = 2; + $bg = $bg - COLOR24M; + $sgr[] = $bg>>16 & 0xFF; + $sgr[] = $bg>>8 & 0xFF; + $sgr[] = $bg & 0xFF; + } elseif ($fg>=COLOR256) { + $sgr[] = 48; $sgr[] = 5; + $sgr[] = $bg - COLOR256; + } else { + $sgr[] = ($bg>=8)?92+$bg:40+$bg; + } } if ($attr>0) { if (($attr & BOLD) == BOLD) $sgr[] = "1";