on f5145ef help sign in
bat/diagnostics/info.sh
210 lines 4.8 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 case "$("$SHELL" --version | head -n 1)" in
75 *fish*)
76 if "$SHELL" --login -i -c 'type bat' 2>&1 | grep 'function' &> /dev/null; then
77 _out_fence "$SHELL" --login -c 'functions bat'
78 return
79 fi
80 ;;
82 *bash* | *zsh*)
83 if "$SHELL" --login -i -c 'type bat' 2>&1 | grep 'function' &> /dev/null; then
84 _out_fence "$SHELL" --login -i -c 'declare -f bat'
85 return
86 fi
87 ;;
89 *)
90 echo "Unable to determine if a wrapper function is set."
91 return
92 ;;
93 esac
94 printf "\nNo wrapper function.\n"
97_system_:run() {
98 _out uname -srm
100 if command -v "sw_vers" &> /dev/null; then _out sw_vers; fi
101 if command -v "lsb_release" &> /dev/null; then _out lsb_release -a; fi
104_tool_:run() {
105 _out less --version | head -n1
108# -----------------------------------------------------------------------------
109# Functions:
110# -----------------------------------------------------------------------------
112_print_command() {
113 printf '\n**$' 1>&2
114 printf ' %s' "$@" 1>&2
115 printf '**\n' 1>&2
118_out() {
119 _print_command "$@"
120 "$@" 2>&1 | sed 's/$/ /'
123_out_fence() {
124 _print_command "$@"
125 printf '```\n' 1>&2
126 "$@" 2>&1
127 printf '```\n' 1>&2
130_tput() {
131 tput "$@" 1>&2 2> /dev/null
134_collects() {
135 printf " - %s\n" "$1" 1>&2
138_ask_module() {
139 _tput clear
140 _tput cup 0 0
142 cat 1>&2 << EOF
143--------------------------------------------------------------------------------
144This script runs some harmless commands to collect information about your
145system and bat configuration. It will give you a small preview of the commands
146that will be run, and ask consent before running them. Once completed, it will
147output a small report that you can review and copy into the issue description.
148--------------------------------------------------------------------------------
149EOF
151 # Print description.
152 _tput setaf 3
153 printf "The following data will be collected:\n" 1>&2
154 _tput sgr0
155 "_$1_:description"
156 _tput sgr0
158 # Print preview.
159 _tput setaf 3
160 printf "\nThe following commands will be run:\n" 1>&2
161 _tput sgr0
162 declare -f "_$1_:run" \
163 | sed 's/^ *//; s/;$//' \
164 | grep '^_out[^ ]* ' \
165 | sed 's/^_out[^ ]* //' 1>&2
167 # Prompt
168 printf "\n" 1>&2
169 local response
170 while true; do
171 _tput cup "$(($( tput lines || echo 22) - 2))"
172 _tput el
173 read -er -p "Collect $(sed 's/_/ /' <<< "$1") data? [Y/n] " response
174 case "$response" in
175 Y | y | yes | '') return 0 ;;
176 N | n | no) return 1 ;;
177 *) continue ;;
178 esac
179 done
182_run_module() {
183 local module="$1"
184 printf "%s\n%s\n" "$module" "$(printf "%${#module}s" | tr ' ' '-')"
185 "_$1_:run"
188# -----------------------------------------------------------------------------
189# Functions:
190# -----------------------------------------------------------------------------
192# Ask for consent.
193if [[ "$1" == '-y' ]]; then
194 _modules_consented=("${_modules[@]}")
195else
196 trap '_tput rmcup; exit 1' INT
197 _tput smcup
198 for _module in "${_modules[@]}"; do
199 if _ask_module "$_module"; then
200 _modules_consented+=("$_module")
201 fi
202 done
203 _tput rmcup
204fi
206# Collect information.
207for _module in "${_modules_consented[@]}"; do
208 _run_module "$_module" 2>&1
209 printf "\n"
210done