made ansibledoc more consistent with term sizes, it now uses display class column calculations

cleared up snippet display
pull/13012/merge
Brian Coca 9 years ago
parent 1027abd596
commit 5c7d717f31

@ -19,11 +19,8 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import fcntl
import datetime import datetime
import os import os
import struct
import termios
import traceback import traceback
import textwrap import textwrap
@ -117,9 +114,9 @@ class DocCLI(CLI):
doc['returndocs'] = returndocs doc['returndocs'] = returndocs
if self.options.show_snippet: if self.options.show_snippet:
text += DocCLI.get_snippet_text(doc) text += self.get_snippet_text(doc)
else: else:
text += DocCLI.get_man_text(doc) text += self.get_man_text(doc)
else: else:
# this typically means we couldn't even parse the docstring, not just that the YAML is busted, # this typically means we couldn't even parse the docstring, not just that the YAML is busted,
# probably a quoting issue. # probably a quoting issue.
@ -155,11 +152,7 @@ class DocCLI(CLI):
def get_module_list_text(self): def get_module_list_text(self):
tty_size = 0 columns = self.display.columns
if os.isatty(0):
tty_size = struct.unpack('HHHH',
fcntl.ioctl(0, termios.TIOCGWINSZ, struct.pack('HHHH', 0, 0, 0, 0)))[1]
columns = max(60, tty_size)
displace = max(len(x) for x in self.module_list) displace = max(len(x) for x in self.module_list)
linelimit = columns - displace - 5 linelimit = columns - displace - 5
text = [] text = []
@ -208,13 +201,15 @@ class DocCLI(CLI):
ret.append(i) ret.append(i)
return os.pathsep.join(ret) return os.pathsep.join(ret)
@staticmethod def get_snippet_text(self, doc):
def get_snippet_text(doc):
text = [] text = []
desc = CLI.tty_ify(doc['short_description']) desc = CLI.tty_ify(doc['short_description'])
text.append("- name: %s" % (desc)) text.append("- name: %s" % (desc))
text.append(" action: %s" % (doc['module'])) text.append(" action: %s" % (doc['module']))
pad = 31
subdent = ''.join([" " for a in xrange(pad)])
limit = self.display.columns - pad
for o in sorted(doc['options'].keys()): for o in sorted(doc['options'].keys()):
opt = doc['options'][o] opt = doc['options'][o]
@ -224,25 +219,25 @@ class DocCLI(CLI):
s = o + "=" s = o + "="
else: else:
s = o s = o
text.append(" %-20s # %s" % (s, textwrap.fill(desc, limit, subsequent_indent=subdent)))
text.append(" %-20s # %s" % (s, desc))
text.append('') text.append('')
return "\n".join(text) return "\n".join(text)
@staticmethod def get_man_text(self, doc):
def get_man_text(doc):
opt_indent=" " opt_indent=" "
text = [] text = []
text.append("> %s\n" % doc['module'].upper()) text.append("> %s\n" % doc['module'].upper())
pad = self.display.columns * 0.20
limit = max(self.display.columns - int(pad), 70)
if isinstance(doc['description'], list): if isinstance(doc['description'], list):
desc = " ".join(doc['description']) desc = " ".join(doc['description'])
else: else:
desc = doc['description'] desc = doc['description']
text.append("%s\n" % textwrap.fill(CLI.tty_ify(desc), initial_indent=" ", subsequent_indent=" ")) text.append("%s\n" % textwrap.fill(CLI.tty_ify(desc), limit, initial_indent=" ", subsequent_indent=" "))
if 'option_keys' in doc and len(doc['option_keys']) > 0: if 'option_keys' in doc and len(doc['option_keys']) > 0:
text.append("Options (= is mandatory):\n") text.append("Options (= is mandatory):\n")
@ -268,19 +263,16 @@ class DocCLI(CLI):
if 'default' in opt: if 'default' in opt:
default = str(opt['default']) default = str(opt['default'])
desc = desc + " [Default: " + default + "]" desc = desc + " [Default: " + default + "]"
text.append("%s\n" % textwrap.fill(CLI.tty_ify(desc), initial_indent=opt_indent, text.append("%s\n" % textwrap.fill(CLI.tty_ify(desc), limit, initial_indent=opt_indent, subsequent_indent=opt_indent))
subsequent_indent=opt_indent))
if 'notes' in doc and doc['notes'] and len(doc['notes']) > 0: if 'notes' in doc and doc['notes'] and len(doc['notes']) > 0:
notes = " ".join(doc['notes']) notes = " ".join(doc['notes'])
text.append("Notes:%s\n" % textwrap.fill(CLI.tty_ify(notes), initial_indent=" ", text.append("Notes:%s\n" % textwrap.fill(CLI.tty_ify(notes), limit-6, initial_indent=" ", subsequent_indent=opt_indent))
subsequent_indent=opt_indent))
if 'requirements' in doc and doc['requirements'] is not None and len(doc['requirements']) > 0: if 'requirements' in doc and doc['requirements'] is not None and len(doc['requirements']) > 0:
req = ", ".join(doc['requirements']) req = ", ".join(doc['requirements'])
text.append("Requirements:%s\n" % textwrap.fill(CLI.tty_ify(req), initial_indent=" ", text.append("Requirements:%s\n" % textwrap.fill(CLI.tty_ify(req), limit-16, initial_indent=" ", subsequent_indent=opt_indent))
subsequent_indent=opt_indent))
if 'examples' in doc and len(doc['examples']) > 0: if 'examples' in doc and len(doc['examples']) > 0:
text.append("Example%s:\n" % ('' if len(doc['examples']) < 2 else 's')) text.append("Example%s:\n" % ('' if len(doc['examples']) < 2 else 's'))

Loading…
Cancel
Save