lib/log: `shellcheck` && `shfmt`

Alsö, fix tests to load `lib/colors` instead of `lib/appearance`...wut

Alsö, `short-circuit _has_colors()`: If we already looked up colors, and we already have them, then don't run `tput` again.

My apologies to future `git blame` hunters ♥
pull/1904/head
John D Pell 2021-10-13 20:52:33 -07:00
parent 512ca416ed
commit 2973434af5
3 changed files with 45 additions and 42 deletions

View File

@ -78,6 +78,7 @@ completion/available/wpscan.completion.bash
# libraries
lib/helpers.bash
lib/log.bash
lib/search.bash
lib/utilities.bash

View File

@ -1,21 +1,26 @@
#!/usr/bin/env bash
# shellcheck shell=bash
#
# A collection of logging functions.
export BASH_IT_LOG_LEVEL_ERROR=1
export BASH_IT_LOG_LEVEL_WARNING=2
export BASH_IT_LOG_LEVEL_ALL=3
function _has_colors()
{
function _has_colors() {
# Check that stdout is a terminal
test -t 1 || return 1
[[ -t 1 ]] || return 1
ncolors=$(tput colors)
test -n "$ncolors" && test "$ncolors" -ge 8 || return 1
return 0
[[ "${_bash_it_available_colors:-0}" -ge 8 ]] \
&& return 0 # short-circuit if possible
local -i ncolors
ncolors="$(tput colors 2> /dev/null)"
_bash_it_available_colors="${ncolors:=0}"
[[ "${ncolors}" -ge 8 ]] && return 0
return 1
}
function _log_general()
{
function _log_general() {
about 'Internal function used for logging, uses BASH_IT_LOG_PREFIX as a prefix'
param '1: color of the message'
param '2: log level to print before the prefix'
@ -26,8 +31,7 @@ function _log_general()
_has_colors && echo -e "$1${message}${echo_normal:-}" || echo -e "${message}"
}
function _log_debug()
{
function _log_debug() {
about 'log a debug message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ALL'
param '1: message to log'
example '$ _log_debug "Loading plugin git..."'
@ -37,8 +41,7 @@ function _log_debug()
_log_general "${echo_green:-}" "DEBUG: " "$1"
}
function _log_warning()
{
function _log_warning() {
about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_WARNING'
param '1: message to log'
example '$ _log_warning "git binary not found, disabling git plugin..."'
@ -48,8 +51,7 @@ function _log_warning()
_log_general "${echo_yellow:-}" " WARN: " "$1"
}
function _log_error()
{
function _log_error() {
about 'log a message by echoing to the screen. needs BASH_IT_LOG_LEVEL >= BASH_IT_LOG_LEVEL_ERROR'
param '1: message to log'
example '$ _log_error "Failed to load git plugin..."'

View File

@ -2,7 +2,7 @@
load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
load ../../lib/appearance
load ../../themes/colors.theme
cite _about _param _example _group _author _version
load ../../lib/log