on 5ebf021 help sign in
bat/diagnostics/info.sh
259 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="$(bat --cache-dir)"
62 if [[ -f "${cache_dir}/syntaxes.bin" ]]; then
63 _print_command "$BAT" "--list-languages"
64 echo "Found custom syntax set."
65 fi
67 if [[ -f "${cache_dir}/themes.bin" ]]; then
68 _print_command "$BAT" "--list-themes"
69 echo "Found custom theme set."
70 fi
73_bat_config_:run() {
74 if [[ -f "$("$BAT" --config-file)" ]]; then
75 _out_fence cat "$("$BAT" --config-file)"
76 fi
79_bat_wrapper_:run() {
80 _bat_wrapper_:detect_wrapper() {
81 local bat="$1"
82 if file "$(which "${bat}")" | grep "text executable" &> /dev/null; then
83 _out_fence cat "$(which "${bat}")"
84 return
85 fi
87 printf "\nNo wrapper script for '%s'.\n" "${bat}"
88 }
90 _bat_wrapper_:detect_wrapper bat
91 if [[ "$BAT" != "bat" ]]; then
92 _bat_wrapper_:detect_wrapper "$BAT"
93 fi
96_bat_wrapper_function_:run() {
97 _bat_wrapper_function_:detect_wrapper() {
98 local command="$1"
99 case "$("$SHELL" --version | head -n 1)" in
100 *fish*)
101 if "$SHELL" --login -i -c "type ${command}" 2>&1 | grep 'function' &> /dev/null; then
102 _out_fence "$SHELL" --login -i -c "functions ${command}"
103 return
104 fi ;;
106 *bash* | *zsh*)
107 local type="$("$SHELL" --login -i -c "type ${command}" 2>&1)"
108 if grep 'function' <<< "$type" &> /dev/null; then
109 _out_fence "$SHELL" --login -i -c "declare -f ${command}"
110 return
111 elif grep 'alias' <<< "$type" &> /dev/null; then
112 _out_fence "$SHELL" --login -i -c "type ${command}"
113 return
114 fi ;;
116 *)
117 echo "Unable to determine if a wrapper function for '${command}' is set."
118 return ;;
119 esac
120 printf "\nNo wrapper function for '%s'.\n" "${command}"
121 }
123 _bat_wrapper_function_:detect_wrapper bat
124 _bat_wrapper_function_:detect_wrapper cat
125 if [[ "$BAT" != "bat" ]]; then
126 _bat_wrapper_function_:detect_wrapper "$BAT"
127 fi
130_system_:run() {
131 _out uname -srm
133 if command -v "sw_vers" &> /dev/null; then _out sw_vers; fi
134 if command -v "lsb_release" &> /dev/null; then _out lsb_release -a; fi
137_tool_:run() {
138 _out less --version | head -n1
141# -----------------------------------------------------------------------------
142# Functions:
143# -----------------------------------------------------------------------------
145_print_command() {
146 printf '\n**$' 1>&2
147 printf ' %s' "$@" 1>&2
148 printf '**\n' 1>&2
151_out() {
152 _print_command "$@"
153 "$@" 2>&1 | sed 's/$/ /'
156_out_fence() {
157 _print_command "$@"
158 printf '```\n' 1>&2
159 "$@" 2>&1
160 printf '```\n' 1>&2
163_tput() {
164 tput "$@" 1>&2 2> /dev/null
167_collects() {
168 printf " - %s\n" "$1" 1>&2
171_ask_module() {
172 _tput clear
173 _tput cup 0 0
175 cat 1>&2 << EOF
176--------------------------------------------------------------------------------
177This script runs some harmless commands to collect information about your
178system and bat configuration. It will give you a small preview of the commands
179that will be run, and ask consent before running them. Once completed, it will
180output a small report that you can review and copy into the issue description.
181--------------------------------------------------------------------------------
182EOF
184 # Print description.
185 _tput setaf 3
186 printf "The following data will be collected:\n" 1>&2
187 _tput sgr0
188 "_$1_:description"
189 _tput sgr0
191 # Print preview.
192 _tput setaf 3
193 printf "\nThe following commands will be run:\n" 1>&2
194 _tput sgr0
195 declare -f "_$1_:run" \
196 | sed 's/^ *//; s/;$//' \
197 | grep '^_out[^ ]* ' \
198 | sed 's/^_out[^ ]* //' \
199 | sed "s/\"\$BAT\"/$BAT/" 1>&2
201 # Prompt
202 printf "\n" 1>&2
203 local response
204 while true; do
205 _tput cup "$(($( tput lines || echo 22) - 2))"
206 _tput el
207 read -er -p "Collect $(sed 's/_/ /' <<< "$1") data? [Y/n] " response
208 case "$response" in
209 Y | y | yes | '') return 0 ;;
210 N | n | no) return 1 ;;
211 *) continue ;;
212 esac
213 done
216_run_module() {
217 local module="$1"
218 printf "%s\n%s\n" "$module" "$(printf "%${#module}s" | tr ' ' '-')"
219 "_$1_:run"
222# -----------------------------------------------------------------------------
223# Functions:
224# -----------------------------------------------------------------------------
226# Tell the user if their executable isn't named "bat".
227if [[ "$BAT" != "bat" ]] && [[ "$1" != '-y' ]]; then
228 trap '_tput rmcup; exit 1' INT
229 _tput smcup
230 _tput clear
231 _tput cup 0 0
232 _tput setaf 1
233 printf "The %s executable on your system is named '%s'.\n%s\n" "bat" "$BAT" \
234 "If your issue is related to installation, please check that this isn't the issue."
235 _tput sgr0
236 printf "Press any key to continue...\n"
237 read -rsn1
238 _tput rmcup
239fi
241# Ask for consent.
242if [[ "$1" == '-y' ]]; then
243 _modules_consented=("${_modules[@]}")
244else
245 trap '_tput rmcup; exit 1' INT
246 _tput smcup
247 for _module in "${_modules[@]}"; do
248 if _ask_module "$_module"; then
249 _modules_consented+=("$_module")
250 fi
251 done
252 _tput rmcup
253fi
255# Collect information.
256for _module in "${_modules_consented[@]}"; do
257 _run_module "$_module" 2>&1
258 printf "\n"
259done