Skip to content

Commit

Permalink
Merge pull request #24 from kellyjonbrazil/dev
Browse files Browse the repository at this point in the history
v1.6.0
  • Loading branch information
kellyjonbrazil authored Dec 12, 2023
2 parents f59b955 + 8a32d76 commit 514863e
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 42 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
jtbl changelog

20231210 v1.6.0
- Add long options
- Add DocuWiki table option
- Add Bash and Zsh completions

20231022 v1.5.3
- Add `-f` option for fancy table output

Expand Down
17 changes: 17 additions & 0 deletions completions/jtbl_bash_completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
_jtbl()
{
OPTIONS=(--cols -c --csv -d --dokuwiki -f --fancy -h --help -H --html -m --markdown -n --no-wrap -q --quiet -r --rotate -t --truncate -v --version)
MOD_OPTIONS=(--cols -n --no-wrap -q --quiet -t --truncate)

COMPREPLY=()
_get_comp_words_by_ref cur prev words cword

if [ "${#words[@]}" != "2" ]; then
COMPREPLY=($(compgen -W "${MOD_OPTIONS[*]}" -- "${cur}"))
return 0
else
COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "${cur}"))
return 0
fi
} &&
complete -F _jtbl jtbl
34 changes: 34 additions & 0 deletions completions/jtbl_zsh_completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#compdef jtbl

_jtbl() {
jtbl_options_describe=(
'--cols:manually configure the terminal width'
'-c:CSV table output'
'--csv:CSV table output'
'-d:DokuWiki table output'
'--dokuwiki:DokuWiki table output'
'-f:fancy table output'
'--fancy:fancy table output'
'-h:help'
'--help:help'
'-H:HTML table output'
'--html:HTML table output'
'-m:markdown table output'
'--markdown:markdown table output'
'-n:do not try to wrap if too wide for the terminal'
'--no-wrap:do not try to wrap if too wide for the terminal'
"-q:quiet - don't print error messages"
"--quiet:quiet - don't print error messages"
'-r:rotate table output'
'--rotate:rotate table output'
'-t:truncate data if too wide for the terminal'
'--truncate:truncate data if too wide for the terminal'
'-v:version info'
'--version:version info'
)

_describe 'commands' jtbl_options_describe
return 0
}

_jtbl
80 changes: 51 additions & 29 deletions jtbl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,24 @@
import tabulate
import shutil

__version__ = '1.5.3'
__version__ = '1.6.0'
SUCCESS, ERROR = True, False

# START add DokuWiki table format
dokuwiki_format = {
"dokuwiki": tabulate.TableFormat(
lineabove=tabulate.Line("|", "-", "|", "|"),
linebelowheader=tabulate.Line("|", "-", "|", "|"),
linebetweenrows=None,
linebelow=None,
headerrow=tabulate.DataRow("^", "^", "^"),
datarow=tabulate.DataRow("|", "|", "|"),
padding=1,
with_header_hide=["lineabove", "linebelowheader"],
)
}
tabulate._table_formats.update(dokuwiki_format) # type: ignore
# END add DokuWiki table format

def ctrlc(signum, frame):
"""exit with error on SIGINT"""
Expand All @@ -30,17 +45,18 @@ def helptext():
Usage: <JSON Data> | jtbl [OPTIONS]
--cols=n manually configure the terminal width
-c CSV table output
-f fancy table output
-h help
-H HTML table output
-m markdown table output
-n no-wrap - do not try to wrap if too wide for the terminal
-q quiet - don't print error messages
-r rotate table output
-t truncate data if too wide for the terminal
-v version info
--cols=n manually configure the terminal width
-c, --csv CSV table output
-d, --dokuwiki DokuWiki table output
-f, --fancy fancy table output
-h, --help help
-H, --html HTML table output
-m, --markdown markdown table output
-n, --no-wrap do not try to wrap if too wide for the terminal
-q, --quiet quiet - don't print error messages
-r, --rotate rotate table output
-t, --truncate truncate data if too wide for the terminal
-v, --version version info
'''))


Expand Down Expand Up @@ -299,33 +315,39 @@ def main():
options.extend(arg[1:])

if arg.startswith('--'):
try:
k, v = arg[2:].split('=')
long_options[k] = int(v)
except Exception:
helptext()

csv = 'c' in options
html = 'H' in options
markdown = 'm' in options
fancy_grid = 'f' in options
nowrap = 'n' in options
quiet = 'q' in options
rotate = 'r' in options
truncate = 't' in options
version_info = 'v' in options
helpme = 'h' in options
if '=' in arg:
try:
k, v = arg[2:].split('=')
long_options[k] = int(v)
except Exception:
helptext()
else:
long_options[arg[2:]] = None

csv = 'c' in options or 'csv' in long_options
dokuwiki = 'd' in options or 'dokuwiki' in long_options
html = 'H' in options or 'html' in long_options
markdown = 'm' in options or 'markdown' in long_options
fancy_grid = 'f' in options or 'fancy' in long_options
nowrap = 'n' in options or 'no-wrap' in long_options
quiet = 'q' in options or 'quiet' in long_options
rotate = 'r' in options or 'rotate' in long_options
truncate = 't' in options or 'truncate' in long_options
version_info = 'v' in options or 'version' in long_options
helpme = 'h' in options or 'help' in long_options

if markdown:
tbl_fmt = 'github'
elif dokuwiki:
tbl_fmt = 'dokuwiki'
elif html:
tbl_fmt = 'html'
elif fancy_grid:
tbl_fmt = 'fancy_grid'
else:
tbl_fmt = 'simple'

if not rotate and (markdown or html or csv):
if not rotate and (markdown or dokuwiki or html or csv):
nowrap = True

columns = None
Expand Down
26 changes: 14 additions & 12 deletions man/jtbl.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH jtbl 1 2023-10-22 1.5.3 "JTBL - JSON tables in the terminal"
.TH jtbl 1 2023-11-24 1.6.0 "JTBL - JSON tables in the terminal"
.SH NAME
jtbl \- Print JSON and JSON Lines data as a table in the terminal
.SH SYNOPSIS
Expand All @@ -18,27 +18,29 @@ cat data.json | jtbl [OPTIONS]
.PP

.SS Options
\fB--cols=n\fP manually configure the terminal width
\fB--cols=n\fP manually configure the terminal width

\fB-c\fP CSV table output
\fB-c\fP, \fB--csv\fP CSV table output

\fB-h\fP help
\fB-d\fP, \fB--dokuwiki\fP DokuWiki table output

\fB-f\fP fancy table output
\fB-f\fP, \fB--fancy\fP fancy table output

\fB-H\fP HTML table output
\fB-h\fP, \fB--help\fP help

\fB-m\fP markdown table output
\fB-H\fP, \fB--html\fP HTML table output

\fB-n\fP no-wrap - do not try to wrap if too wide for the terminal
\fB-m\fP, \fB--markdown\fP markdown table output

\fB-q\fP quiet - don't print error messages
\fB-n\fP, \fB--no-wrap\fP do not try to wrap if too wide for the terminal

\fB-r\fP rotate table output
\fB-q\fP, \fB--quiet\fP quiet - don't print error messages

\fB-t\fP truncate data if too wide for the terminal
\fB-r\fP, \fB--rotate\fP rotate table output

\fB-v\fP version info
\fB-t\fP, \fB--truncate\fP truncate data if too wide for the terminal

\fB-v\fP, \fB--version\fP version info

.SS Example
.na
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='jtbl',
version='1.5.3',
version='1.6.0',
author='Kelly Brazil',
author_email='[email protected]',
description='A simple cli tool to print JSON and JSON Lines data as a table in the terminal.',
Expand Down
24 changes: 24 additions & 0 deletions tests/test_make_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,30 @@ def test_markdown(self):
self.assertEqual(jtbl.cli.make_table(data=stdin, columns=self.columns, nowrap=True, table_format='github'), (self.SUCCESS, expected))


def test_dokuwiki(self):
"""test DokuWiki markdown output"""
stdin = [
{
"column1": "data",
"column2": 123,
"column3": True,
"column4": None
},
{
"column1": "This is a long string that should not be truncated by the markdown table format. Lines should not be wrapped for markdown.",
"column2": 123,
"column3": True,
"column4": None
}
]

expected = textwrap.dedent('''\
^ column1 ^ column2 ^ column3 ^ column4 ^
| data | 123 | True | |
| This is a long string that should not be truncated by the markdown table format. Lines should not be wrapped for markdown. | 123 | True | |''')
self.assertEqual(jtbl.cli.make_table(data=stdin, columns=self.columns, nowrap=True, table_format='dokuwiki'), (self.SUCCESS, expected))


def test_add_remove_fields(self):
"""test with added and missing fields"""
stdin = [{"foo this is a very long long key":"this is a very very long string yes it is"},{"foo this is a very long long key":"medium length string","bar this is another very long string":"now is the time for all good men to come to the aide of their party"},{"baz is yet another long key name":"hello there how are you doing today? I am fine, thank you.","bar this is another very long string":"short string"}]
Expand Down

0 comments on commit 514863e

Please sign in to comment.