Commit 7964f69c authored by ∞'s avatar 💻

Add Table

parent d58dfc48
Pipeline #7933 passed with stages
in 41 seconds
......@@ -5,3 +5,4 @@ from .command import Command
from .decorators import arg, cmd
from .group import Group
from .node import Node
from .table import Table
......@@ -167,11 +167,11 @@ class Argument:
out += ' '
self.cmd.print(out)
if self.param.default != self.param.empty:
if self.default or self.param.default != self.param.empty:
self.cmd.print(
'Default: '
+ colors.blue3
+ str(self.param.default)
+ str(self.default or self.param.default)
+ colors.reset
)
......
......@@ -105,31 +105,19 @@ class Group(EntryPoint, dict):
self.print(self.doc.strip())
self.print()
documented = []
undocumented = []
for name, command in self.items():
doc = command.help(short=True)
cmd = (
getattr(colors, command.color, command.color),
name,
doc,
from .table import Table
table = Table(['command', 'help'], header=False, *[
(
(
getattr(colors, command.color, command.color),
name,
),
command.help(short=True),
)
if doc:
documented.append(cmd)
else:
undocumented.append(cmd)
if documented:
self.print('ORANGE', 'DOCUMENTED SUB-COMMANDS')
for cmd in documented:
self.print(cmd[0] + cmd[1] + colors.reset)
self.print(cmd[2] + '\n')
if undocumented:
self.print('ORANGE', 'UNDOCUMENTED SUB-COMMANDS')
for cmd in undocumented:
self.print(cmd[0] + cmd[1] + colors.reset)
for name, command in self.items()
])
self.print('ORANGE', 'SUB-COMMANDS')
table.print(self.print)
help.cli2 = dict(color='green')
def load(self, obj, parent=None, public=True):
......
import os
from .colors import colors
class Column:
def __init__(self, name, maxlength=None):
self.name = name
self.maxlength = maxlength or 0
self.minlength = len(name)
class Table(list):
def __init__(self, columns, *args, header=True):
self.columns = [
Column(**c)
if isinstance(c, dict)
else Column(c)
for c in columns
]
self.header = header
super().__init__(args)
def print(self, print_function=None):
print_function = print_function or print
# set maxlength value length and maxlength for each column based on data
for row in self:
for colnum, item in enumerate(row):
data = item
if isinstance(data, (list, tuple)):
color = data[0]
data = data[1]
else:
color = ''
data = item
length = len(data)
if length > self.columns[colnum].maxlength:
self.columns[colnum].maxlength = length
# shrink last column if necessary
try:
size = os.get_terminal_size().columns
except:
size = 80
sumsize = sum([c.maxlength for c in self.columns]) + len(self.columns)
maxsize = max([c.maxlength for c in self.columns])
if sumsize > size:
_ = sum([c.maxlength for c in self.columns[:-1]]) - len(self.columns)
self.columns[-1].maxlength = size - _ - len(self.columns) - 1
if self.header:
line = []
for column in self.columns:
spaces = column.maxlength - len(column.name)
left = int(spaces / 2)
right = spaces - left
line.append(
' ' * left
+ column.name
+ ' ' * right
)
print_function(' '.join(line))
line = []
for column in self.columns:
line.append(
'=' * column.maxlength
)
print_function(' '.join(line))
rows = list(self)
while rows:
row = rows.pop(0)
line = []
leftovers = [[] for c in self.columns]
for colnum, column in enumerate(self.columns):
data = row[colnum]
if isinstance(data, (list, tuple)):
color = data[0]
data = data[1]
else:
color = ''
data = row[colnum]
datawords = data.split(' ')
words = []
for dataword in datawords:
if len(' '.join(words + [dataword])) <= column.maxlength:
words.append(dataword)
else:
leftovers[colnum].append(dataword)
data = ' '.join(words)
line.append(
color
+ data
+ ' ' * (column.maxlength - len(data))
+ colors.reset
)
for leftover in leftovers:
if len(leftover):
rows.insert(0, [' '.join(l) for l in leftovers])
break
print_function(' '.join(line))
......@@ -19,7 +19,5 @@ Call cli2.test_node.example_function with args=['x'] and kwargs=dict(y='z')
cli2 cli2.test_node.example_function x y=z
DOCUMENTED SUB-COMMANDS
help
Get help for a dotted path
SUB-COMMANDS
help Get help for a dotted path
......@@ -17,7 +17,5 @@ Call cli2.test_node.example_function with args=['x'] and kwargs=dict(y='z')
cli2 cli2.test_node.example_function x y=z
DOCUMENTED SUB-COMMANDS
help
Get help for a dotted path
SUB-COMMANDS
help Get help for a dotted path
......@@ -9,30 +9,23 @@ cli2 help cli2.test_node SUB-COMMAND
DESCRIPTION
Test cases for cli2.Node
DOCUMENTED SUB-COMMANDS
help
Get help for a command or group
Node
A Python node, used to browse python module programatically for callables
example_function
Example function docstring where the first sentence unfortunnately spreads over the next line
example_object
Represents a group of named commands
UNDOCUMENTED SUB-COMMANDS
ExampleClass
ExampleClassCallable
example_object_callable
test_callable_class
test_callable_object
test_class
test_dict
test_eq
test_function
test_list
test_module
test_object
test_unknown
SUB-COMMANDS
help  Get help for a command or group 
ExampleClass  
ExampleClassCallable  
Node  A Python node, used to browse python module for 
 programatically callables 
example_function  Example function docstring where the first sentence over
 unfortunnately spreads the next line 
example_object  Represents a group of named commands 
example_object_callable 
test_callable_class  
test_callable_object  
test_class  
test_dict  
test_eq  
test_function  
test_list  
test_module  
test_object  
test_unknown  
......@@ -9,9 +9,6 @@ cli2 help cli2.test_node.example_object SUB-COMMAND
DESCRIPTION
Represents a group of named commands.
DOCUMENTED SUB-COMMANDS
help
Get help for a command or group
UNDOCUMENTED SUB-COMMANDS
example_method
SUB-COMMANDS
help  Get help for a command or group
example_method 
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment