[pacman-dev] [PATCH 04/11] add color to callback

Daniel Wallace daniel.wallace at gatech.edu
Sun Aug 5 05:46:40 EDT 2012


Signed-off-by: Daniel Wallace <daniel.wallace at gatech.edu>
---
 src/pacman/callback.c | 164 +++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 128 insertions(+), 36 deletions(-)

diff --git a/src/pacman/callback.c b/src/pacman/callback.c
index 01c6b61..1c766e5 100644
--- a/src/pacman/callback.c
+++ b/src/pacman/callback.c
@@ -33,6 +33,7 @@
 /* pacman */
 #include "callback.h"
 #include "util.h"
+#include "color.h"
 #include "conf.h"
 
 /* download progress bar */
@@ -223,16 +224,28 @@ void cb_event(alpm_event_t event, void *data1, void *data2)
 			printf(_("generating %s with %s... "), (char *)data1, (char *)data2);
 			break;
 		case ALPM_EVENT_DELTA_PATCH_DONE:
-			printf(_("success!\n"));
+			if (config->color) {
+				color_printf(COLOR_GREEN_ALL, _("success!\n"));
+			} else {
+				printf(_("success!\n"));
+			}
 			break;
 		case ALPM_EVENT_DELTA_PATCH_FAILED:
-			printf(_("failed.\n"));
+			if (config->color) {
+				color_printf(COLOR_RED_ALL, _("failed.\n"));
+			} else {
+				printf(_("failed.\n"));
+			}
 			break;
 		case ALPM_EVENT_SCRIPTLET_INFO:
 			fputs((const char *)data1, stdout);
 			break;
 		case ALPM_EVENT_RETRIEVE_START:
-			printf(_(":: Retrieving packages ...\n"));
+			if (config->color) {
+				color_printf(COLOR_DOUBLECOLON, _(":: Retrieving packages ...\n"));
+			} else {
+				printf(_(":: Retrieving packages ...\n"));
+			}
 			break;
 		case ALPM_EVENT_DISKSPACE_START:
 			if(config->noprogressbar) {
@@ -266,32 +279,63 @@ void cb_question(alpm_question_t event, void *data1, void *data2,
 	switch(event) {
 		case ALPM_QUESTION_INSTALL_IGNOREPKG:
 			if(!config->op_s_downloadonly) {
-				*response = yesno(_(":: %s is in IgnorePkg/IgnoreGroup. Install anyway?"),
+				if (config->color) {
+					*response = color_yesno(COLOR_DOUBLECOLON, _(":: %s is in IgnorePkg/IgnoreGroup. Install anyway?"),
 								  alpm_pkg_get_name(data1));
+
+				} else {
+					*response = yesno(_(":: %s is in IgnorePkg/IgnoreGroup. Install anyway?"),
+								  alpm_pkg_get_name(data1));
+				}
 			} else {
 				*response = 1;
 			}
 			break;
 		case ALPM_QUESTION_REPLACE_PKG:
-			*response = yesno(_(":: Replace %s with %s/%s?"),
-					alpm_pkg_get_name(data1),
-					(char *)data3,
-					alpm_pkg_get_name(data2));
+			if (config->color) {
+				*response = color_yesno(COLOR_DOUBLECOLON,
+						_(":: Replace %s with %s/%s?"),
+						alpm_pkg_get_name(data1),
+						(char *)data3,
+						alpm_pkg_get_name(data2));
+			} else {
+				*response = yesno(_(":: Replace %s with %s/%s?"),
+						alpm_pkg_get_name(data1),
+						(char *)data3,
+						alpm_pkg_get_name(data2));
+			}
 			break;
 		case ALPM_QUESTION_CONFLICT_PKG:
 			/* data parameters: target package, local package, conflict (strings) */
 			/* print conflict only if it contains new information */
 			if(strcmp(data1, data3) == 0 || strcmp(data2, data3) == 0) {
-				*response = noyes(_(":: %s and %s are in conflict. Remove %s?"),
-						(char *)data1,
-						(char *)data2,
-						(char *)data2);
+				if (config->color) {
+					*response = color_noyes(COLOR_DOUBLECOLON,
+							_(":: %s and %s are in conflict. Remove %s?"),
+							(char *)data1,
+							(char *)data2,
+							(char *)data2);
+				} else {
+					*response = noyes(_(":: %s and %s are in conflict. Remove %s?"),
+							(char *)data1,
+							(char *)data2,
+							(char *)data2);
+				}
 			} else {
-				*response = noyes(_(":: %s and %s are in conflict (%s). Remove %s?"),
-						(char *)data1,
-						(char *)data2,
-						(char *)data3,
-						(char *)data2);
+				if (config->color) {
+					*response = color_noyes(COLOR_DOUBLECOLON,
+							_(":: %s and %s are in conflict (%s). Remove %s?"),
+							(char *)data1,
+							(char *)data2,
+							(char *)data3,
+							(char *)data2);
+				} else {
+					*response = noyes(_(":: %s and %s are in conflict (%s). Remove %s?"),
+							(char *)data1,
+							(char *)data2,
+							(char *)data3,
+							(char *)data2);
+				}
 			}
 			break;
 		case ALPM_QUESTION_REMOVE_PKGS:
@@ -304,10 +348,17 @@ void cb_question(alpm_question_t event, void *data1, void *data2,
 							(char *)alpm_pkg_get_name(i->data));
 					count++;
 				}
-				printf(_n(
-							":: The following package cannot be upgraded due to unresolvable dependencies:\n",
-							":: The following packages cannot be upgraded due to unresolvable dependencies:\n",
-							count));
+				if (config->color) {
+					color_printf(COLOR_DOUBLECOLON, _n(
+								":: The following package cannot be upgraded due to unresolvable dependencies:\n",
+								":: The following packages cannot be upgraded due to unresolvable dependencies:\n",
+								count));
+				} else {
+					printf(_n(
+								":: The following package cannot be upgraded due to unresolvable dependencies:\n",
+								":: The following packages cannot be upgraded due to unresolvable dependencies:\n",
+								count));
+				}
 				list_display("     ", namelist, getcols(fileno(stdout)));
 				printf("\n");
 				*response = noyes(_n(
@@ -322,8 +373,13 @@ void cb_question(alpm_question_t event, void *data1, void *data2,
 				alpm_list_t *providers = data1;
 				size_t count = alpm_list_count(providers);
 				char *depstring = alpm_dep_compute_string((alpm_depend_t *)data2);
-				printf(_(":: There are %zd providers available for %s:\n"), count,
-						depstring);
+				if (config->color) {
+					color_printf(COLOR_DOUBLECOLON, _(":: There are %zd providers available for %s:\n"), 
+							count, depstring);
+				} else {
+					printf(_(":: There are %zd providers available for %s:\n"), count,
+							depstring);
+				}
 				free(depstring);
 				select_display(providers);
 				*response = select_question(count);
@@ -331,18 +387,33 @@ void cb_question(alpm_question_t event, void *data1, void *data2,
 			break;
 		case ALPM_QUESTION_LOCAL_NEWER:
 			if(!config->op_s_downloadonly) {
-				*response = yesno(_(":: %s-%s: local version is newer. Upgrade anyway?"),
-						alpm_pkg_get_name(data1),
-						alpm_pkg_get_version(data1));
+				if (config->color) {
+					*response = color_yesno(COLOR_DOUBLECOLON,
+							_(":: %s-%s: local version is newer. Upgrade anyway?"),
+							alpm_pkg_get_name(data1),
+							alpm_pkg_get_version(data1));
+				} else {
+					*response = yesno(_(":: %s-%s: local version is newer. Upgrade anyway?"),
+							alpm_pkg_get_name(data1),
+							alpm_pkg_get_version(data1));
+				}
 			} else {
 				*response = 1;
 			}
 			break;
 		case ALPM_QUESTION_CORRUPTED_PKG:
-			*response = yesno(_(":: File %s is corrupted (%s).\n"
-						"Do you want to delete it?"),
-					(char *)data1,
-					alpm_strerror(*(alpm_errno_t *)data2));
+			if (config->color) {
+				*response = color_yesno(COLOR_DOUBLECOLON,
+						_(":: File %s is corrupted (%s).\n"
+							"Do you want to delete it?"),
+						(char *)data1,
+						alpm_strerror(*(alpm_errno_t *)data2));
+			} else {
+				*response = yesno(_(":: File %s is corrupted (%s).\n"
+							"Do you want to delete it?"),
+						(char *)data1,
+						alpm_strerror(*(alpm_errno_t *)data2));
+			}
 			break;
 		case ALPM_QUESTION_IMPORT_KEY:
 			{
@@ -356,8 +427,14 @@ void cb_question(alpm_question_t event, void *data1, void *data2,
 					revoked = " (revoked)";
 				}
 
-				*response = yesno(_(":: Import PGP key %d%c/%s, \"%s\", created: %s%s?"),
-						key->length, key->pubkey_algo, key->fingerprint, key->uid, created, revoked);
+				if (config->color) {
+					*response = color_yesno(COLOR_DOUBLECOLON,
+							_(":: Import PGP key %d%c/%s, \"%s\", created: %s%s?"),
+							key->length, key->pubkey_algo, key->fingerprint, key->uid, created, revoked);
+				} else {
+					*response = yesno(_(":: Import PGP key %d%c/%s, \"%s\", created: %s%s?"),
+							key->length, key->pubkey_algo, key->fingerprint, key->uid, created, revoked);
+				}
 			}
 			break;
 	}
@@ -490,8 +567,15 @@ void cb_progress(alpm_progress_t event, const char *pkgname, int percent,
 
 	}
 
-	printf("(%*ld/%*ld) %ls%-*s", digits, (unsigned long)current,
-			digits, (unsigned long)howmany, wcstr, padwid, "");
+	if (config->color) {
+		color_printf(COLOR_BLUE_ALL,
+				"(%*ld/%*ld)", digits, (unsigned long)current,
+				digits, (unsigned long)howmany);
+	} else {
+		printf("(%*ld/%*ld)", digits, (unsigned long)current,
+				digits, (unsigned long)howmany);
+	}
+	printf(" %ls%-*s", wcstr, padwid, "");
 
 	free(wcstr);
 
@@ -747,12 +831,20 @@ void cb_log(alpm_loglevel_t level, const char *fmt, va_list args)
 
 	if(on_progress) {
 		char *string = NULL;
-		pm_vasprintf(&string, level, fmt, args);
+		if (config->color) {
+			color_pm_vasprintf(&string, level, fmt, args);
+		} else {
+			pm_vasprintf(&string, level, fmt, args);
+		}
 		if(string != NULL) {
 			output = alpm_list_add(output, string);
 		}
 	} else {
-		pm_vfprintf(stderr, level, fmt, args);
+		if (config->color) {
+			color_pm_vfprintf(stderr, level, fmt, args);
+		} else {
+			pm_vfprintf(stderr, level, fmt, args);
+		}
 	}
 }
 
-- 
1.7.11.4



More information about the pacman-dev mailing list