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