-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add colors to p11-kit command-line tools
- adds URL highlighting to trust list command - adds colors to trust list command - adds colors to p11-kit list-modules command Signed-off-by: Zoltan Fridrich <[email protected]>
- Loading branch information
1 parent
2fbe188
commit 1b14bfa
Showing
7 changed files
with
222 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ if get_option('test') | |
endif | ||
|
||
libp11_tool_sources = [ | ||
'print.c', | ||
'tool.c' | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright (c) 2023 Red Hat Inc. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* * Redistributions of source code must retain the above | ||
* copyright notice, this list of conditions and the | ||
* following disclaimer. | ||
* * Redistributions in binary form must reproduce the | ||
* above copyright notice, this list of conditions and | ||
* the following disclaimer in the documentation and/or | ||
* other materials provided with the distribution. | ||
* * The names of contributors to this software may not be | ||
* used to endorse or promote products derived from this | ||
* software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | ||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF | ||
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | ||
* DAMAGE. | ||
* | ||
* Author: Zoltan Fridrich <[email protected]> | ||
*/ | ||
|
||
#include "config.h" | ||
|
||
#include "print.h" | ||
|
||
#include <stdarg.h> | ||
#include <unistd.h> | ||
|
||
static const char * | ||
color_to_sgr (p11_color color) | ||
{ | ||
switch (color) { | ||
case P11_COLOR_BLACK: return "30"; | ||
case P11_COLOR_RED: return "31"; | ||
case P11_COLOR_GREEN: return "32"; | ||
case P11_COLOR_YELLOW: return "33"; | ||
case P11_COLOR_BLUE: return "34"; | ||
case P11_COLOR_MAGENTA: return "35"; | ||
case P11_COLOR_CYAN: return "36"; | ||
case P11_COLOR_WHITE: return "37"; | ||
default: return "0"; | ||
} | ||
} | ||
|
||
void | ||
p11_highlight_word (FILE *fp, | ||
const char *string) | ||
{ | ||
if (isatty (fileno (fp))) | ||
fprintf (fp, "\e]8;;%s\e\\\033[36m%s\033[0m\e]8;;\e\\\n", string, string); | ||
else | ||
fprintf (fp, "%s\n", string); | ||
} | ||
|
||
void | ||
p11_print_word (FILE *fp, | ||
const char *string, | ||
p11_color color, | ||
p11_font font) | ||
{ | ||
if (!isatty (fileno (fp))) { | ||
fputs (string, fp); | ||
return; | ||
} | ||
|
||
fprintf (fp, "\033[%s", color_to_sgr (color)); | ||
if (font & P11_FONT_BOLD) | ||
fprintf (fp, ";1"); | ||
if (font & P11_FONT_UNDERLINE) | ||
fprintf (fp, ";4"); | ||
fprintf (fp, "m%s\033[0m", string); | ||
} | ||
|
||
void | ||
p11_print_value (FILE *fp, | ||
size_t indent, | ||
const char *key, | ||
p11_color color, | ||
p11_font font, | ||
const char *value_fmt, | ||
...) | ||
{ | ||
size_t i; | ||
va_list args; | ||
|
||
for (i = 0; i < indent; ++i) | ||
fputc (' ', fp); | ||
|
||
p11_print_word (fp, key, color, font); | ||
p11_print_word (fp, ": ", color, font); | ||
|
||
va_start (args, value_fmt); | ||
vfprintf (fp, value_fmt, args); | ||
va_end (args); | ||
fputc ('\n', fp); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (c) 2023 Red Hat Inc. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* * Redistributions of source code must retain the above | ||
* copyright notice, this list of conditions and the | ||
* following disclaimer. | ||
* * Redistributions in binary form must reproduce the | ||
* above copyright notice, this list of conditions and | ||
* the following disclaimer in the documentation and/or | ||
* other materials provided with the distribution. | ||
* * The names of contributors to this software may not be | ||
* used to endorse or promote products derived from this | ||
* software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | ||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF | ||
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | ||
* DAMAGE. | ||
* | ||
* Author: Zoltan Fridrich <[email protected]> | ||
*/ | ||
|
||
#ifndef P11_PRINT_H_ | ||
#define P11_PRINT_H_ | ||
|
||
#include <stdio.h> | ||
|
||
#define P11_PRINT_VALUE_BOLD(INDENT, KEY, VALUE) \ | ||
p11_print_value (stdout, INDENT, KEY, P11_COLOR_DEFAULT, P11_FONT_BOLD, VALUE) | ||
|
||
typedef enum { | ||
P11_COLOR_DEFAULT, | ||
P11_COLOR_BLACK, | ||
P11_COLOR_RED, | ||
P11_COLOR_GREEN, | ||
P11_COLOR_YELLOW, | ||
P11_COLOR_BLUE, | ||
P11_COLOR_MAGENTA, | ||
P11_COLOR_CYAN, | ||
P11_COLOR_WHITE | ||
} p11_color; | ||
|
||
typedef enum { | ||
P11_FONT_DEFAULT = 0, | ||
P11_FONT_BOLD = 1<<0, | ||
P11_FONT_UNDERLINE = 1<<1 | ||
} p11_font; | ||
|
||
void p11_highlight_word (FILE *fp, | ||
const char *string); | ||
|
||
void p11_print_word (FILE *fp, | ||
const char *string, | ||
p11_color color, | ||
p11_font font); | ||
|
||
void p11_print_value (FILE *fp, | ||
size_t indent, | ||
const char *key, | ||
p11_color color, | ||
p11_font font, | ||
const char *value_fmt, | ||
...); | ||
|
||
#endif /* P11_PRINT_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.