Skip to content

Commit 231e09d

Browse files
committed
✨ Add --color option for greater control of output formatting
This option adheres to the --color convention used by many other commands, such as grep(1).
1 parent 88e2916 commit 231e09d

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

safe_hashes.sh

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ readonly UNDERLINE="\e[4m"
1414
readonly BOLD="\e[1m"
1515
readonly RESET="\e[0m"
1616

17+
color="auto"
18+
19+
use_formatting() {
20+
if [[ "${color}" == "always" ]]; then
21+
return 0
22+
fi
23+
[[ "${color}" == "auto" ]] && [[ -t 1 ]] && tput sgr0 >/dev/null 2>&1
24+
}
25+
1726
# Check the Bash version compatibility.
1827
if [[ "${BASH_VERSINFO[0]:-0}" -lt 4 ]]; then
1928
echo -e "${BOLD}${RED}Error: This script requires Bash 4.0 or higher!${RESET}"
@@ -196,7 +205,7 @@ usage() {
196205
Usage: $0 [--help] [--version] [--list-networks]
197206
--network <network> --address <address> [--nonce <nonce>]
198207
[--nested-safe-address <address>] [--nested-safe-nonce <nonce>]
199-
[--message <file>] [--interactive]
208+
[--message <file>] [--interactive] [--color <never|auto|always>]
200209
201210
Options:
202211
--help Display this help message
@@ -209,6 +218,7 @@ Options:
209218
--nested-safe-nonce <nonce> Specify the nonce for the nested Safe transaction (optional for transaction hashes)
210219
--message <file> Specify the message file (required for off-chain message hashes)
211220
--interactive Use the interactive mode (optional for transaction hashes)
221+
--color <never|auto|always> Specify whether color should be used in output
212222
213223
Example for transaction hashes:
214224
$0 --network ethereum --address 0x1234...5678 --nonce 42
@@ -263,7 +273,7 @@ list_networks() {
263273
# Utility function to print a section header.
264274
print_header() {
265275
local header=$1
266-
if [[ -t 1 ]] && tput sgr0 >/dev/null 2>&1; then
276+
if use_formatting; then
267277
# Terminal supports formatting.
268278
printf "\n${UNDERLINE}%s${RESET}\n" "$header"
269279
else
@@ -278,7 +288,7 @@ print_field() {
278288
local value=$2
279289
local empty_line="${3:-false}"
280290

281-
if [[ -t 1 ]] && tput sgr0 >/dev/null 2>&1; then
291+
if use_formatting; then
282292
# Terminal supports formatting.
283293
printf "%s: ${GREEN}%s${RESET}\n" "$label" "$value"
284294
else
@@ -880,6 +890,18 @@ calculate_safe_hashes() {
880890
interactive="1"
881891
shift
882892
;;
893+
--color)
894+
case "$2" in
895+
never|auto|always)
896+
color="$2"
897+
shift 2
898+
;;
899+
*)
900+
echo "Invalid --color option: $2" >&2
901+
usage
902+
;;
903+
esac
904+
;;
883905
*)
884906
echo "Unknown option: $1" >&2
885907
usage

0 commit comments

Comments
 (0)