mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-27 05:13:10 +00:00
add option to modify script being restyled
This change allows the user to specify the script name on the CLI in addition to being redirected from stdin. It also adds a `-w` flag to write the modified script to the original file.
This commit is contained in:
parent
85e701f422
commit
2606cfe72d
2 changed files with 60 additions and 18 deletions
|
@ -7,12 +7,14 @@ fish_indent [OPTIONS]
|
||||||
|
|
||||||
\subsection fish_indent-description Description
|
\subsection fish_indent-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.
|
`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.
|
||||||
|
|
||||||
The following options are available:
|
The following options are available:
|
||||||
|
|
||||||
- `-d` or `--dump` dumps information about the parsed fish commands to stderr
|
- `-d` or `--dump` dumps information about the parsed fish commands to stderr
|
||||||
|
|
||||||
|
- `-w` or `--write` indents a specified file and immediately writes to that file
|
||||||
|
|
||||||
- `-i` or `--no-indent` do not indent commands; only reformat to one job per line
|
- `-i` or `--no-indent` do not indent commands; only reformat to one job per line
|
||||||
|
|
||||||
- `-v` or `--version` displays the current fish version and then exits
|
- `-v` or `--version` displays the current fish version and then exits
|
||||||
|
|
|
@ -18,12 +18,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||||
#include "config.h" // IWYU pragma: keep
|
#include "config.h" // IWYU pragma: keep
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <wchar.h>
|
#include <wchar.h>
|
||||||
#include <wctype.h>
|
#include <wctype.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
@ -164,10 +166,9 @@ static void prettify_node_recursive(const wcstring &source, const parse_node_tre
|
||||||
// Entry point for prettification.
|
// Entry point for prettification.
|
||||||
static wcstring prettify(const wcstring &src, bool do_indent) {
|
static wcstring prettify(const wcstring &src, bool do_indent) {
|
||||||
parse_node_tree_t tree;
|
parse_node_tree_t tree;
|
||||||
if (!parse_tree_from_string(src,
|
int parse_flags = (parse_flag_continue_after_error | parse_flag_include_comments |
|
||||||
parse_flag_continue_after_error | parse_flag_include_comments |
|
parse_flag_leave_unterminated | parse_flag_show_blank_lines);
|
||||||
parse_flag_leave_unterminated | parse_flag_show_blank_lines,
|
if (!parse_tree_from_string(src, parse_flags, &tree, NULL)) {
|
||||||
&tree, NULL /* errors */)) {
|
|
||||||
// We return the initial string on failure.
|
// We return the initial string on failure.
|
||||||
return src;
|
return src;
|
||||||
}
|
}
|
||||||
|
@ -339,26 +340,27 @@ int main(int argc, char *argv[]) {
|
||||||
// Types of output we support.
|
// Types of output we support.
|
||||||
enum {
|
enum {
|
||||||
output_type_plain_text,
|
output_type_plain_text,
|
||||||
|
output_type_file,
|
||||||
output_type_ansi,
|
output_type_ansi,
|
||||||
output_type_html
|
output_type_html
|
||||||
} output_type = output_type_plain_text;
|
} output_type = output_type_plain_text;
|
||||||
|
const char *output_location;
|
||||||
bool do_indent = true;
|
bool do_indent = true;
|
||||||
|
|
||||||
const char *short_opts = "+dhvi";
|
const char *short_opts = "+dhvwi";
|
||||||
const struct option long_opts[] = {{"dump", no_argument, NULL, 'd'},
|
const struct option long_opts[] = {
|
||||||
{"no-indent", no_argument, NULL, 'i'},
|
{"dump", no_argument, NULL, 'd'}, {"no-indent", no_argument, NULL, 'i'},
|
||||||
{"help", no_argument, NULL, 'h'},
|
{"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'v'},
|
||||||
{"version", no_argument, NULL, 'v'},
|
{"write", no_argument, NULL, 'w'}, {"html", no_argument, NULL, 1},
|
||||||
{"html", no_argument, NULL, 1},
|
{"ansi", no_argument, NULL, 2}, {NULL, 0, NULL, 0}};
|
||||||
{"ansi", no_argument, NULL, 2},
|
|
||||||
{NULL, 0, NULL, 0}};
|
|
||||||
|
|
||||||
int opt;
|
int opt;
|
||||||
while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
|
while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 0: {
|
case 0: {
|
||||||
fwprintf(stderr, _(L"getopt_long() unexpectedly returned zero\n"));
|
fwprintf(stderr, _(L"getopt_long() unexpectedly returned zero\n"));
|
||||||
exit_without_destructors(127);
|
exit(127);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
case 'd': {
|
case 'd': {
|
||||||
dump_parse_tree = true;
|
dump_parse_tree = true;
|
||||||
|
@ -366,12 +368,16 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
case 'h': {
|
case 'h': {
|
||||||
print_help("fish_indent", 1);
|
print_help("fish_indent", 1);
|
||||||
exit_without_destructors(0);
|
exit(0);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
case 'v': {
|
case 'v': {
|
||||||
fwprintf(stderr, _(L"%ls, version %s\n"), program_name, get_fish_version());
|
fwprintf(stderr, _(L"%ls, version %s\n"), program_name, get_fish_version());
|
||||||
exit(0);
|
exit(0);
|
||||||
assert(0 && "Unreachable code reached");
|
break;
|
||||||
|
}
|
||||||
|
case 'w': {
|
||||||
|
output_type = output_type_file;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'i': {
|
case 'i': {
|
||||||
|
@ -388,12 +394,33 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
// We assume getopt_long() has already emitted a diagnostic msg.
|
// We assume getopt_long() has already emitted a diagnostic msg.
|
||||||
exit_without_destructors(1);
|
exit(1);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const wcstring src = read_file(stdin);
|
argc -= optind;
|
||||||
|
argv += optind;
|
||||||
|
|
||||||
|
wcstring src;
|
||||||
|
if (argc == 0) {
|
||||||
|
src = read_file(stdin);
|
||||||
|
} else if (argc == 1) {
|
||||||
|
FILE *fh = fopen(*argv, "r");
|
||||||
|
if (fh) {
|
||||||
|
src = read_file(fh);
|
||||||
|
fclose(fh);
|
||||||
|
output_location = *argv;
|
||||||
|
} else {
|
||||||
|
fwprintf(stderr, _(L"Opening \"%s\" failed: %s\n"), *argv, strerror(errno));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fwprintf(stderr, _(L"Too many arguments\n"));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
const wcstring output_wtext = prettify(src, do_indent);
|
const wcstring output_wtext = prettify(src, do_indent);
|
||||||
|
|
||||||
// Maybe colorize.
|
// Maybe colorize.
|
||||||
|
@ -409,6 +436,19 @@ int main(int argc, char *argv[]) {
|
||||||
colored_output = no_colorize(output_wtext);
|
colored_output = no_colorize(output_wtext);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case output_type_file: {
|
||||||
|
FILE *fh = fopen(output_location, "w");
|
||||||
|
if (fh) {
|
||||||
|
fputs(wcs2str(output_wtext), fh);
|
||||||
|
fclose(fh);
|
||||||
|
exit(0);
|
||||||
|
} else {
|
||||||
|
fwprintf(stderr, _(L"Opening \"%s\" failed: %s\n"), output_location,
|
||||||
|
strerror(errno));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
case output_type_ansi: {
|
case output_type_ansi: {
|
||||||
colored_output = ansi_colorize(output_wtext, colors);
|
colored_output = ansi_colorize(output_wtext, colors);
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in a new issue