xbps-query: do not print ANSI escape codes if stdout is not a tty.

This commit is contained in:
Juan RP 2014-09-18 10:14:42 +02:00
parent 3b88cbb025
commit 2550d3d006
2 changed files with 39 additions and 17 deletions

3
NEWS
View File

@ -1,5 +1,8 @@
xbps-0.40 (???): xbps-0.40 (???):
* xbps-query(8): -S/--show mode no longer prints ANSI escape codes if stdout
is not a tty; suggested by @chneukirchen.
* Reduced considerably memory requirement when checking SHA256 hashes on * Reduced considerably memory requirement when checking SHA256 hashes on
files. Previous way of allocating heap memory as big as the file being files. Previous way of allocating heap memory as big as the file being
processed wasn't a great idea. processed wasn't a great idea.

View File

@ -41,7 +41,8 @@
static void static void
print_value_obj(const char *keyname, xbps_object_t obj, print_value_obj(const char *keyname, xbps_object_t obj,
const char *indent, bool raw) const char *indent, const char *bold,
const char *reset, bool raw)
{ {
xbps_array_t allkeys; xbps_array_t allkeys;
xbps_object_t obj2, keysym; xbps_object_t obj2, keysym;
@ -54,12 +55,12 @@ print_value_obj(const char *keyname, xbps_object_t obj,
switch (xbps_object_type(obj)) { switch (xbps_object_type(obj)) {
case XBPS_TYPE_STRING: case XBPS_TYPE_STRING:
if (!raw) if (!raw)
printf("%s%s%s%s: ", indent, _BOLD, keyname, _RESET); printf("%s%s%s%s: ", indent, bold, keyname, reset);
printf("%s\n", xbps_string_cstring_nocopy(obj)); printf("%s\n", xbps_string_cstring_nocopy(obj));
break; break;
case XBPS_TYPE_NUMBER: case XBPS_TYPE_NUMBER:
if (!raw) if (!raw)
printf("%s%s%s%s: ", indent, _BOLD, keyname, _RESET); printf("%s%s%s%s: ", indent, bold, keyname, reset);
if (xbps_humanize_number(size, if (xbps_humanize_number(size,
(int64_t)xbps_number_unsigned_integer_value(obj)) == -1) (int64_t)xbps_number_unsigned_integer_value(obj)) == -1)
printf("%ju\n", printf("%ju\n",
@ -69,12 +70,12 @@ print_value_obj(const char *keyname, xbps_object_t obj,
break; break;
case XBPS_TYPE_BOOL: case XBPS_TYPE_BOOL:
if (!raw) if (!raw)
printf("%s%s%s%s: ", indent, _BOLD, keyname, _RESET); printf("%s%s%s%s: ", indent, bold, keyname, reset);
printf("%s\n", xbps_bool_true(obj) ? "yes" : "no"); printf("%s\n", xbps_bool_true(obj) ? "yes" : "no");
break; break;
case XBPS_TYPE_ARRAY: case XBPS_TYPE_ARRAY:
if (!raw) if (!raw)
printf("%s%s%s%s:\n", indent, _BOLD, keyname, _RESET); printf("%s%s%s%s:\n", indent, bold, keyname, reset);
for (unsigned int i = 0; i < xbps_array_count(obj); i++) { for (unsigned int i = 0; i < xbps_array_count(obj); i++) {
obj2 = xbps_array_get(obj, i); obj2 = xbps_array_get(obj, i);
if (xbps_object_type(obj2) == XBPS_TYPE_STRING) { if (xbps_object_type(obj2) == XBPS_TYPE_STRING) {
@ -82,7 +83,7 @@ print_value_obj(const char *keyname, xbps_object_t obj,
printf("%s%s%s\n", indent, !raw ? "\t" : "", printf("%s%s%s\n", indent, !raw ? "\t" : "",
value); value);
} else { } else {
print_value_obj(keyname, obj2, " ", raw); print_value_obj(keyname, obj2, " ", bold, reset, raw);
} }
} }
break; break;
@ -92,7 +93,7 @@ print_value_obj(const char *keyname, xbps_object_t obj,
keysym = xbps_array_get(allkeys, i); keysym = xbps_array_get(allkeys, i);
ksymname = xbps_dictionary_keysym_cstring_nocopy(keysym); ksymname = xbps_dictionary_keysym_cstring_nocopy(keysym);
obj2 = xbps_dictionary_get_keysym(obj, keysym); obj2 = xbps_dictionary_get_keysym(obj, keysym);
print_value_obj(ksymname, obj2, " ", raw); print_value_obj(ksymname, obj2, " ", bold, reset, raw);
} }
xbps_object_release(allkeys); xbps_object_release(allkeys);
if (raw) if (raw)
@ -101,7 +102,7 @@ print_value_obj(const char *keyname, xbps_object_t obj,
case XBPS_TYPE_DATA: case XBPS_TYPE_DATA:
if (!raw) { if (!raw) {
xbps_humanize_number(size, (int64_t)xbps_data_size(obj)); xbps_humanize_number(size, (int64_t)xbps_data_size(obj));
printf("%s%s%s%s: %s\n", indent, _BOLD, keyname, _RESET, size); printf("%s%s%s%s: %s\n", indent, bold, keyname, reset, size);
} else { } else {
FILE *f; FILE *f;
char buf[BUFSIZ-1]; char buf[BUFSIZ-1];
@ -110,7 +111,7 @@ print_value_obj(const char *keyname, xbps_object_t obj,
data = xbps_data_data(obj); data = xbps_data_data(obj);
f = fmemopen(data, xbps_data_size(obj), "r"); f = fmemopen(data, xbps_data_size(obj), "r");
assert(f); assert(f);
while (fgets(buf, BUFSIZ-1, f)) while (fgets(buf, sizeof(buf), f))
printf("%s", buf); printf("%s", buf);
fclose(f); fclose(f);
free(data); free(data);
@ -127,13 +128,23 @@ void
show_pkg_info_one(xbps_dictionary_t d, const char *keys) show_pkg_info_one(xbps_dictionary_t d, const char *keys)
{ {
xbps_object_t obj; xbps_object_t obj;
const char *bold, *reset;
char *key, *p, *saveptr; char *key, *p, *saveptr;
int v_tty = isatty(STDOUT_FILENO);
if (v_tty) {
bold = _BOLD;
reset = _RESET;
} else {
bold = "";
reset = "";
}
if (strchr(keys, ',') == NULL) { if (strchr(keys, ',') == NULL) {
obj = xbps_dictionary_get(d, keys); obj = xbps_dictionary_get(d, keys);
if (obj == NULL) if (obj == NULL)
return; return;
print_value_obj(keys, obj, NULL, true); print_value_obj(keys, obj, NULL, bold, reset, true);
return; return;
} }
key = strdup(keys); key = strdup(keys);
@ -144,18 +155,17 @@ show_pkg_info_one(xbps_dictionary_t d, const char *keys)
obj = xbps_dictionary_get(d, p); obj = xbps_dictionary_get(d, p);
if (obj == NULL) if (obj == NULL)
continue; continue;
print_value_obj(p, obj, NULL, true); print_value_obj(p, obj, NULL, bold, reset, true);
} }
free(key); free(key);
} }
static void static void
print_srcrevs(const char *keyname, xbps_string_t obj) print_srcrevs(const char *keyname, xbps_string_t obj, const char *bold, const char *reset)
{ {
const char *str = xbps_string_cstring_nocopy(obj); const char *str = xbps_string_cstring_nocopy(obj);
/* parse string appending a \t after EOL */ printf("%s%s%s:\n ", bold, keyname, reset);
printf("%s%s%s:\n ", _BOLD, keyname, _RESET);
for (unsigned int i = 0; i < strlen(str); i++) { for (unsigned int i = 0; i < strlen(str); i++) {
if (str[i] == '\n') if (str[i] == '\n')
printf("\n "); printf("\n ");
@ -170,7 +180,16 @@ show_pkg_info(xbps_dictionary_t dict)
{ {
xbps_array_t all_keys; xbps_array_t all_keys;
xbps_object_t obj, keysym; xbps_object_t obj, keysym;
const char *keyname; const char *keyname, *bold, *reset;
int v_tty = isatty(STDOUT_FILENO);
if (v_tty) {
bold = _BOLD;
reset = _RESET;
} else {
bold = "";
reset = "";
}
all_keys = xbps_dictionary_all_keys(dict); all_keys = xbps_dictionary_all_keys(dict);
for (unsigned int i = 0; i < xbps_array_count(all_keys); i++) { for (unsigned int i = 0; i < xbps_array_count(all_keys); i++) {
@ -186,11 +205,11 @@ show_pkg_info(xbps_dictionary_t dict)
/* special case for source-revisions obj */ /* special case for source-revisions obj */
if (strcmp(keyname, "source-revisions") == 0) { if (strcmp(keyname, "source-revisions") == 0) {
print_srcrevs(keyname, obj); print_srcrevs(keyname, obj, bold, reset);
continue; continue;
} }
/* anything else */ /* anything else */
print_value_obj(keyname, obj, NULL, false); print_value_obj(keyname, obj, NULL, bold, reset, false);
} }
} }