on 5f6e310 help sign in
bat/diagnostics/info.sh
217 lines 5.2 kB history raw
1#!/usr/bin/env bash
2_modules=('system' 'bat' 'bat_config' 'bat_wrapper' 'bat_wrapper_function' 'tool')
3_modules_consented=()
5set -o pipefail
7export LC_ALL=C
8export LANG=C
10# -----------------------------------------------------------------------------
11# Modules:
12# -----------------------------------------------------------------------------
14_bat_:description() {
15 _collects "Version information for 'bat'."
16 _collects "Custom syntaxes and themes for 'bat'."
19_bat_config_:description() {
20 _collects "The environment variables used by 'bat'."
21 _collects "The 'bat' configuration file."
24_bat_wrapper_:description() {
25 _collects "Any wrapper script used by 'bat'."
28_bat_wrapper_function_:description() {
29 _collects "The wrapper function surrounding 'bat' (if applicable)."
32_system_:description() {
33 _collects "Operating system name."
34 _collects "Operating system version."
37_tool_:description() {
38 _collects "Version information for 'less'."
41# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43_bat_:run() {
44 _out bat --version
45 _out env | grep '^BAT_\|^PAGER='
47 local cache_dir="$(bat --cache-dir)"
48 if [[ -f "${cache_dir}/syntaxes.bin" ]]; then
49 _print_command "bat" "--list-languages"
50 echo "Found custom syntax set."
51 fi
53 if [[ -f "${cache_dir}/themes.bin" ]]; then
54 _print_command "bat" "--list-themes"
55 echo "Found custom theme set."
56 fi
59_bat_config_:run() {
60 if [[ -f "$(bat --config-file)" ]]; then
61 _out_fence cat "$(bat --config-file)"
62 fi
65_bat_wrapper_:run() {
66 if file "$(which bat)" | grep "text executable" &> /dev/null; then
67 _out_fence cat "$(which bat)"
68 return
69 fi
70 printf "\nNo wrapper script.\n"
73_bat_wrapper_function_:run() {
74 _bat_wrapper_function_:detect_wrapper() {
75 local command="$1"
76 case "$("$SHELL" --version | head -n 1)" in
77 *fish*)
78 if "$SHELL" --login -i -c "type ${command}" 2>&1 | grep 'function' &>/dev/null; then
79 _out_fence "$SHELL" --login -i -c "functions ${command}"
80 return
81 fi ;;
83 *bash*|*zsh*)
84 local type="$("$SHELL" --login -i -c "type ${command}" 2>&1)"
85 if grep 'function' <<< "$type" &>/dev/null; then
86 _out_fence "$SHELL" --login -i -c "declare -f ${command}"
87 return
88 elif grep 'alias' <<< "$type" &>/dev/null; then
89 _out_fence "$SHELL" --login -i -c "type ${command}"
90 return
91 fi ;;
93 *)
94 echo "Unable to determine if a wrapper function for '${command}' is set."
95 return ;;
96 esac
97 printf "\nNo wrapper function for '%s'.\n" "${command}"
98 }
100 _bat_wrapper_function_:detect_wrapper bat
101 _bat_wrapper_function_:detect_wrapper cat
104_system_:run() {
105 _out uname -srm
107 if command -v "sw_vers" &> /dev/null; then _out sw_vers; fi
108 if command -v "lsb_release" &> /dev/null; then _out lsb_release -a; fi
111_tool_:run() {
112 _out less --version | head -n1
115# -----------------------------------------------------------------------------
116# Functions:
117# -----------------------------------------------------------------------------
119_print_command() {
120 printf '\n**$' 1>&2
121 printf ' %s' "$@" 1>&2
122 printf '**\n' 1>&2
125_out() {
126 _print_command "$@"
127 "$@" 2>&1 | sed 's/$/ /'
130_out_fence() {
131 _print_command "$@"
132 printf '```\n' 1>&2
133 "$@" 2>&1
134 printf '```\n' 1>&2
137_tput() {
138 tput "$@" 1>&2 2> /dev/null
141_collects() {
142 printf " - %s\n" "$1" 1>&2
145_ask_module() {
146 _tput clear
147 _tput cup 0 0
149 cat 1>&2 << EOF
150--------------------------------------------------------------------------------
151This script runs some harmless commands to collect information about your
152system and bat configuration. It will give you a small preview of the commands
153that will be run, and ask consent before running them. Once completed, it will
154output a small report that you can review and copy into the issue description.
155--------------------------------------------------------------------------------
156EOF
158 # Print description.
159 _tput setaf 3
160 printf "The following data will be collected:\n" 1>&2
161 _tput sgr0
162 "_$1_:description"
163 _tput sgr0
165 # Print preview.
166 _tput setaf 3
167 printf "\nThe following commands will be run:\n" 1>&2
168 _tput sgr0
169 declare -f "_$1_:run" \
170 | sed 's/^ *//; s/;$//' \
171 | grep '^_out[^ ]* ' \
172 | sed 's/^_out[^ ]* //' 1>&2
174 # Prompt
175 printf "\n" 1>&2
176 local response
177 while true; do
178 _tput cup "$(($( tput lines || echo 22) - 2))"
179 _tput el
180 read -er -p "Collect $(sed 's/_/ /' <<< "$1") data? [Y/n] " response
181 case "$response" in
182 Y | y | yes | '') return 0 ;;
183 N | n | no) return 1 ;;
184 *) continue ;;
185 esac
186 done
189_run_module() {
190 local module="$1"
191 printf "%s\n%s\n" "$module" "$(printf "%${#module}s" | tr ' ' '-')"
192 "_$1_:run"
195# -----------------------------------------------------------------------------
196# Functions:
197# -----------------------------------------------------------------------------
199# Ask for consent.
200if [[ "$1" == '-y' ]]; then
201 _modules_consented=("${_modules[@]}")
202else
203 trap '_tput rmcup; exit 1' INT
204 _tput smcup
205 for _module in "${_modules[@]}"; do
206 if _ask_module "$_module"; then
207 _modules_consented+=("$_module")
208 fi
209 done
210 _tput rmcup
211fi
213# Collect information.
214for _module in "${_modules_consented[@]}"; do
215 _run_module "$_module" 2>&1
216 printf "\n"
217done