#include #include #include #include #define LEFT_TO_RIGHT 0 #define RIGHT_TO_LEFT 1 char *home_to_tilde(char *s, char *home) { char *new_str; char *index; int x, i, sx; sx = strlen(s); new_str = malloc(strlen(s) + 1); new_str[0] = 0; if (!strncmp(s, home, strlen(home))) { strcat(new_str, "~"); x = strlen(home); } else x = 0; for(i=(x) ? 1 : 0; s[x]; i++, x++) new_str[i] = s[x]; return new_str; } char *cwd_to_color(char *cwd, int color) { static char col[2048]; char *ptr; int i = 0; char g1[32], g2[32], g3[32], n[32]; sprintf(g1, "\\[\\033[1;30m\\]"); sprintf(g2, "\\[\\033[0;%im\\]", color); sprintf(g3, "\\[\\033[1;%im\\]", color); col[0] = 0; ptr = cwd; while(*ptr) { if (*ptr == '~' || *ptr == '/') { strcat(col, g3); i += strlen(g3); } col[i++] = *ptr; if (*ptr == '~' || *ptr == '/') { strcat(col, g2); i += strlen(g2); } ptr++; } return col; } char *gradient_text(char *s, int color, int direction) { static char user[1024]; if (direction == LEFT_TO_RIGHT) { snprintf(user, sizeof(user), "\\[\\033[1;37m\\]%c\\[\\033[1;%im\\]%c\\[\\033[0;%im\\]", s[0], color, s[1], color); s += 2; strcat(user, s); } else if (direction == RIGHT_TO_LEFT) { int x = strlen(s); snprintf(user, sizeof(user), "\\[\\033[0;%im\\]%s", color, s); snprintf(user + (strlen(user) - 2), sizeof(user) - (strlen(user) + 2), "\\[\\033[1;%im\\]%c\\[\\033[1;37m\\]%c", color, s[x - 2], s[x - 1]); } return user; } char *get_username(int color) { static char user[1024]; char *user_ptr; user_ptr = getlogin(); snprintf(user, sizeof(user), gradient_text(user_ptr, color, LEFT_TO_RIGHT)); /* snprintf(user, sizeof(user), "\\[\\033[1;37m\\]%c\\[\\033[1;%im\\]%c\\[\\033[0;%im\\]", user_ptr[0], color, user_ptr[1], color); user_ptr += 2; strcat(user, user_ptr);*/ return user; } char *get_hostname(int color) { static char host[1024]; char *host_ptr, host_buf[1024]; int len = 0; gethostname(host_buf, sizeof(host_buf)); snprintf(host, sizeof(host), gradient_text(host_buf, color, LEFT_TO_RIGHT)); /* snprintf(host, sizeof(host), "\\[\\033[1;%im\\]%c\\[\\033[0;%im\\]", color, host_buf[0], color); host_ptr = host_buf; host_ptr++; strcat(host, host_ptr);*/ return host; } char *real_calc_ps1(char *cwd, char *user, char *host, int color, char dm) { static char ps1[2048]; char g1[32], g2[32], g3[32], n[32]; sprintf(g1, "\\[\\033[1;30m\\]"); sprintf(g2, "\\[\\033[0;%im\\]", color); sprintf(g3, "\\[\\033[1;%im\\]", color); sprintf(n, "\\[\\033[0m\\]"); snprintf(ps1, sizeof(ps1), "%s%c%s%c%s%c %s %s@ %s %s%c %s%s %s%c%s%c%s%c\n%s%c%s%c%s%c %s> %s", g1, dm, g2, dm, g3, dm, user, g1, host, g1, dm, g2, cwd, g3, dm, g2, dm, g1, dm, g1, dm, g2, dm, g3, dm, g2, n); return ps1; } char *calc_ps1(int color, char dm) { char *cwd, *new_cwd, *color_cwd; char *user, *host, *ps1; cwd = get_current_dir_name(); if (!cwd) return NULL; new_cwd = home_to_tilde(cwd, getenv("HOME")); color_cwd = cwd_to_color(new_cwd, color); free(new_cwd); user = get_username(color); host = get_hostname(color); ps1 = real_calc_ps1(color_cwd, user, host, color, dm); return ps1; } int main(int argc, char **argv) { char *ps1; if (argc < 3) { printf("ERROR passing args to jastaps1!\n"); return 1; } ps1 = calc_ps1(atoi(argv[1]), argv[2][0]); if (!ps1) return 1; printf("%s\n", ps1); return 0; }