My Second Post
 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
#!/usr/bin/env python3
r"""
File:        write-good-wrapper
Author:      Neal Joslin
Date:        2023-05-21
Email:       nealiumj@gmail.com
Description: Wrapper for the `write-good` command for Vim Quickfix

Notes:
    Vim Settings:
    `setlocal makeprg=write-good-wrapper\ %`
    `setlocal errorformat=%A%f:%l:%c:%m`
"""
import re
import sys

from subprocess import run


def write_good_wrapper(file_path: str) -> None:
    """
    Run write-good command, filter out unusable lines and put into format that
    can be easily parsed by vim's `errorformat`

    Output:
        {file_path}:{line_number}:{column_number}:{msg}

    Args:
        file_path (str): File to lint
    """
    cmd = f'write-good {file_path}'
    data = run(cmd, capture_output=True, shell=True, check=False)
    output = data.stdout.splitlines()

    for line in output:
        result = re.search(
            r'(.*) on line (\d+) at column (\d+)', line.decode('utf-8'))
        # "therefore" is wordy or unneeded on line 3 at column 132
        if result:
            print(
                f'{file_path}:'
                f'{result.group(2)}:'
                f'{result.group(3)}:'
                'e:'
                f'{result.group(1)}'
            )


def main() -> None:
    """Loop through given files or prompt usage if none are given"""
    if len(sys.argv) >= 2:
        for file in sys.argv[1:]:
            write_good_wrapper(file)
    else:
        print(
            'Usage:\n'
            '\twrite-good-wrapper <file1>\n'
            '\twrite-good-wrapper <file1> <file2> <file{x}>\n'
        )
        sys.exit(1)


if __name__ == '__main__':
    main()
Post:
2/2
comments powered by Disqus