fish_indent: Allow multiple file arguments

Allows `fish_indent -w **.fish` to restyle all fish files under the
current directory.

(This also has the sideeffect of reducing style.fish time by ~10s, as
we only need to invoke `fish_indent` once, instead of once per-file)
This commit is contained in:
Fabian Homborg 2019-05-20 21:04:51 +02:00
parent 8e640cdcc5
commit c2b7e9b2e6
3 changed files with 58 additions and 69 deletions

View file

@ -95,16 +95,7 @@ if set -q fish_files[1]
set PATH . $PATH set PATH . $PATH
end end
echo === Running "$green"fish_indent"$normal" echo === Running "$green"fish_indent"$normal"
for file in $fish_files fish_indent -w -- $fish_files
cp $file $file.new # preserves mode bits
fish_indent <$file >$file.new
if cmp --quiet $file $file.new
rm $file.new
else
echo $file was NOT correctly formatted
mv $file.new $file
end
end
end end
if set -q python_files[1] if set -q python_files[1]

View file

@ -6,13 +6,13 @@ fish_indent - indenter and prettifier
Synopsis Synopsis
-------- --------
fish_indent [OPTIONS] fish_indent [OPTIONS] [FILE...]
Description Description
----------- -----------
``fish_indent`` is used to indent a piece of fish code. ``fish_indent`` reads commands from standard input and outputs them to standard output or a specified file. ``fish_indent`` is used to indent a piece of fish code. ``fish_indent`` reads commands from standard input or the given filenames and outputs them to standard output or a specified file (if `-w` is given).
The following options are available: The following options are available:

View file

@ -629,25 +629,22 @@ int main(int argc, char *argv[]) {
exit(1); exit(1);
} }
src = read_file(stdin); src = read_file(stdin);
} else if (argc == 1) { } else {
FILE *fh = fopen(*argv, "r"); ret = EXIT_SUCCESS;
for (int i; i < argc; i++) {
FILE *fh = fopen(argv[i], "r");
if (fh) { if (fh) {
src = read_file(fh); src = read_file(fh);
fclose(fh); fclose(fh);
output_location = *argv; output_location = argv[i];
} else { } else {
std::fwprintf(stderr, _(L"Opening \"%s\" failed: %s\n"), *argv, std::strerror(errno)); std::fwprintf(stderr, _(L"Opening \"%s\" failed: %s\n"), *argv, std::strerror(errno));
exit(1); exit(1);
} }
} else {
std::fwprintf(stderr, _(L"Too many arguments\n"));
exit(1);
}
if (output_type == output_type_pygments_csv) { if (output_type == output_type_pygments_csv) {
std::string output = make_pygments_csv(src); std::string output = make_pygments_csv(src);
fputs(output.c_str(), stdout); fputs(output.c_str(), stdout);
return EXIT_SUCCESS;
} }
const wcstring output_wtext = prettify(src, do_indent); const wcstring output_wtext = prettify(src, do_indent);
@ -670,7 +667,6 @@ int main(int argc, char *argv[]) {
if (fh) { if (fh) {
std::fputws(output_wtext.c_str(), fh); std::fputws(output_wtext.c_str(), fh);
fclose(fh); fclose(fh);
exit(0);
} else { } else {
std::fwprintf(stderr, _(L"Opening \"%s\" failed: %s\n"), output_location, std::fwprintf(stderr, _(L"Opening \"%s\" failed: %s\n"), output_location,
std::strerror(errno)); std::strerror(errno));
@ -693,5 +689,7 @@ int main(int argc, char *argv[]) {
} }
std::fputws(str2wcstring(colored_output).c_str(), stdout); std::fputws(str2wcstring(colored_output).c_str(), stdout);
return ret; }
}
return 0;
} }