Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Indent long paragraphs #22

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import time
import wikipedia

from utils import wrap_text

class wikiclss():
def __init__(self, srchqery):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -389,4 +389,4 @@ def mainfunc(srchqery, wkaction):


if __name__ == "__main__":
mainfunc()
mainfunc()
33 changes: 33 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -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