Skip to content

Commit

Permalink
Solarized theme with 256-color mode
Browse files Browse the repository at this point in the history
  • Loading branch information
noccy80 committed Dec 15, 2016
1 parent 0e59446 commit 33e6204
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 18 deletions.
18 changes: 10 additions & 8 deletions bin/phpl-edit
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
if (RELOAD) passthru("phpl-reload --theme");
} else {
print_help();
return;
}

config_write();

26 changes: 21 additions & 5 deletions bin/phpl-reload
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

define("PHPL_MODULES", __DIR__."/../modules");
define("PHPL_THEMEs", __DIR__."/../themes");
define("PHPL_THEMES", __DIR__."/../themes");
define("PHPL_CACHE_DIR", __DIR__."/../cache");
define("PHPL_STATE", __DIR__."/../cache/current");
define("PHPL_CACHE_PROMPT", __DIR__."/../cache/prompt.dat");
Expand Down
8 changes: 7 additions & 1 deletion lib/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function config_read() {

$root = SdlParser::parseFile(PHPL_CONFIG);

@list($items) = $root->getChildrenByTagName('items');
$items = $root->getChildrenByTagName('items')[0];
if (!$items) {
fprintf(STDERR, "Error: No items defined in configuration file!\n");
exit(1);
Expand All @@ -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() {
Expand Down
39 changes: 36 additions & 3 deletions lib/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -138,17 +140,48 @@ 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;
}

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";
Expand Down

0 comments on commit 33e6204

Please sign in to comment.