fdisk: add -L to colorize output
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
a41309c227
commit
80a1712f54
|
@ -192,11 +192,31 @@ static int ask_offset(struct fdisk_context *cxt,
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void fputs_info(const char *msg, FILE *out)
|
static void fputs_info(const char *msg, FILE *out, char *buf, size_t bufsz)
|
||||||
{
|
{
|
||||||
if (info_count == 1)
|
if (info_count == 1)
|
||||||
fputc('\n', out);
|
fputc('\n', out);
|
||||||
|
|
||||||
|
if (msg && colors_wanted()) {
|
||||||
|
char *sep = _(": ");
|
||||||
|
char *p = strstr(msg, sep);
|
||||||
|
|
||||||
|
if (p) {
|
||||||
|
size_t sz = strlen(sep);
|
||||||
|
|
||||||
|
strncpy(buf, msg, bufsz);
|
||||||
|
buf[p - msg + sz] = '\0';
|
||||||
|
|
||||||
|
color_enable(UL_COLOR_BROWN);
|
||||||
|
fputs(buf, out);
|
||||||
|
color_disable();
|
||||||
|
fputs(p + sz, out);
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fputs(msg, out);
|
fputs(msg, out);
|
||||||
|
done:
|
||||||
fputc('\n', out);
|
fputc('\n', out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,16 +239,20 @@ int ask_callback(struct fdisk_context *cxt, struct fdisk_ask *ask,
|
||||||
return ask_offset(cxt, ask, buf, sizeof(buf));
|
return ask_offset(cxt, ask, buf, sizeof(buf));
|
||||||
case FDISK_ASKTYPE_INFO:
|
case FDISK_ASKTYPE_INFO:
|
||||||
info_count++;
|
info_count++;
|
||||||
fputs_info(fdisk_ask_print_get_mesg(ask), stdout);
|
fputs_info(fdisk_ask_print_get_mesg(ask), stdout, buf, sizeof(buf));
|
||||||
break;
|
break;
|
||||||
case FDISK_ASKTYPE_WARNX:
|
case FDISK_ASKTYPE_WARNX:
|
||||||
|
color_fenable(UL_COLOR_RED, stderr);
|
||||||
fputs(fdisk_ask_print_get_mesg(ask), stderr);
|
fputs(fdisk_ask_print_get_mesg(ask), stderr);
|
||||||
|
color_fdisable(stderr);
|
||||||
fputc('\n', stderr);
|
fputc('\n', stderr);
|
||||||
break;
|
break;
|
||||||
case FDISK_ASKTYPE_WARN:
|
case FDISK_ASKTYPE_WARN:
|
||||||
|
color_fenable(UL_COLOR_RED, stderr);
|
||||||
fputs(fdisk_ask_print_get_mesg(ask), stderr);
|
fputs(fdisk_ask_print_get_mesg(ask), stderr);
|
||||||
errno = fdisk_ask_print_get_errno(ask);
|
errno = fdisk_ask_print_get_errno(ask);
|
||||||
fprintf(stderr, ": %m\n");
|
fprintf(stderr, ": %m\n");
|
||||||
|
color_fdisable(stderr);
|
||||||
break;
|
break;
|
||||||
case FDISK_ASKTYPE_YESNO:
|
case FDISK_ASKTYPE_YESNO:
|
||||||
fputc('\n', stdout);
|
fputc('\n', stdout);
|
||||||
|
|
|
@ -326,9 +326,11 @@ int print_fdisk_menu(struct fdisk_context *cxt)
|
||||||
while ((e = next_menu_entry(cxt, &mc))) {
|
while ((e = next_menu_entry(cxt, &mc))) {
|
||||||
if (IS_MENU_HID(e))
|
if (IS_MENU_HID(e))
|
||||||
continue; /* hidden entry */
|
continue; /* hidden entry */
|
||||||
if (IS_MENU_SEP(e))
|
if (IS_MENU_SEP(e)) {
|
||||||
|
color_enable(UL_COLOR_BOLD);
|
||||||
printf("\n %s\n", _(e->title));
|
printf("\n %s\n", _(e->title));
|
||||||
else
|
color_disable();
|
||||||
|
} else
|
||||||
printf(" %c %s\n", e->key, _(e->title));
|
printf(" %c %s\n", e->key, _(e->title));
|
||||||
}
|
}
|
||||||
fputc('\n', stdout);
|
fputc('\n', stdout);
|
||||||
|
|
|
@ -218,6 +218,10 @@ A reasonable value is 63. This option is DEPRECATED.
|
||||||
.BI \-h
|
.BI \-h
|
||||||
Display a help text and exit.
|
Display a help text and exit.
|
||||||
.TP
|
.TP
|
||||||
|
.BI \-L[=\fIwhen\fR]
|
||||||
|
Colorize output in interactive mode. The optional argument \fIwhen\fP can
|
||||||
|
be \fBauto\fR, \fBnever\fR or \fBalways\fR. The default is \fBauto\fR.
|
||||||
|
.TP
|
||||||
.B \-l
|
.B \-l
|
||||||
List the partition tables for the specified devices and then exit.
|
List the partition tables for the specified devices and then exit.
|
||||||
If no devices are given, those mentioned in
|
If no devices are given, those mentioned in
|
||||||
|
|
|
@ -60,6 +60,8 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
|
||||||
fputs(_(" -b <size> sector size (512, 1024, 2048 or 4096)\n"), out);
|
fputs(_(" -b <size> sector size (512, 1024, 2048 or 4096)\n"), out);
|
||||||
fputs(_(" -c[=<mode>] compatible mode: 'dos' or 'nondos' (default)\n"), out);
|
fputs(_(" -c[=<mode>] compatible mode: 'dos' or 'nondos' (default)\n"), out);
|
||||||
fputs(_(" -h print this help text\n"), out);
|
fputs(_(" -h print this help text\n"), out);
|
||||||
|
fputs(_(" -c[=<mode>] compatible mode: 'dos' or 'nondos' (default)\n"), out);
|
||||||
|
fputs(_(" -L[=<when>] colorize output (auto, always or never)\n"), out);
|
||||||
fputs(_(" -u[=<unit>] display units: 'cylinders' or 'sectors' (default)\n"), out);
|
fputs(_(" -u[=<unit>] display units: 'cylinders' or 'sectors' (default)\n"), out);
|
||||||
fputs(_(" -v print program version\n"), out);
|
fputs(_(" -v print program version\n"), out);
|
||||||
fputs(_(" -C <number> specify the number of cylinders\n"), out);
|
fputs(_(" -C <number> specify the number of cylinders\n"), out);
|
||||||
|
@ -341,6 +343,7 @@ enum {
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int i, c, act = ACT_FDISK;
|
int i, c, act = ACT_FDISK;
|
||||||
|
int colormode = UL_COLORMODE_AUTO;
|
||||||
struct fdisk_context *cxt;
|
struct fdisk_context *cxt;
|
||||||
|
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
|
@ -355,7 +358,7 @@ int main(int argc, char **argv)
|
||||||
|
|
||||||
fdisk_context_set_ask(cxt, ask_callback, NULL);
|
fdisk_context_set_ask(cxt, ask_callback, NULL);
|
||||||
|
|
||||||
while ((c = getopt(argc, argv, "b:c::C:hH:lsS:u::vV")) != -1) {
|
while ((c = getopt(argc, argv, "b:c::C:hH:lL::sS:u::vV")) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'b':
|
case 'b':
|
||||||
{
|
{
|
||||||
|
@ -402,6 +405,11 @@ int main(int argc, char **argv)
|
||||||
case 'l':
|
case 'l':
|
||||||
act = ACT_LIST;
|
act = ACT_LIST;
|
||||||
break;
|
break;
|
||||||
|
case 'L':
|
||||||
|
if (optarg)
|
||||||
|
colormode = colormode_or_err(optarg,
|
||||||
|
_("unsupported color mode"));
|
||||||
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
act = ACT_SHOWSIZE;
|
act = ACT_SHOWSIZE;
|
||||||
break;
|
break;
|
||||||
|
@ -454,13 +462,17 @@ int main(int argc, char **argv)
|
||||||
if (argc-optind != 1)
|
if (argc-optind != 1)
|
||||||
usage(stderr);
|
usage(stderr);
|
||||||
|
|
||||||
|
colors_init(colormode);
|
||||||
|
|
||||||
if (fdisk_context_assign_device(cxt, argv[optind], 0) != 0)
|
if (fdisk_context_assign_device(cxt, argv[optind], 0) != 0)
|
||||||
err(EXIT_FAILURE, _("cannot open %s"), argv[optind]);
|
err(EXIT_FAILURE, _("cannot open %s"), argv[optind]);
|
||||||
|
|
||||||
/* Here starts interactive mode, use fdisk_{warn,info,..} functions */
|
/* Here starts interactive mode, use fdisk_{warn,info,..} functions */
|
||||||
fdisk_info(cxt, _("Welcome to fdisk (%s).\n\n"
|
color_enable(UL_COLOR_GREEN);
|
||||||
"Changes will remain in memory only, until you decide to write them.\n"
|
fdisk_info(cxt, _("Welcome to fdisk (%s).\n"), PACKAGE_STRING);
|
||||||
"Be careful before using the write command.\n"), PACKAGE_STRING);
|
color_disable();
|
||||||
|
fdisk_info(cxt, _("Changes will remain in memory only, until you decide to write them.\n"
|
||||||
|
"Be careful before using the write command.\n"));
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
|
||||||
if (!fdisk_dev_has_disklabel(cxt)) {
|
if (!fdisk_dev_has_disklabel(cxt)) {
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
*/
|
*/
|
||||||
#include "fdiskP.h"
|
#include "fdiskP.h"
|
||||||
#include "blkdev.h"
|
#include "blkdev.h"
|
||||||
|
#include "colors.h"
|
||||||
|
|
||||||
extern void toggle_units(struct fdisk_context *cxt);
|
extern void toggle_units(struct fdisk_context *cxt);
|
||||||
|
|
||||||
|
|
|
@ -53,10 +53,25 @@ extern int colormode_or_err(const char *str, const char *errmsg);
|
||||||
/* Initialize the global variable OUT_IS_TERM */
|
/* Initialize the global variable OUT_IS_TERM */
|
||||||
extern int colors_init(int mode);
|
extern int colors_init(int mode);
|
||||||
|
|
||||||
|
/* Returns 1 or 0 */
|
||||||
|
extern int colors_wanted(void);
|
||||||
|
|
||||||
/* Set the color to CLR_SCHEME */
|
/* Set the color to CLR_SCHEME */
|
||||||
extern void color_enable(const char *clr_scheme);
|
extern void color_fenable(const char *clr_scheme, FILE *f);
|
||||||
|
|
||||||
|
static inline void color_enable(const char *clr_scheme)
|
||||||
|
{
|
||||||
|
color_fenable(clr_scheme, stdout);
|
||||||
|
}
|
||||||
|
|
||||||
/* Reset colors to default */
|
/* Reset colors to default */
|
||||||
extern void color_disable(void);
|
extern void color_fdisable(FILE *f);
|
||||||
|
|
||||||
|
static inline void color_disable(void)
|
||||||
|
{
|
||||||
|
color_fdisable(stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* UTIL_LINUX_COLORS_H */
|
#endif /* UTIL_LINUX_COLORS_H */
|
||||||
|
|
15
lib/colors.c
15
lib/colors.c
|
@ -27,16 +27,21 @@ int colors_init(int mode)
|
||||||
return ul_color_term_ok;
|
return ul_color_term_ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
void color_enable(const char *color_scheme)
|
int colors_wanted(void)
|
||||||
{
|
{
|
||||||
if (ul_color_term_ok && color_scheme)
|
return ul_color_term_ok;
|
||||||
fputs(color_scheme, stdout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void color_disable(void)
|
void color_fenable(const char *color_scheme, FILE *f)
|
||||||
|
{
|
||||||
|
if (ul_color_term_ok && color_scheme)
|
||||||
|
fputs(color_scheme, f);
|
||||||
|
}
|
||||||
|
|
||||||
|
void color_fdisable(FILE *f)
|
||||||
{
|
{
|
||||||
if (ul_color_term_ok)
|
if (ul_color_term_ok)
|
||||||
fputs(UL_COLOR_RESET, stdout);
|
fputs(UL_COLOR_RESET, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
int colormode_from_string(const char *str)
|
int colormode_from_string(const char *str)
|
||||||
|
|
Loading…
Reference in New Issue