Skip to content

Commit

Permalink
Import color theme from r2 (#163)
Browse files Browse the repository at this point in the history
* Import color theme from r2

* Improve rgb: color parsing and add missing ;

* Remove hardcoded option, always use r2 colors

* Make it work without r2cmd

* Remove dead code

* Fix unterminated string

* Fuck this keyboard

* Dead code
  • Loading branch information
radare authored and wargio committed May 20, 2019
1 parent 71ed8fc commit 22efa79
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
26 changes: 20 additions & 6 deletions libdec/colors/ansi.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,30 @@
white: [37, 39],
gray: [90, 39],
};
var Color = function(name) {
if (!__colors[name]) {
throw new Error('Invalid name: ' + name);
function pair(name, n) {
if (name.length === 6) {
n *= 2;
return parseInt(name.substring (n, n + 2), 16);
}
return parseInt(name.substring (n, n + 1), 16) << 4;
}
var Color = function(name) {
var fn = function(x) {
var o = arguments.callee;
return o.open + x + o.close;
};
fn.open = '\u001b[' + __colors[name][0] + 'm';
fn.close = '\u001b[' + __colors[name][1] + 'm';
if (name.startsWith('rgb:')) {
name = name.substring (4);
const str = '\u001b[38;2;'+ pair(name, 0) + ';' + pair(name, 1) + ';' + pair(name, 2);
fn.open = '\u001b[' + str + 'm';
fn.close = '\u001b[0m';
} else {
if (!__colors[name]) {
throw new Error('Invalid name: ' + name);
}
fn.open = '\u001b[' + __colors[name][0] + 'm';
fn.close = '\u001b[' + __colors[name][1] + 'm';
}
return fn;
};
Color.make = function(theme) {
Expand All @@ -47,4 +61,4 @@
return g;
};
return Color;
});
});
16 changes: 1 addition & 15 deletions libdec/colors/invalid.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,7 @@
*/

(function() {
var __colors = [
'black',
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
'white',
'gray',
];
var Color = function(name) {
if (__colors.indexOf(name) < 0) {
throw new Error('Invalid name: ' + name);
}
var fn = function(x) {
return x;
};
Expand All @@ -44,4 +30,4 @@
return g;
};
return Color;
});
});
32 changes: 21 additions & 11 deletions libdec/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@
*/

(function() {
function initializeColors() {
const config = {};
const colors = r2cmd? r2cmd("ec*"): '';
colors.split('\n').forEach(function(line) {
const tok = line.split(' ');
config[tok[1]] = tok[2];
});
return {
"callname": config['call'] || 'gray',
"integers": config['num'] || 'cyan',
"comment": config['comment'] || 'red',
"labels": config['flag'] || ' green',
"types": config['func_var_type'] || 'green',
"macro": config['cjmp'] || 'yellow',
"flow": config['flow'] || 'magenta',
"text": config['usrcmt'] || 'yellow',
};
}
//const _autoregex = /(\bif\b|\belse\b|\bwhile\b|\bfor\b|\bdo\b|\breturn\b|[ui]+nt[123468]+\_t|\bvoid\b|\bconst\b|\bsizeof\b|\bfloat\b|\bdouble\b|0x[0-9A-Fa-f]+|\b\d+\b)/g

/**
Expand All @@ -31,16 +49,8 @@

// const defaulttheme = JSON.parse(include('themes/default.json'));
// Just to be sure this won't impact anybody..
const defaulttheme = {
"callname": "gray",
"integers": "cyan",
"comment": "red",
"labels": "green",
"types": "green",
"macro": "yellow",
"flow": "magenta",
"text": "yellow"
};

const defaulttheme = initializeColors();

var colortheme = defaulttheme;

Expand Down Expand Up @@ -192,4 +202,4 @@
this.auto = _colorize_text;
this.html = _htmlize;
};
});
});

0 comments on commit 22efa79

Please sign in to comment.