def format_definition(json_obj): """ Format an API-provided JSON object for display""" word = json_obj['word'] definition = dedent(json_obj['definition']).replace('\r\n', ' ') example = dedent(json_obj['example']).replace('\r\n', ' ') permalink = json_obj['permalink'] parts = textwrap(definition, DEFINITION_LENGTH) definition = parts[0] if len(parts) > 1: definition += ' [...]' if example: parts = textwrap(example, TOTAL_LENGTH - len(definition)) example = parts[0] if len(parts) > 1: example += ' [...]' s = bold(word) + ': ' if definition and not example: s += definition elif example and not definition: s += 'E.g. ' + example else: # U+2014 EM DASH s += definition + u' \u2014 e.g. ' + example return '%s (%s)' % (s, permalink)
def generate(text, wrap=True, width=maxWidth, tailChar="\\"): ######################### #parsing text (wrapping)# ######################### #wrapping text if wrap: textWrapped = [] for i in text: textWrapped.extend(textwrap(i, width)) text = textWrapped #calculating width textWidth = max(len(i) for i in text) textHeight = len(text) ########## #printing# ########## output = [] #generating top line output.append(f" {'_' * (textWidth + 2)} ") #one line of text if textHeight == 1: output.append(f"< {text[0]} > ") #multiple lines of text else: for i, line in enumerate(text): if i == 0: output.append(f"/ {line.ljust(textWidth)} \\ ") elif textHeight - i == 1: output.append(f"\\ {line.ljust(textWidth)} / ") else: output.append(f"│ {line.ljust(textWidth)} │ ") spacing = " " * textWidth #creating bottom line and the tail output.append(f" {'¯' * (textWidth + 2)} {tailChar} ") output.append(f"{spacing} {tailChar}") output = [ f"{bubbleLine}{asciiartLine}" for bubbleLine, asciiartLine in zip( output, ["" for _ in range(textHeight + 3 - 4)] + asciiart[:4]) ] #appending the rest of asciiart for line in asciiart[4:]: output.append(f"{spacing}{line}") return output
def _pprint(l): """ Formats a list l to be displayed in a tabular layout. It is possible to pass an integer width to the textwrap function. The width of the terminal window could be obtained via the Python console module. However, since it is not included in Jas, we decided not to use it. The default width that textwrap uses is set to 70. There might be a better way to do this. """ col = max([len(x) for x in l]) + 3 padded = ''.join([x.ljust(col) for x in l]) print '\n'.join(textwrap(padded))
def wrap(string): """Wraps strings of legn""" return '\n'.join(textwrap(string, 60))
def textwrap(text, width): return '\n'.join(textwrap(text, width=width))