|
| 1 | + |
| 2 | +# lib_console.sh |
| 3 | + |
| 4 | +Include for console support. |
| 5 | + |
| 6 | +**Usage**: |
| 7 | + |
| 8 | +```bash |
| 9 | +MY_PATH="$(realpath "${BASH_SOURCE[0]}")" |
| 10 | +SCRIPT_DIR="$(dirname "${MY_PATH}")" |
| 11 | +readonly MY_PATH |
| 12 | +readonly SCRIPT_DIR |
| 13 | +readonly LIB_BASH_DIR="${SCRIPT_DIR}/lib_bash" |
| 14 | +source "${LIB_BASH_DIR}/lib_console.sh" |
| 15 | +``` |
| 16 | + |
| 17 | +## Color Constants |
| 18 | + |
| 19 | +### Foreground Colors |
| 20 | + |
| 21 | +* `COLOR_RESET`: Reset the color. |
| 22 | +* `COLOR_BLACK`: Black color. |
| 23 | +* `COLOR_RED`: Red color. |
| 24 | +* `COLOR_GREEN`: Green color. |
| 25 | +* `COLOR_YELLOW`: Yellow color. |
| 26 | +* `COLOR_BLUE`: Blue color. |
| 27 | +* `COLOR_MAGENTA`: Magenta color. |
| 28 | +* `COLOR_CYAN`: Cyan color. |
| 29 | +* `COLOR_WHITE`: White color. |
| 30 | +* `COLOR_GRAY`: Gray color. |
| 31 | +* `COLOR_LIGHT_RED`: Light red color. |
| 32 | +* `COLOR_LIGHT_GREEN`: Light green color. |
| 33 | +* `COLOR_LIGHT_YELLOW`: Light yellow color. |
| 34 | +* `COLOR_LIGHT_BLUE`: Light blue color. |
| 35 | +* `COLOR_LIGHT_MAGENTA`: Light magenta color. |
| 36 | +* `COLOR_LIGHT_CYAN`: Light cyan color. |
| 37 | +* `COLOR_LIGHT_WHITE`: Light white color. |
| 38 | + |
| 39 | +### Background Colors |
| 40 | + |
| 41 | +* `COLOR_BG_BLACK`: Black background color. |
| 42 | +* `COLOR_BG_RED`: Red background color. |
| 43 | +* `COLOR_BG_GREEN`: Green background color. |
| 44 | +* `COLOR_BG_YELLOW`: Yellow background color. |
| 45 | +* `COLOR_BG_BLUE`: Blue background color. |
| 46 | +* `COLOR_BG_MAGENTA`: Magenta background color. |
| 47 | +* `COLOR_BG_CYAN`: Cyan background color. |
| 48 | +* `COLOR_BG_WHITE`: White background color. |
| 49 | + |
| 50 | +## Logging |
| 51 | + |
| 52 | +All logging functions to output information to STDERR. STDERR is preferred, |
| 53 | +as it is not affected by the output redirection of the script, thus, the |
| 54 | +desired standard output can be redirected to a file or another command. |
| 55 | + |
| 56 | +### log_debug |
| 57 | + |
| 58 | +Output debug message. |
| 59 | + |
| 60 | +The function is used to output debug information. It is only printed |
| 61 | +if the `DEBUG` variable is set to a value greater than 0 (zero). |
| 62 | + |
| 63 | +If the standard output supports color, the debug information is printed in |
| 64 | +gray color. Otherwise, the debug information is printed without color. |
| 65 | + |
| 66 | +**Usage**: |
| 67 | + |
| 68 | +```bash |
| 69 | +log_debug "Only show if DEBUG=1 (or more)" |
| 70 | +grep "pattern" file.txt | log_debug |
| 71 | +``` |
| 72 | + |
| 73 | +### log_info |
| 74 | + |
| 75 | +Output information message. |
| 76 | + |
| 77 | +The function is used to output information messages. No extra color is |
| 78 | +used for the information message. |
| 79 | + |
| 80 | +**Usage**: |
| 81 | + |
| 82 | +```bash |
| 83 | +log_info "An informational message." |
| 84 | +grep "pattern" file.txt | log_info |
| 85 | +``` |
| 86 | + |
| 87 | +### log_warn |
| 88 | + |
| 89 | +Output warning message. |
| 90 | + |
| 91 | +The function is used to output warning messages. The warning message |
| 92 | +is printed in yellow color. |
| 93 | + |
| 94 | +**Usage**: |
| 95 | + |
| 96 | +```bash |
| 97 | +log_warn "A warning notice." |
| 98 | +grep "pattern" file.txt | log_warn |
| 99 | +``` |
| 100 | + |
| 101 | +### log_error |
| 102 | + |
| 103 | +Output error message. |
| 104 | + |
| 105 | +The function is used to output error messages. The error message is |
| 106 | +printed in red color. |
| 107 | + |
| 108 | +**Usage**: |
| 109 | + |
| 110 | +```bash |
| 111 | +log_error "An error." |
| 112 | +grep "pattern" file.txt | log_error |
| 113 | +``` |
| 114 | + |
| 115 | +### log_fatal |
| 116 | + |
| 117 | +Output a fatal error message. |
| 118 | + |
| 119 | +The function is used to output error messages. The error message is |
| 120 | +printed with dark red background and bright yellow color. |
| 121 | + |
| 122 | +**Usage**: |
| 123 | + |
| 124 | +```bash |
| 125 | +log_fatal "A fatal error." |
| 126 | +grep "pattern" file.txt | log_fatal |
| 127 | +``` |
| 128 | + |
| 129 | +### ccat |
| 130 | + |
| 131 | +Alternative to `cat` that also processes colored input. |
| 132 | + |
| 133 | +**Usage**: |
| 134 | + |
| 135 | +```bash |
| 136 | +ccat <<EOF |
| 137 | +${COLOR_RED}This is red text.${COLOR_RESET} |
| 138 | +${COLOR_YELLOW}This is yellow text.${COLOR_RESET} |
| 139 | +EOF |
| 140 | +``` |
0 commit comments