content is now centered in the terminal
This commit is contained in:
parent
35123e920c
commit
636e8bb4b8
|
@ -1,6 +1,7 @@
|
||||||
# Offpunk History
|
# Offpunk History
|
||||||
|
|
||||||
## 1.0 - Unreleased
|
## 1.0 - Unreleased
|
||||||
|
- Content is now centered for more elegant reading
|
||||||
- Fixed a crash when accessing links in list (thanks Matthieu Talbot for the report)
|
- Fixed a crash when accessing links in list (thanks Matthieu Talbot for the report)
|
||||||
- Fixed a crash due to a typo in a variable name rarely accessed.
|
- Fixed a crash due to a typo in a variable name rarely accessed.
|
||||||
|
|
||||||
|
|
51
offpunk.py
51
offpunk.py
|
@ -61,9 +61,6 @@ except ModuleNotFoundError:
|
||||||
try:
|
try:
|
||||||
import ansiwrap
|
import ansiwrap
|
||||||
wrap_method = ansiwrap.wrap
|
wrap_method = ansiwrap.wrap
|
||||||
#from blessed import Terminal
|
|
||||||
#term = Terminal()
|
|
||||||
#wrap_method = term.wrap
|
|
||||||
_HAS_ANSIWRAP = True
|
_HAS_ANSIWRAP = True
|
||||||
except ModuleNotFoundError:
|
except ModuleNotFoundError:
|
||||||
print("Try installing python-ansiwrap for better rendering")
|
print("Try installing python-ansiwrap for better rendering")
|
||||||
|
@ -71,10 +68,32 @@ except ModuleNotFoundError:
|
||||||
wrap_method = textwrap.wrap
|
wrap_method = textwrap.wrap
|
||||||
_HAS_ANSIWRAP = False
|
_HAS_ANSIWRAP = False
|
||||||
|
|
||||||
|
global TERM_WIDTH
|
||||||
|
TERM_WIDTH = 80
|
||||||
|
|
||||||
|
def term_width():
|
||||||
|
width = TERM_WIDTH
|
||||||
|
cur = shutil.get_terminal_size()[0]
|
||||||
|
if cur < width:
|
||||||
|
width = cur
|
||||||
|
return width
|
||||||
|
|
||||||
# return wrapped text as a list of lines
|
# return wrapped text as a list of lines
|
||||||
def wraplines(*args,**kwargs):
|
def wraplines(*args,**kwargs):
|
||||||
# print("will wrap with %s and %s"%(str(args),str(kwargs)))
|
# print("will wrap with %s and %s"%(str(args),str(kwargs)))
|
||||||
return wrap_method(*args,**kwargs)
|
lines = wrap_method(*args,**kwargs)
|
||||||
|
lines2 = []
|
||||||
|
textwidth = TERM_WIDTH
|
||||||
|
termspace = shutil.get_terminal_size()[0]
|
||||||
|
#Following code instert blanck spaces to center the content
|
||||||
|
if termspace > textwidth:
|
||||||
|
margin = int((termspace - textwidth)/2)
|
||||||
|
else:
|
||||||
|
margin = 0
|
||||||
|
for l in lines :
|
||||||
|
lines2.append(margin*" "+l)
|
||||||
|
#line = 30*" " + line
|
||||||
|
return lines2
|
||||||
# return wrapped text as one string
|
# return wrapped text as one string
|
||||||
def wrapparagraph(*args,**kwargs):
|
def wrapparagraph(*args,**kwargs):
|
||||||
return "\n".join(wraplines(*args,**kwargs))
|
return "\n".join(wraplines(*args,**kwargs))
|
||||||
|
@ -255,16 +274,6 @@ urllib.parse.uses_netloc.append("gemini")
|
||||||
urllib.parse.uses_relative.append("spartan")
|
urllib.parse.uses_relative.append("spartan")
|
||||||
urllib.parse.uses_netloc.append("spartan")
|
urllib.parse.uses_netloc.append("spartan")
|
||||||
|
|
||||||
global TERM_WIDTH
|
|
||||||
TERM_WIDTH = 80
|
|
||||||
|
|
||||||
def term_width():
|
|
||||||
width = TERM_WIDTH
|
|
||||||
cur = shutil.get_terminal_size()[0] - 1
|
|
||||||
if cur < width:
|
|
||||||
width = cur
|
|
||||||
return width
|
|
||||||
|
|
||||||
def fix_ipv6_url(url):
|
def fix_ipv6_url(url):
|
||||||
if not url:
|
if not url:
|
||||||
return
|
return
|
||||||
|
@ -388,7 +397,11 @@ class GemtextRenderer(AbstractRenderer):
|
||||||
final = ""
|
final = ""
|
||||||
for l in wrapped:
|
for l in wrapped:
|
||||||
if color:
|
if color:
|
||||||
l = color + l + "\x1b[0m"
|
spaces = ""
|
||||||
|
while l.startswith(" "):
|
||||||
|
l = l[1:]
|
||||||
|
spaces += " "
|
||||||
|
l = spaces + color + l + "\x1b[0m"
|
||||||
if l.strip() != "":
|
if l.strip() != "":
|
||||||
final += l + "\n"
|
final += l + "\n"
|
||||||
return final
|
return final
|
||||||
|
@ -409,7 +422,13 @@ class GemtextRenderer(AbstractRenderer):
|
||||||
if line.startswith("```"):
|
if line.startswith("```"):
|
||||||
preformatted = not preformatted
|
preformatted = not preformatted
|
||||||
elif preformatted:
|
elif preformatted:
|
||||||
rendered_text += line + "\n"
|
# infinite line to not wrap preformated
|
||||||
|
l = wraplines(line,100000000)
|
||||||
|
if len(l) > 0:
|
||||||
|
l = l[0]
|
||||||
|
else:
|
||||||
|
l = ""
|
||||||
|
rendered_text += l + "\n"
|
||||||
elif line.startswith("=>"):
|
elif line.startswith("=>"):
|
||||||
strippedline = line[2:].strip()
|
strippedline = line[2:].strip()
|
||||||
if strippedline:
|
if strippedline:
|
||||||
|
|
Loading…
Reference in New Issue