replaced magic numbers with macros

This commit is contained in:
Martinez 2015-04-07 13:18:11 +02:00
parent 6da1698762
commit 313a2dee99
2 changed files with 79 additions and 28 deletions

18
256-xterm-colors.py Executable file
View file

@ -0,0 +1,18 @@
#! /usr/bin/env python3
# encoding: utf-8
import sys
def out(text):
sys.stdout.write(text)
if __name__ == '__main__':
for i in range(256):
out('\033[48;5;%dm\033[38;5;15m %03d ' %(i, i))
out('\033[48;5;0m\033[38;5;%dm %03d\t' %(i, i))
if (i + 1) % 8 == 0 if (i + 1) <= 16 else ((i + 1) - 16) % 6 == 0:
out('\n')
if (i + 1) % 16 == 0 if (i + 1) <= 16 else ((i + 1) - 16) % 36 == 0:
out('\n')

View file

@ -19,6 +19,38 @@
#define SYM_LEFTBLK "\ue0b2" #define SYM_LEFTBLK "\ue0b2"
#define SYM_LEFT "\ue0b3" #define SYM_LEFT "\ue0b3"
#define COL_DEFAULT_FG 7
#define COL_DEFAULT_BG 0
#define COL_LOGIN_USER_FG 202
#define COL_LOGIN_USER_BG 232
#define COL_LOGIN_ROOT_FG 232
#define COL_LOGIN_ROOT_BG 124
#define COL_SCREEN_FG 232
#define COL_SCREEN_BG 27
#define COL_PATH_FG 202
#define COL_PATH2_FG 232
#define COL_PATH_BG 232
#define COL_GIT_CLEAN_FG 232
#define COL_GIT_CLEAN_BG 88
#define COL_GIT_DIRTY_FG 232
#define COL_GIT_DIRTY_BG 46
#define COL_LOCK_FG 196
#define COL_TIME_FG 202
#define COL_TIME_BG 232
#define COL_LOAD_OK_FG 202
#define COL_LOAD_OK_BG 232
#define COL_LOAD_WARN_FG 232
#define COL_LOAD_WARN_BG 3
#define COL_LOAD_HIGH_FG 232
#define COL_LOAD_HIGH_BG 202
#define COL_LOAD_OVER_FG 15
#define COL_LOAD_OVER_BG 196
#define COL_PROMPT_OK_FG 232
#define COL_PROMPT_OK_BG 34
#define COL_ERR_FG 15
#define COL_ERR_BG 196
#define PATH_MAX 4096 #define PATH_MAX 4096
#ifndef uint #ifndef uint
@ -87,7 +119,7 @@ public:
this->segments = new Segment*[this->max]; this->segments = new Segment*[this->max];
this->set_colors(7, 0); this->set_colors(COL_DEFAULT_FG, COL_DEFAULT_BG);
} }
~Output(void) { ~Output(void) {
@ -176,7 +208,7 @@ void render_screen(Output *output) {
char *tmp = getenv("WINDOW"); char *tmp = getenv("WINDOW");
if (tmp) { if (tmp) {
output->push(":"); output->set_colors(COL_SCREEN_FG, COL_SCREEN_BG, SYM_RIGHTBLK);
output->push(tmp); output->push(tmp);
} }
} }
@ -210,11 +242,11 @@ void render_path(Output *output) {
} }
// render path with increasing background brightness // render path with increasing background brightness
uint fg = 202; uint fg = COL_PATH_FG;
uint bg = 233; uint bg = COL_PATH_BG;
for (uint i = 0; i < token_count; i++) { for (uint i = 0; i < token_count; i++) {
if (bg == 241) fg = 232; if (bg == 241) fg = COL_PATH2_FG;
if (bg < 255) { if (bg < 255) {
output->set_colors(fg, bg++, SYM_RIGHTBLK); output->set_colors(fg, bg++, SYM_RIGHTBLK);
@ -244,9 +276,9 @@ void render_git(Output *output) {
// so that I can check if there are unstaged changes // so that I can check if there are unstaged changes
if (system("git diff-files --quiet --ignore-submodules")) { if (system("git diff-files --quiet --ignore-submodules")) {
output->set_colors(232, 88, SYM_RIGHTBLK); output->set_colors(COL_GIT_CLEAN_FG, COL_GIT_CLEAN_BG, SYM_RIGHTBLK);
} else { } else {
output->set_colors(232, 46, SYM_RIGHTBLK); output->set_colors(COL_GIT_DIRTY_FG, COL_GIT_DIRTY_BG, SYM_RIGHTBLK);
} }
output->push(SYM_GIT); output->push(SYM_GIT);
@ -262,7 +294,9 @@ void render_git(Output *output) {
void render_lock(Output *output) { void render_lock(Output *output) {
if (access(".", W_OK) != 0) { if (access(".", W_OK) != 0) {
output->set_colors(196, 232, SYM_RIGHTBLK); // output->set_colors(COL_LOCK_FG, COL_LOCK_BG, SYM_RIGHTBLK);
output->_push_color(COL_LOCK_FG);
output->push(" ");
output->push(SYM_LOCK); output->push(SYM_LOCK);
} }
} }
@ -301,24 +335,24 @@ void render_sysload(Output *output) {
if (getloadavg(&load, 1) == 1) { if (getloadavg(&load, 1) == 1) {
if (load > 12) { if (load > 12) {
bg = 196; fg = COL_LOAD_OVER_FG;
fg = 15; bg = COL_LOAD_OVER_BG;
} else if (load > 8) { } else if (load > 8) {
bg = 88; fg = COL_LOAD_HIGH_FG;
fg = 15; bg = COL_LOAD_HIGH_BG;
} else if (load > 4) { } else if (load > 4) {
bg = 3; fg = COL_LOAD_WARN_FG;
fg = 232; bg = COL_LOAD_WARN_BG;
} else { } else {
bg = 34; fg = COL_LOAD_OK_FG;
fg = 232; bg = COL_LOAD_OK_BG;
} }
output->set_colors(fg, bg, SYM_RIGHTBLK); output->set_colors(fg, bg, SYM_RIGHTBLK);
sprintf(tmp, "%.2f", load); sprintf(tmp, "%.2f", load);
output->push(tmp); output->push(tmp);
} else { } else {
output->set_colors(15, 196, SYM_RIGHTBLK); output->set_colors(COL_ERR_FG, COL_ERR_BG, SYM_RIGHTBLK);
output->push("ERR"); output->push("ERR");
} }
} }
@ -344,36 +378,35 @@ int main(int argc, char **argv) {
char buf[PATH_MAX]; char buf[PATH_MAX];
if (config.is_root) { if (config.is_root) {
top_left.set_colors(232, 124); // red bg top_left.set_colors(COL_LOGIN_ROOT_FG, COL_LOGIN_ROOT_BG);
} else { } else {
top_left.set_colors(232, 202); // aware orange bg top_left.set_colors(COL_LOGIN_USER_FG, COL_LOGIN_USER_BG);
} }
top_left.push(" "); top_left.push(" ");
top_left.push(getlogin()); top_left.push(getlogin());
top_left.push("@");
top_left.set_colors(202, 232, SYM_RIGHTBLK);
gethostname(buf, 256); gethostname(buf, 256);
top_left.push(buf); top_left.push(buf);
render_screen(&top_left); render_screen(&top_left);
render_path(&top_left); render_path(&top_left);
render_git(&top_left);
render_lock(&top_left); render_lock(&top_left);
render_git(&top_left);
top_left.set_colors(7, 0, SYM_RIGHTBLK); top_left.set_colors(COL_DEFAULT_FG, COL_DEFAULT_BG, SYM_RIGHTBLK);
/* -------------------------------------------------- /* --------------------------------------------------
* prompt: [last_exit_status:]$|# > * prompt: [last_exit_status:]$|# >
*/ */
if (config.last_exit_status) { if (config.last_exit_status) {
prompt.set_colors(7, 88); prompt.set_colors(COL_ERR_FG, COL_ERR_BG);
prompt.push(" "); prompt.push(" ");
prompt.push(config.last_exit_status); prompt.push(config.last_exit_status);
} else { } else {
prompt.set_colors(232, 34); prompt.set_colors(COL_PROMPT_OK_FG, COL_PROMPT_OK_BG);
prompt.push(" "); prompt.push(" ");
if (config.is_root) { if (config.is_root) {
@ -383,16 +416,16 @@ int main(int argc, char **argv) {
} }
} }
prompt.set_colors(7, 0, SYM_RIGHTBLK); prompt.set_colors(COL_DEFAULT_FG, COL_DEFAULT_BG, SYM_RIGHTBLK);
/* -------------------------------------------------- /* --------------------------------------------------
* top_right: < time < spaces < sysload * top_right: < time < spaces < sysload
*/ */
top_right.set_colors(232, 202, SYM_LEFTBLK, false); top_right.set_colors(COL_TIME_FG, COL_TIME_BG, SYM_LEFTBLK, false);
render_time(&top_right); render_time(&top_right);
render_sysload(&top_right); render_sysload(&top_right);
top_right.set_colors(7, 0, SYM_RIGHTBLK); top_right.set_colors(COL_DEFAULT_FG, COL_DEFAULT_BG, SYM_RIGHTBLK);
/* -------------------------------------------------- /* --------------------------------------------------
* output the prompt * output the prompt