|
3 | 3 | import argparse |
4 | 4 | from collections.abc import Callable, Generator, Iterable, Mapping, Sequence |
5 | 5 | import contextlib |
| 6 | +from datetime import datetime |
6 | 7 | import itertools |
7 | 8 | import logging |
8 | 9 | from pathlib import Path |
|
17 | 18 | from mdformat._util import atomic_write, detect_newline_type, is_md_equal |
18 | 19 | import mdformat.plugins |
19 | 20 | import mdformat.renderer |
| 21 | +from src.mdformat._output import diff |
20 | 22 |
|
21 | 23 | # Match "\r" and "\n" characters that are not part of a "\r\n" sequence |
22 | 24 | RE_NON_CRLF_LINE_END = re.compile(r"(?:[^\r]|^)\n|\r(?:[^\n]|\Z)") |
@@ -92,6 +94,17 @@ def run(cli_args: Sequence[str]) -> int: # noqa: C901 |
92 | 94 | ): |
93 | 95 | format_errors_found = True |
94 | 96 | print_error(f'File "{path_str}" is not formatted.') |
| 97 | + |
| 98 | + if opts["diff"]: |
| 99 | + then = datetime.utcfromtimestamp(path.stat().st_mtime) |
| 100 | + now = datetime.utcnow() |
| 101 | + src_name = f"{path}\t{then} +0000" |
| 102 | + dst_name = f"{path}\t{now} +0000" |
| 103 | + |
| 104 | + diff_contents = diff( |
| 105 | + original_str, formatted_str, src_name, dst_name |
| 106 | + ) |
| 107 | + print(diff_contents) |
95 | 108 | else: |
96 | 109 | if not changes_ast and not is_md_equal( |
97 | 110 | original_str, |
@@ -146,6 +159,11 @@ def make_arg_parser( |
146 | 159 | parser.add_argument( |
147 | 160 | "--check", action="store_true", help="do not apply changes to files" |
148 | 161 | ) |
| 162 | + parser.add_argument( |
| 163 | + "--diff", |
| 164 | + action="store_true", |
| 165 | + help="show diff of what would be changed when running with --check", |
| 166 | + ) |
149 | 167 | version_str = f"mdformat {mdformat.__version__}" |
150 | 168 | if plugin_versions_str: |
151 | 169 | version_str += f" ({plugin_versions_str})" |
|
0 commit comments