Skip to content

Commit

Permalink
feat: Add 'zero' argument to change character for 0
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Sep 4, 2020
1 parent db411b7 commit 1c13c00
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
14 changes: 13 additions & 1 deletion src/dependenpy/cli.py
Expand Up @@ -18,6 +18,8 @@
import argparse
import sys

from colorama import init

from . import __version__
from .dsm import DSM
from .helpers import CSV, FORMAT, JSON, guess_depth
Expand Down Expand Up @@ -76,11 +78,14 @@ def main(args=None):
# special case for json.dumps indent argument
indent = None

# init colorama
init()

try:
if args.dependencies:
dsm.print(format=args.format, output=output, indent=indent)
elif args.matrix:
dsm.print_matrix(format=args.format, output=output, depth=depth, indent=indent)
dsm.print_matrix(format=args.format, output=output, depth=depth, indent=indent, zero=args.zero)
elif args.treemap:
dsm.print_treemap(format=args.format, output=output)
elif args.graph:
Expand Down Expand Up @@ -184,4 +189,11 @@ def get_parser():
version="dependenpy %s" % __version__,
help="Show the current version of the program and exit.",
)
parser.add_argument(
"-z",
"--zero",
dest="zero",
default="0",
help="Character to use for cells with value=0 (text matrix display only).",
)
return parser
4 changes: 4 additions & 0 deletions src/dependenpy/helpers.py
Expand Up @@ -25,6 +25,10 @@ def print(self, format=TEXT, output=sys.stdout, **kwargs):
"""
if format is None:
format = TEXT

if format != TEXT:
kwargs.pop("zero", "")

if format == TEXT:
print(self._to_text(**kwargs), file=output)
elif format == CSV:
Expand Down
24 changes: 15 additions & 9 deletions src/dependenpy/structures.py
Expand Up @@ -4,6 +4,8 @@

import json

from colorama import Back, Fore, Style

from .helpers import PrintMixin


Expand Down Expand Up @@ -101,28 +103,32 @@ def _to_json(self, **kwargs):
def _to_text(self, **kwargs):
if not self.keys or not self.data:
return ""
zero = kwargs.pop("zero", "0")
max_key_length = max(len(k) for k in self.keys)
max_dep_length = len(str(max(j for i in self.data for j in i)))
max_dep_length = max([len(str(c)) for l in self.data for c in l] + [len(zero)])
key_col_length = len(str(len(self.keys)))
key_line_length = max(key_col_length, 2)
column_length = max(key_col_length, max_dep_length)
bold = Style.BRIGHT
reset = Style.RESET_ALL

# first line left headers
text = [("\n {:>%s} | {:>%s} ||" % (max_key_length, key_line_length)).format("Module", "Id")]
text = [f"\n {bold}{'Module':>{max_key_length}}{reset}{bold}{'Id':>{key_line_length}}{reset} │"]
# first line column headers
for i, _ in enumerate(self.keys):
text.append(("{:^%s}|" % column_length).format(i))
text.append(f"{bold}{i:^{column_length}}{reset}│")
text.append("\n")
# line of dashes
text.append((" %s-+-%s-++" % ("-" * max_key_length, "-" * key_line_length)))
for i, _ in enumerate(self.keys):
text.append("%s+" % ("-" * column_length))
text.append(f" {'─' * max_key_length}─┼─{'─' * key_line_length}─┼")
for _ in range(len(self.keys) - 1):
text.append(f"{'─' * column_length}┼")
text.append(f"{'─' * column_length}┤")
text.append("\n")
# lines
for i, k in enumerate(self.keys):
text.append((" {:>%s} | {:>%s} ||" % (max_key_length, key_line_length)).format(k, i))
text.append(f" {k:>{max_key_length}}{bold}{i:>{key_line_length}}{reset} │")
for v in self.data[i]:
text.append(("{:>%s}|" % column_length).format(v))
text.append(("{:>%s}" % column_length).format(v if v else zero))
text.append("\n")
text.append("\n")

Expand Down Expand Up @@ -321,7 +327,7 @@ def _to_json(self, **kwargs):
for edge in self.edges
],
},
**kwargs
**kwargs,
)

def _to_text(self, **kwargs):
Expand Down

0 comments on commit 1c13c00

Please sign in to comment.