Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command line client? [feature request] #235

Open
con-f-use opened this issue Apr 9, 2023 · 0 comments
Open

Command line client? [feature request] #235

con-f-use opened this issue Apr 9, 2023 · 0 comments

Comments

@con-f-use
Copy link

con-f-use commented Apr 9, 2023

Is your feature request related to a problem? Please describe.

The column command in util-linux lacks a bunch of features that PrettyTable has. It is also not too cross-platform compatible.

Maybe PrettyTable sould define a console_script/project_script entry point and implement a CLI so we can have nice tables in shell scripts.

Describe the solution

Here is a simple version of what I mean:

# t.py
# Something like this but more elaborate should be part of PrettyTable

import prettytable, sys


def read_data(infile, sep=None):
    with (sys.stdin if infile == "-" else open(infile)) as fh:
        for x in fh:
            yield x.split(sep)

def make_table(data):
    table = prettytable.PrettyTable()
    table.field_names = next(data)

    for row in data:
        if len(row) != len(table.field_names):
            continue
        table.add_row(row)

    return table

def main(args=None, style=prettytable.SINGLE_BORDER):
    if args is None:
        args = sys.argv[1:]

    infile = args[0] if args else "-"
    data = read_data(infile)
    table = make_table(data)
    table.set_style(style)

    print(table)

if __name__ == "__main__":
    ret = main()
    raise SystemExit(ret)

In a final implementation, the python t.py call would of course be something like pretty-table --style msword_friednly --seperator '\t' --output-format json --input-format csv or something. But for my minimal example:

$ $ echo -e "a b c d\n1 2 3 4\n5 6 7 8\n" | python t.py
┌───┬───┬───┬───┐
│ a │ b │ c │ d │
├───┼───┼───┼───┤
│ 1 │ 2 │ 3 │ 4 │
│ 5 │ 6 │ 7 │ 8 │
└───┴───┴───┴───┘

Something like it could be part of PrettyTable and of course implement more options such as --separator '\t' or --style msword_friednly. The implimentation would like be a console_script entrypoint or project.scripts entry point.

If you think something like this might get merged, I might open a PR. Beforehand, there would be some questions, like for example which argument parser to use (click, argparse) or what the command's name should be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant