on 3d07dec help sign in
bat/diagnostics/info.sh
261 lines 6.3 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
10BAT="bat"
11if ! command -v bat &>/dev/null; then
12 if command -v batcat &> /dev/null; then
13 BAT="batcat"
14 else
15 tput setaf 1
16 printf "%s\n%s\n" \
17 "Unable to find a bat executable on your PATH." \
18 "Please ensure that 'bat' exists and is not named something else."
19 tput sgr0
20 exit 1
21 fi
22fi
24# -----------------------------------------------------------------------------
25# Modules:
26# -----------------------------------------------------------------------------
28_bat_:description() {
29 _collects "Version information for 'bat'."
30 _collects "Custom syntaxes and themes for 'bat'."
33_bat_config_:description() {
34 _collects "The environment variables used by 'bat'."
35 _collects "The 'bat' configuration file."
38_bat_wrapper_:description() {
39 _collects "Any wrapper script used by 'bat'."
42_bat_wrapper_function_:description() {
43 _collects "The wrapper function surrounding 'bat' (if applicable)."
46_system_:description() {
47 _collects "Operating system name."
48 _collects "Operating system version."
51_tool_:description() {
52 _collects "Version information for 'less'."
55# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57_bat_:run() {
58 _out "$BAT" --version
59 _out env | grep '^BAT_\|^PAGER='
61 local cache_dir
62 cache_dir="$($BAT --cache-dir)"
63 if [[ -f "${cache_dir}/syntaxes.bin" ]]; then
64 _print_command "$BAT" "--list-languages"
65 echo "Found custom syntax set."
66 fi
68 if [[ -f "${cache_dir}/themes.bin" ]]; then
69 _print_command "$BAT" "--list-themes"
70 echo "Found custom theme set."
71 fi
74_bat_config_:run() {
75 if [[ -f "$("$BAT" --config-file)" ]]; then
76 _out_fence cat "$("$BAT" --config-file)"
77 fi
80_bat_wrapper_:run() {
81 _bat_wrapper_:detect_wrapper() {
82 local bat="$1"
83 if file "$(command -v "${bat}")" | grep "text executable" &> /dev/null; then
84 _out_fence cat "$(command -v "${bat}")"
85 return
86 fi
88 printf "\nNo wrapper script for '%s'.\n" "${bat}"
89 }
91 _bat_wrapper_:detect_wrapper bat
92 if [[ "$BAT" != "bat" ]]; then
93 _bat_wrapper_:detect_wrapper "$BAT"
94 fi
97_bat_wrapper_function_:run() {
98 _bat_wrapper_function_:detect_wrapper() {
99 local command="$1"
100 case "$("$SHELL" --version | head -n 1)" in
101 *fish*)
102 if "$SHELL" --login -i -c "type ${command}" 2>&1 | grep 'function' &> /dev/null; then
103 _out_fence "$SHELL" --login -i -c "functions ${command}"
104 return
105 fi ;;
107 *bash* | *zsh*)
108 local type
109 type="$("$SHELL" --login -i -c "type ${command}" 2>&1)"
110 if grep 'function' <<< "$type" &> /dev/null; then
111 _out_fence "$SHELL" --login -i -c "declare -f ${command}"
112 return
113 elif grep 'alias' <<< "$type" &> /dev/null; then
114 _out_fence "$SHELL" --login -i -c "type ${command}"
115 return
116 fi ;;
118 *)
119 echo "Unable to determine if a wrapper function for '${command}' is set."
120 return ;;
121 esac
122 printf "\nNo wrapper function for '%s'.\n" "${command}"
123 }
125 _bat_wrapper_function_:detect_wrapper bat
126 _bat_wrapper_function_:detect_wrapper cat
127 if [[ "$BAT" != "bat" ]]; then
128 _bat_wrapper_function_:detect_wrapper "$BAT"
129 fi
132_system_:run() {
133 _out uname -srm
135 if command -v "sw_vers" &> /dev/null; then _out sw_vers; fi
136 if command -v "lsb_release" &> /dev/null; then _out lsb_release -a; fi
139_tool_:run() {
140 _out less --version | head -n1
143# -----------------------------------------------------------------------------
144# Functions:
145# -----------------------------------------------------------------------------
147_print_command() {
148 printf '\n**$' 1>&2
149 printf ' %s' "$@" 1>&2
150 printf '**\n' 1>&2
153_out() {
154 _print_command "$@"
155 "$@" 2>&1 | sed 's/$/ /'
158_out_fence() {
159 _print_command "$@"
160 printf '```\n' 1>&2
161 "$@" 2>&1
162 printf '```\n' 1>&2
165_tput() {
166 tput "$@" 1>&2 2> /dev/null
169_collects() {
170 printf " - %s\n" "$1" 1>&2
173_ask_module() {
174 _tput clear
175 _tput cup 0 0
177 cat 1>&2 << EOF
178--------------------------------------------------------------------------------
179This script runs some harmless commands to collect information about your
180system and bat configuration. It will give you a small preview of the commands
181that will be run, and ask consent before running them. Once completed, it will
182output a small report that you can review and copy into the issue description.
183--------------------------------------------------------------------------------
184EOF
186 # Print description.
187 _tput setaf 3
188 printf "The following data will be collected:\n" 1>&2
189 _tput sgr0
190 "_$1_:description"
191 _tput sgr0
193 # Print preview.
194 _tput setaf 3
195 printf "\nThe following commands will be run:\n" 1>&2
196 _tput sgr0
197 declare -f "_$1_:run" \
198 | sed 's/^ *//; s/;$//' \
199 | grep '^_out[^ ]* ' \
200 | sed 's/^_out[^ ]* //' \
201 | sed "s/\"\$BAT\"/$BAT/" 1>&2
203 # Prompt
204 printf "\n" 1>&2
205 local response
206 while true; do
207 _tput cup "$(($( tput lines || echo 22) - 2))"
208 _tput el
209 read -er -p "Collect $(sed 's/_/ /' <<< "$1") data? [Y/n] " response
210 case "$response" in
211 Y | y | yes | '') return 0 ;;
212 N | n | no) return 1 ;;
213 *) continue ;;
214 esac
215 done
218_run_module() {
219 local module="$1"
220 printf "%s\n%s\n" "$module" "$(printf "%${#module}s" | tr ' ' '-')"
221 "_$1_:run"
224# -----------------------------------------------------------------------------
225# Functions:
226# -----------------------------------------------------------------------------
228# Tell the user if their executable isn't named "bat".
229if [[ "$BAT" != "bat" ]] && [[ "$1" != '-y' ]]; then
230 trap '_tput rmcup; exit 1' INT
231 _tput smcup
232 _tput clear
233 _tput cup 0 0
234 _tput setaf 1
235 printf "The %s executable on your system is named '%s'.\n%s\n" "bat" "$BAT" \
236 "If your issue is related to installation, please check that this isn't the issue."
237 _tput sgr0
238 printf "Press any key to continue...\n"
239 read -rsn1
240 _tput rmcup
241fi
243# Ask for consent.
244if [[ "$1" == '-y' ]]; then
245 _modules_consented=("${_modules[@]}")
246else
247 trap '_tput rmcup; exit 1' INT
248 _tput smcup
249 for _module in "${_modules[@]}"; do
250 if _ask_module "$_module"; then
251 _modules_consented+=("$_module")
252 fi
253 done
254 _tput rmcup
255fi
257# Collect information.
258for _module in "${_modules_consented[@]}"; do
259 _run_module "$_module" 2>&1
260 printf "\n"
261done