diff --git a/doc_src/cmds/fish_indent.rst b/doc_src/cmds/fish_indent.rst index ffc15d29f..444185d38 100644 --- a/doc_src/cmds/fish_indent.rst +++ b/doc_src/cmds/fish_indent.rst @@ -22,6 +22,8 @@ The following options are available: - ``-i`` or ``--no-indent`` do not indent commands; only reformat to one job per line. +- ``-c`` or ``--check`` do not indent, only return 0 if the code is already indented as fish_indent would, 1 otherwise. + - ``-v`` or ``--version`` displays the current fish version and then exits. - ``--ansi`` colorizes the output using ANSI escape sequences, appropriate for the current $TERM, using the colors defined in the environment (such as ``$fish_color_command``). diff --git a/src/fish_indent.cpp b/src/fish_indent.cpp index 1074e2f1b..6ff842703 100644 --- a/src/fish_indent.cpp +++ b/src/fish_indent.cpp @@ -836,12 +836,13 @@ int main(int argc, char *argv[]) { output_type_file, output_type_ansi, output_type_pygments_csv, + output_type_check, output_type_html } output_type = output_type_plain_text; const char *output_location = ""; bool do_indent = true; - const char *short_opts = "+d:hvwiD:"; + const char *short_opts = "+d:hvwicD:"; const struct option long_opts[] = {{"debug-level", required_argument, nullptr, 'd'}, {"debug-stack-frames", required_argument, nullptr, 'D'}, {"dump-parse-tree", no_argument, nullptr, 'P'}, @@ -852,6 +853,7 @@ int main(int argc, char *argv[]) { {"html", no_argument, nullptr, 1}, {"ansi", no_argument, nullptr, 2}, {"pygments", no_argument, nullptr, 3}, + {"check", no_argument, nullptr, 'c'}, {nullptr, 0, nullptr, 0}}; int opt; @@ -889,6 +891,10 @@ int main(int argc, char *argv[]) { output_type = output_type_pygments_csv; break; } + case 'c': { + output_type = output_type_check; + break; + } case 'd': { char *end; long tmp; @@ -997,6 +1003,12 @@ int main(int argc, char *argv[]) { case output_type_pygments_csv: { DIE("pygments_csv should have been handled above"); } + case output_type_check: { + if (output_wtext != src) { + return 1; + } + break; + } } std::fputws(str2wcstring(colored_output).c_str(), stdout); diff --git a/tests/checks/indent.fish b/tests/checks/indent.fish index 2c9548219..af871a74b 100644 --- a/tests/checks/indent.fish +++ b/tests/checks/indent.fish @@ -318,3 +318,10 @@ command 2' | $fish_indent #CHECK: {{^}}command 1 | #CHECK: {{^ }}command 1 cont || #CHECK: {{^}}command 2 + +echo " foo" | fish_indent --check +echo $status +#CHECK: 1 +echo "foo" | fish_indent --check +echo $status +#CHECK: 0