Skip to content

feature provide column headers via parameter for asciitable #590

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

Open
DEvil0000 opened this issue Sep 9, 2024 · 5 comments
Open

feature provide column headers via parameter for asciitable #590

DEvil0000 opened this issue Sep 9, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@DEvil0000
Copy link

I would like to provide column headers via a parameter for asciitable.
also defining a separator char might be helpfull.

example command:
cat $file | jc --asciitable --headers="a b" --separator=" "

example input file:
aaaaaa xyz
bbbbb uvw

example output:
[{"a": "aaaaaa", "b": "xyz"},{"a": "bbbbb", "b": "uvw"}]

@kellyjonbrazil kellyjonbrazil added the enhancement New feature or request label Sep 9, 2024
@snoblenet
Copy link

Indeed.

Either that, or a new parser for headerless ASCII tables that returns an array of arrays:

[
  ['row_1_col_1', 'row_1_col_2', 'row_1_col_3'],
  ['row_2_col_1', 'row_2_col_2', 'row_2_col_3'],
  ['row_3_col_1', 'row_3_col_2', 'row_3_col_3'],
]

Use cases would include:

mas list | jc --headerless-asciitable

@snoblenet
Copy link

snoblenet commented May 28, 2025

We'd need a robust, generalisable solution in Python, but here's a naive, MAS-specific implementation as a shellscript:

LIST=$(
  mas list |
    awk '
      {
        id = $1
        match($0, /\([0-9]/)
        version = substr($0, RSTART)
        gsub(/\(/, "",version)
        gsub(/\)/, "",version)
        name = substr($0, index($0, $2), RSTART - index($0, $2) - 1)
        gsub(/^[ \t]+|[ \t]+$/, "", name)
        print "[\"" id "\", \"" name "\", \"" version "\"],"
      }' |
    sed '$s/,\s*$//'
)

echo "[\n$LIST\n]" | jq -s '[.[][] | {id: .[0], name: .[1], version: .[2]}]'

The naive, MAS-specific implementation returns an object like this:

[
  {
    "id": "1352778147",
    "name": "Bitwarden",
    "version": "2025.4.2"
  },
  {
    "id": "6448461551",
    "name": "Command X",
    "version": "1.5.1"
  },
  {
    "id": "409222199",
    "name": "Cyberduck",
    "version": "9.1.4"
  },
  {
    "id": "1572239625",
    "name": "EPUB Viewer Pro",
    "version": "5.1.2"
  },
  {
    "id": "880764359",
    "name": "Export for iTunes",
    "version": "3.7.6"
  },
  {
    "id": "1289583905",
    "name": "Pixelmator Pro",
    "version": "3.6.18"
  },
  {
    "id": "1611378436",
    "name": "Pure Paste",
    "version": "1.12.1"
  },
  {
    "id": "1522267256",
    "name": "Shareful",
    "version": "1.11.0"
  },
  {
    "id": "507257563",
    "name": "Sip",
    "version": "4.5.2"
  },
  {
    "id": "803453959",
    "name": "Slack",
    "version": "4.44.60"
  },
  {
    "id": "1666327168",
    "name": "Spaced",
    "version": "1.2.0"
  },
  {
    "id": "6636491400",
    "name": "Text Capture",
    "version": "2.0"
  },
  {
    "id": "425424353",
    "name": "The Unarchiver",
    "version": "4.3.9"
  },
  {
    "id": "1463298887",
    "name": "Userscripts",
    "version": "4.7.1"
  },
  {
    "id": "1607635845",
    "name": "Velja",
    "version": "2.1.1"
  },
  {
    "id": "310633997",
    "name": "WhatsApp",
    "version": "25.15.75"
  },
  {
    "id": "497799835",
    "name": "Xcode",
    "version": "16.3"
  }
]

@kellyjonbrazil
Copy link
Owner

kellyjonbrazil commented May 28, 2025

One way to do this would be by using environment variables. This way we don't need to add arguments to jc:

$ JC_ASCIITABLE_COLS="column1,column2,column3" cat $file | jc --asciitable

We would just need to add env variable functionality to the existing parser code or create new, similar parsers with the capability.

@kellyjonbrazil
Copy link
Owner

kellyjonbrazil commented May 28, 2025

In fact, the parse() function in lib.py already accepts *kwargs, so when using as a library you could call the parser with parser-specific arguments:

jc/jc/lib.py

Lines 392 to 399 in ac8120e

def parse(
parser_mod_name: Union[str, ModuleType],
data: Union[str, bytes, Iterable[str]],
quiet: bool = False,
raw: bool = False,
ignore_exceptions: Optional[bool] = None,
**kwargs
) -> Union[JSONDictType, List[JSONDictType], Iterator[JSONDictType]]:

So the parser's parse() function could be extended to use a new kwarg for something like this. Within the function you would allow it to accept the passed value or pull it from the environment variable for jc command-line support. It would be a heavy lift to add additional argument support in the jc command-line so I think environment variables would be the best solution there.

@snoblenet
Copy link

Promising. It's years since I've done anything meaningful with Python so I won't volunteer my own efforts on this one, if that's OK.

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

No branches or pull requests

3 participants