diff --git a/main.py b/main.py index f05922a..d690d30 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ import sys import time import wikipedia - +from utils import wrap_text class wikiclss(): def __init__(self, srchqery): @@ -277,7 +277,7 @@ def getdcont(self): purltext = wikipedia.page(self.srchqery).content stoptime = time.monotonic() duration = stoptime - strttime - purltext = self.prsehead(purltext) + purltext = wrap_text(self.prsehead(purltext)) click.echo(click.style("RESULT > ", fg="green", bold=True) + click.style("CONTENT > ", fg="blue", bold=True) + "\n" + purltext) click.echo(click.style("RAISED > ", fg="green", bold=True) + "1 result in " + str(duration)[0:3] + " seconds [" + self.obtntime() + "]") except wikipedia.exceptions.HTTPTimeoutError: @@ -389,4 +389,4 @@ def mainfunc(srchqery, wkaction): if __name__ == "__main__": - mainfunc() \ No newline at end of file + mainfunc() diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..4894946 --- /dev/null +++ b/utils.py @@ -0,0 +1,33 @@ +import os + +def get_terminal_width(): + """Get current terminal width""" + return os.get_terminal_size()[0] + +def wrap_text(message): + """Wrap the whole content of a page""" + wrapped_message = str() + max_width = get_terminal_width() + + for m in message.split('\n'): + if len(m) <= max_width: + wrapped_message += m + '\n' + else: + wrapped_message += wrap_paragraph(m, max_width) + + return wrapped_message + +def wrap_paragraph(paragraph, max_width, indent=9): + """Wrap a long paragraph""" + wrapped_paragraph = str() + indent_text = ' ' * indent + paragraph_width = len(paragraph) + width = max_width - indent + + #wrapped_paragraph += paragraph[0 : max_width] + for i in range(0, paragraph_width, width): + wrapped_paragraph += indent_text + wrapped_paragraph += paragraph[i : i + width] + wrapped_paragraph += '\n' + + return wrapped_paragraph