]> glassweightruler.freedombox.rocks Git - xdg-ninja.git/blob - xdg-ninja.sh
Add support for bat for markdown rendering
[xdg-ninja.git] / xdg-ninja.sh
1 #!/usr/bin/env sh
2 # shellcheck disable=SC2016
3
4 USE_GLOW=false
5 USE_BAT=false
6 if command -v glow >/dev/null 2>/dev/null; then
7 USE_GLOW=true
8 elif command -v bat >/dev/null 2>/dev/null; then
9 USE_BAT=true
10 else
11 printf "Glow or bat not found, markdown rendering not available.\n"
12 printf "Output will be raw markdown and might look weird.\n"
13 printf "Install glow or bat for easier reading & copy-paste.\n"
14 fi
15
16 unalias -a
17
18 HELPSTRING="""\
19
20
21 \e[37;45;1mxdg-ninja\e[0m
22
23 \e[1;3mCheck your \$HOME for unwanted files.\e[1;0m
24
25 ────────────────────────────────────
26
27 \e[3m--help\e[0m \e[1mThis help menu\e[0m
28 \e[3m-h\e[0m
29
30 \e[3m--no-skip-ok\e[0m \e[1mDisplay messages for all files checked (verbose)\e[0m
31 \e[3m-v\e[0m
32
33 \e[3m--skip-ok\e[0m \e[1mDon't display anything for files that do not exist (default)\e[0m
34
35 """
36
37 SKIP_OK=true
38 for i in "$@"; do
39 if [ "$i" = "--help" ] || [ "$i" = "-h" ]; then
40 printf "%b" "$HELPSTRING"
41 exit
42 elif [ "$i" = "--skip-ok" ]; then
43 SKIP_OK=true
44 elif [ "$i" = "--no-skip-ok" ]; then
45 SKIP_OK=false
46 elif [ "$i" = "-v" ]; then
47 SKIP_OK=false
48 fi
49 done
50
51 if [ -z "${XDG_DATA_HOME}" ]; then
52 printf '\e[1;36m%s\e[1;0m\n' "The \$XDG_DATA_HOME environment variable is not set, make sure to add it to your shell's configuration before setting any of the other environment variables!"
53 printf '\e[1;36m ⤷ \e[1mThe recommended value is: \e[1;3m$HOME/.local/share\e[1;0m\n'
54 fi
55 if [ -z "${XDG_CONFIG_HOME}" ]; then
56 printf '\e[1;36m%s\e[1;0m\n' "The \$XDG_CONFIG_HOME environment variable is not set, make sure to add it to your shell's configuration before setting any of the other environment variables!"
57 printf '\e[1;36m ⤷ \e[1mThe recommended value is: \e[1;3m$HOME/.config\e[1;0m\n'
58 fi
59 if [ -z "${XDG_STATE_HOME}" ]; then
60 printf '\e[1;36m%s\e[1;0m\n' "The \$XDG_STATE_HOME environment variable is not set, make sure to add it to your shell's configuration before setting any of the other environment variables!"
61 printf '\e[1;36m ⤷ \e[1mThe recommended value is: \e[1;3m$HOME/.local/state\e[1;0m\n'
62 fi
63 if [ -z "${XDG_CACHE_HOME}" ]; then
64 printf '\e[1;36m%s\e[1;0m\n' "The \$XDG_CACHE_HOME environment variable is not set, make sure to add it to your shell's configuration before setting any of the other environment variables!"
65 printf '\e[1;36m ⤷ \e[1mThe recommended value is: \e[1;3m$HOME/.cache\e[1;0m\n'
66 fi
67 if [ -z "${XDG_RUNTIME_DIR}" ]; then
68 printf '\e[1;36m%s\e[1;0m\n' "The \$XDG_RUNTIME_DIR environment variable is not set, make sure to add it to your shell's configuration before setting any of the other environment variables!"
69 printf '\e[1;36m ⤷ \e[1mThe recommended value is: \e[1;3m/run/user/$UID\e[1;0m\n'
70 fi
71
72 if ! command -v jq >/dev/null 2>/dev/null; then
73 printf "jq is needed to run this script, but it wasn't found. Please install it to be able to use this script."
74 exit
75 fi
76
77 printf "\n"
78
79 # Function to expand environment variables in string
80 # https://stackoverflow.com/a/20316582/11110290
81 apply_shell_expansion() {
82 data="$1"
83 delimiter="__apply_shell_expansion_delimiter__"
84 command=$(printf "cat <<%s\n%s\n%s" "$delimiter" "$data" "$delimiter")
85 eval "$command"
86 }
87
88 # Returns 0 if the path doesn't lead anywhere
89 # Return 1 if the path points to a file, 2 if it points to a directory
90 check_not_exists_file() {
91 FILE_PATH=$(apply_shell_expansion "$1")
92 if [ -f "$FILE_PATH" ]; then
93 return 1
94 elif [ -d "$FILE_PATH" ]; then
95 return 2
96 else
97 return 0
98 fi
99 }
100
101 # Function to handle the formatting of output
102 log() {
103 MODE="$1"
104 NAME="$2"
105 FILENAME="$3"
106 HELP="$4"
107
108 case "$MODE" in
109
110 ERR)
111 printf '[\e[1;31m%s\e[1;0m]: \e[1;3m%s\e[1;0m\n' "$NAME" "$FILENAME"
112 ;;
113
114 WARN)
115 printf '[\e[1;33m%s\e[1;0m]: \e[1;3m%s\e[1;0m\n' "$NAME" "$FILENAME"
116 ;;
117
118 INFO)
119 printf '[\e[1;36m%s\e[1;0m]: \e[1;3m%s\e[1;0m\n' "$NAME" "$FILENAME"
120 ;;
121
122 SUCS)
123 [ "$SKIP_OK" = false ] &&
124 printf '[\e[1;32m%s\e[1;0m]: \e[1;3m%s\e[1;0m\n' "$NAME" "$FILENAME"
125 ;;
126
127 HELP)
128 if $USE_GLOW; then
129 printf "%s" "$HELP" | glow -
130 elif $USE_BAT; then
131 printf "%s" "$HELP" | bat -pp -f --language markdown
132 else
133 printf "%s" "$HELP"
134 fi
135 ;;
136
137 esac
138 }
139
140 # Checks that the given file does not exist, otherwise outputs help
141 check_file() {
142 INPUT="$1"
143 NAME="$2"
144
145 FILENAME=$(printf "%s" "$INPUT" | jq -r .path)
146 MOVABLE=$(printf "%s" "$INPUT" | jq -r .movable)
147 HELP=$(printf "%s" "$INPUT" | jq -r .help)
148
149 check_not_exists_file "$FILENAME"
150
151 case $? in
152
153 0)
154 log SUCS "$NAME" "$FILENAME" "$HELP"
155 ;;
156
157 1 | 2)
158 if "$MOVABLE"; then
159 log ERR "$NAME" "$FILENAME" "$HELP"
160 else
161 log WARN "$NAME" "$FILENAME" "$HELP"
162 fi
163 if [ "$HELP" ]; then
164 log HELP "$NAME" "$FILENAME" "$HELP"
165 else
166 log HELP "$NAME" "$FILENAME" "_No help available._"
167 fi
168 ;;
169
170 esac
171 }
172
173 # Reads a file from programs/, calls check_file on each file specified for the program
174 check_program() {
175 INPUT=$1
176
177 NAME=$(printf "%s" "$INPUT" | jq -r .name)
178
179 while IFS= read -r file; do
180 check_file "$file" "$NAME"
181 done <<EOF
182 $(echo "$INPUT" | jq -rc '.files[]')
183 EOF
184 }
185
186 # Loops over all files in the programs/ directory and calls check_program
187 enumerate_programs() {
188 printf "\e[1;3mStarting to check your \e[1;36m\$HOME.\e[1;0m\n"
189 printf "\n"
190 for prog_filename in "${0%/*}"/programs/*; do
191 check_program "$(cat "$prog_filename")"
192 done
193 printf "\e[1;3mDone checking your \e[1;36m\$HOME.\e[1;0m\n"
194 printf "\n"
195 printf "\e[3mIf you have files in your \e[1;36m\$HOME\e[1;0m that shouldn't be there, but weren't recognised by xdg-ninja, please consider creating a configuration file for it and opening a pull request on github.\e[1;0m\n"
196 printf "\n"
197 }
198
199 enumerate_programs