i3-style
Version:
Make your i3wm config a little more stylish
192 lines (178 loc) • 5.76 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var _, matchBar, matchBarColors, matchWindowColors, mkBarColors, mkConfig, mkTheme, mkWindowColors, namer, stripConfig, templates;
_ = require('underscore');
namer = require('color-namer');
_.templateSettings = {
interpolate: /\{\{(.+?)\}\}/g
};
templates = {
windowColors: _.template("client.{{state}} {{border}} {{background}} {{text}} {{indicator}}"),
barColors: _.template("{{state}} {{border}} {{background}} {{text}}")
};
matchBar = /bar\s*{\s*\n([^}]*)\n}/g;
matchBarColors = /\s*colors\s*{[^}]*}/g;
matchWindowColors = /\s*client\.(?:focused|focused_inactive|unfocused|urgent)\s+.*/g;
stripConfig = function(config) {
return config.replace(matchWindowColors, '').replace(matchBarColors, '');
};
mkWindowColors = function(colors, windowColors) {
var result;
if (colors == null) {
colors = {};
}
result = [];
_.each(windowColors, function(parts, state) {
return result.push(templates.windowColors({
state: state,
border: colors[parts.border] || parts.border,
background: colors[parts.background] || parts.background,
text: colors[parts.text] || parts.text,
indicator: colors[parts.indicator] || parts.indicator
}));
});
return result.join('\n');
};
mkBarColors = function(colors, bar_colors) {
var result;
if (colors == null) {
colors = {};
}
result = [];
_.each(bar_colors, function(parts, state) {
if (_.isString(parts)) {
return result.push(state + " " + (colors[parts] || parts));
} else {
return result.push(templates.barColors({
state: state,
border: colors[parts.border] || parts.border,
background: colors[parts.background] || parts.background,
text: colors[parts.text] || parts.text
}));
}
});
return result.join("\n ");
};
mkConfig = function(theme, config) {
config = stripConfig(config);
config = config.replace(matchBar, function(match, inside) {
return "bar {\n " + (inside.trim()) + "\n colors {\n " + (mkBarColors(theme.colors, theme.bar_colors)) + "\n }\n}";
});
config += "\n" + (mkWindowColors(theme.colors, theme.window_colors)) + "\n";
return config;
};
mkTheme = function(config) {
var addColor, background, barColor, border, color, indicator, j, len, line, ref, ref1, ref2, ref3, stateBarInside, stateColorsInside, text, theme;
theme = {
meta: {
description: 'AUTOMATICALLY GENERATED THEME'
}
};
addColor = function(hex) {
var c, colorName, i;
if (!hex) {
return null;
}
if (theme.colors == null) {
theme.colors = {};
}
colorName = namer(hex).html[0].name;
if ((theme.colors[colorName] != null) && theme.colors[colorName] !== hex) {
i = 0;
while (true) {
i += 1;
c = "" + colorName + i;
if (!theme.colors[c] || theme.colors[c] === hex) {
colorName = c;
break;
}
}
}
theme.colors[colorName] = hex;
return colorName;
};
stateBarInside = false;
stateColorsInside = false;
ref = config.split('\n');
for (j = 0, len = ref.length; j < len; j++) {
line = ref[j];
line = line.replace(/^\s+|\s+$/g, '');
line = line.replace(/\s+/g, ' ');
if (line.indexOf('bar {') === 0) {
stateBarInside = true;
continue;
}
if (line.indexOf('colors {') === 0 && stateBarInside) {
stateColorsInside = true;
continue;
}
if (line.indexOf('}') === 0) {
if (stateColorsInside) {
stateColorsInside = false;
continue;
}
if (stateBarInside) {
stateBarInside = false;
continue;
}
}
if (line.indexOf('client.') === 0) {
color = line.split(' ');
color[0] = color[0].substring('client.'.length);
if ((ref1 = color[0]) === 'focused' || ref1 === 'focused_inactive' || ref1 === 'unfocused' || ref1 === 'urgent') {
border = addColor(color[1]);
background = addColor(color[2]);
text = addColor(color[3]);
indicator = addColor(color[4]);
if (theme.window_colors == null) {
theme.window_colors = {};
}
theme.window_colors[color[0]] = {};
if (border != null) {
theme.window_colors[color[0]].border = border;
}
if (background != null) {
theme.window_colors[color[0]].background = background;
}
if (text != null) {
theme.window_colors[color[0]].text = text;
}
if (indicator != null) {
theme.window_colors[color[0]].indicator = indicator;
}
}
continue;
}
if (stateColorsInside) {
barColor = line.split(' ');
if ((ref2 = barColor[0]) === 'separator' || ref2 === 'background' || ref2 === 'statusline') {
if (theme.bar_colors == null) {
theme.bar_colors = {};
}
theme.bar_colors[barColor[0]] = addColor(barColor[1]);
} else if ((ref3 = barColor[0]) === 'focused_workspace' || ref3 === 'active_workspace' || ref3 === 'inactive_workspace' || ref3 === 'urgent_workspace') {
border = addColor(barColor[1]);
background = addColor(barColor[2]);
text = addColor(barColor[3]);
if (theme.bar_colors == null) {
theme.bar_colors = {};
}
theme.bar_colors[barColor[0]] = {};
if (border != null) {
theme.bar_colors[barColor[0]].border = border;
}
if (background != null) {
theme.bar_colors[barColor[0]].background = background;
}
if (text != null) {
theme.bar_colors[barColor[0]].text = text;
}
}
continue;
}
}
return theme;
};
module.exports = {
mkConfig: mkConfig,
mkTheme: mkTheme
};