-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathformat
More file actions
executable file
·244 lines (214 loc) · 5.46 KB
/
format
File metadata and controls
executable file
·244 lines (214 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/bin/bash
# SPDX-FileCopyrightText: 2020 - 2024 sudorook <daemon@nullcodon.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -eu
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command find xdg-mime && exit 3
function format_c() {
! check_command clang-format && exit 3
local tmp
tmp="$(mktemp)"
trap 'rm -f "${tmp}"' INT ERR TERM
if [ -f "${HOME}/.clang-format" ] || [ -f .clang-format ]; then
clang-format "${1}" > "${tmp}"
else
clang-format -style=mozilla "${1}" > "${tmp}"
fi
mv "${tmp}" "${1}"
sync
}
export -f format_c
function format_js() {
! check_command prettier && exit 3
prettier -w "${1}"
}
export -f format_js
function format_lua() {
! check_command stylua && exit 3
stylua --indent-type spaces --indent-width 2 --column-width 80 "${1}"
}
export -f format_lua
function format_markdown() {
! check_command prettier && exit 3
prettier --log-level silent --prose-wrap always -w "${1}"
}
export -f format_markdown
function format_python() {
! check_command black && exit 3
black -q -l 79 "${1}"
}
export -f format_python
function format_R() {
! check_command Rscript && exit 3
Rscript <(
cat << EOF
if (system.file(package="styler") != "") {
library(styler)
styler::style_file("${1}", scope = I(c("tokens", "indention", "spaces", "line_breaks")))
quit(status=0)
} else {
print("styler not installed.")
quit(status=1)
}
EOF
)
}
export -f format_R
function format_ruby() {
! check_command rubocop && exit 3
rubocop --autocorrect --stderr "${1}" 2> /dev/null
}
export -f format_ruby
function format_rust() {
! check_command rustfmt && exit 3
rustfmt "${1}"
}
export -f format_rust
function format_shellscript() {
! check_command shfmt && exit 3
shfmt -i 2 -ci -kp -sr -w "${1}"
}
export -f format_shellscript
function format_tex() {
! check_command latexindent && exit 3
latexindent -wd "${1}"
}
export -f format_tex
function format_wrapper() {
local lang
local mime
# Parse mime-type and language
mime="$(xdg-mime query filetype "${1}")"
shopt -s extglob
mime="${mime/+(text\/|application\/)?(x-)?(script.)/}"
lang=${LANGUAGE:-"${mime}"}
lang=${lang,,}
lang="${lang//+(cpp)/c++}" # MIME types use 'c++', so convert 'cpp' strings.
shopt -u extglob
if [[ "${mime}" = plain ]]; then
local ext="${1##*.}"
case "${ext,,}" in
r | rmd)
mime="${ext,,}"
lang=${LANGUAGE:-"${mime}"}
show_warning "WARNING: Could not find MIME data. Defaulting to ${mime@Q}."
;;
esac
fi
# Check if the file MIME type and the specified language are mismatched. If
# so, skip unless the FORCE parameter is set to true.
if [[ "${mime}" != "${lang}" ]]; then
if ! "${FORCE}"; then
if ! [[ "${mime}" =~ ${lang} ]]; then
show_warning "WARNING: ${1@Q} language and MIME type mismatch. Skipping." >&2
return
fi
else
show_info "Force-formatting ${1@Q} to ${lang@Q}." >&2
fi
fi
case "${lang}" in
c | csrc | chdr | c++ | c++hdr | c++src)
if [[ "${1##*.}" != y ]] && [[ "${1##*.}" != l ]]; then
format_c "${1}"
else
show_warning "WARNING: skipping ${1@Q} parser/lexer." >&2
fi
;;
js | json)
format_js "${1}"
;;
lua)
format_lua "${1}"
;;
markdown)
format_markdown "${1}"
;;
python | python3)
format_python "${1}"
;;
r)
format_R "${1}"
;;
ruby)
format_ruby "${1}"
;;
rust)
format_rust "${1}"
;;
shellscript)
format_shellscript "${1}"
;;
tex)
format_tex "${1}"
;;
*)
show_warning "WARNING: skipping ${1@Q} type ${mime@Q}." >&2
return
;;
esac
show_success "${lang@Q}: ${1@Q} done!" >&2
}
export -f format_wrapper
function print_usage() {
show_header "Usage: format <file or directory> -l <lang>"
cat << EOF
-l|--language (optional) programming language (e.g. C++, Python)
-h|--help print (this) message
-f|--force run formatter regardless of MIME type
Specify a file to format a single file. Specify a directory to recursively
format _all_ files in it. Any files without MIME type supported by this
script will be ignored.
EOF
}
#
# Main
#
FORCE=false
OPTIONS=fhl:
LONGOPTIONS=force,help,language:
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case "${1}" in
-f | --force)
FORCE=true
shift
;;
-l | --language)
LANGUAGE="${2}"
shift 2
;;
-h | --help)
print_usage
exit
;;
--)
shift
break
;;
*)
show_error "ERROR"
exit 3
;;
esac
done
export FORCE
export LANGUAGE
find "${@:-.}" \( -name ".git" -o -path "*/.*" \) -prune , -type f -print0 |
xargs -0 -P "$(nproc)" -I{} bash -c 'format_wrapper "{}"'