Created
March 10, 2021 08:41
-
-
Save Pinacolada64/bd8de4abf78c8768c572305e903e2a73 to your computer and use it in GitHub Desktop.
Word wrap and More prompt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import textwrap | |
# https://docs.python.org/3/library/textwrap.html#textwrap.TextWrapper.wrap | |
# https://inventwithpython.com/blog/2014/12/02/why-is-object-oriented-programming-useful-with-a-role-playing-game-example/ | |
# https://python-prompt-toolkit.readthedocs.io/en/master/ | |
class Terminal: | |
# TODO: make baud_rate and output_count be default arguments, not specified in the call for the function to work: | |
# TODO: outputCount should be a member of Terminal, I think: | |
def __init__(self, translation_type, screen_height, screen_width, more_prompt, output_count): | |
# baud_rate = 2400, | |
# output_count = Terminal.outputCount | |
self.morePrompt = more_prompt | |
self.translationType = translation_type | |
self.screenHeight = screen_height | |
self.screenWidth = screen_width | |
# i don't want to always specify these last two parameters, can they be local variables? | |
# self.baudRate = baud_rate | |
self.outputCount = output_count # how many lines output since last more_prompt encountered | |
print(f"self.translationType = {self.translationType}") | |
print(f'self.morePrompt: {self.morePrompt}') | |
def count_lines(self, wrapped_text): | |
"""Count number of lines in wrapped text""" | |
if not wrapped_text: | |
return 0 | |
else: | |
_num = 0 | |
for _num in wrapped_text: | |
_num = +1 | |
return _num | |
def output(self, text): | |
""" | |
This will do the work of actually outputting text, honoring morePrompt setting hopefully | |
:param text: | |
:return: none | |
""" | |
wrapped_text = textwrap.wrap(text, width=self.screenWidth) | |
# display list of lines | |
print(wrapped_text) | |
# wrapped_text is a list of lines: | |
for line in wrapped_text: | |
self.outputCount += 1 | |
if self.morePrompt is True and self.outputCount == self.screenHeight: | |
# do more_prompt: | |
self.outputCount = 0 | |
input("More: ") # toss response | |
else: | |
print(line) | |
print(f"lines: {self.outputCount}") | |
if __name__ == '__main__': | |
text_to_wrap = r""" | |
Ut libero in quis aut consectetur non. Quo ratione dolores et mollitia occaecati ipsam autem. Beatae enim asperiores | |
officia ipsa sit fugiat vero corrupti. Dolorem corrupti architecto dolore voluptates optio tempora explicabo. Et ad | |
ullam eligendi. Consequatur et accusamus est officia. | |
\n\n | |
Vel repellendus soluta pariatur consequatur. Quasi nemo ut reiciendis provident quis quidem culpa. Exercitationem in | |
nulla ut adipisci voluptates. Doloribus aut sapiente et voluptate assumenda aliquam autem. Praesentium id adipisci | |
quas officiis et. | |
\n\n | |
Eum omnis laborum suscipit minima dolore. Accusamus itaque dolorem est dolores aut consequatur eius. Iusto qui | |
libero dolore occaecati eveniet laborum dolores. Molestiae porro perspiciatis eaque qui occaecati rerum. | |
\n\n | |
Placeat qui atque magni. Quod enim voluptatibus sint. Ut modi inventore voluptatem. Veritatis non voluptatem est | |
esse. Provident architecto similique voluptas amet dolorem sunt ab. Omnis amet eos eum soluta aliquam inventore | |
voluptatem repudiandae.\n\n | |
Maiores nobis sint illo. Qui accusamus aut omnis mollitia ab tenetur. Voluptas aut deleniti quia. Accusantium | |
voluptatum neque animi quaerat autem ratione. Quo omnis quas dolorum. Eligendi numquam vel velit voluptatem | |
doloremque dignissimos deleniti eos.\n | |
""" | |
pcTerm = Terminal(translation_type='ANSI', screen_height=25, screen_width=80, more_prompt=True, output_count=1) | |
c64Term = Terminal(translation_type="PETSCII", screen_height=25, screen_width=40, more_prompt=True, output_count=1) | |
print("Emulating a 40x25 Commodore 64") | |
c64Term.output(text_to_wrap) | |
x = input("Key: ") | |
print("Emulating a 80x25 ANSI terminal") | |
pcTerm.output(text_to_wrap) | |
print("end") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment