Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # python class file that allows to read in a file split paragraphs then print each line of
- # paragraph and its characters within that line in chosen colour to a terminal
- # after each paragraph you are then prompted for input donated by $: following which the next
- # paragraph will be printed as per above sequence
- # Good practice for presentations to explain points whilst presenting media of some type using a terminal
- # This also aids as a prompt for to remind yourself of key issue you need to discuss
- # MD Harrington 08/05/2023 facebook link https://www.facebook.com/mark.harrington.14289/
- # Instagram Link https://www.instagram.com/markukh2021/
- import time
- class TextReader:
- def __init__(self, filename):
- self.filename = filename
- def read_file(self):
- color_code = self.get_color_code()
- with open(self.filename, 'r') as f:
- text = f.read()
- return text, color_code
- def get_color_code(self):
- color = input("Select a color for the text (red/green/blue/purple/white/yellow/cyan): ")
- color_codes = {'black': '\033[30m', 'red': '\033[31m', 'green': '\033[32m',
- 'yellow': '\033[33m', 'blue': '\033[34m', 'purple': '\033[35m',
- 'cyan': '\033[36m', 'white': '\033[37m'}
- if color in color_codes:
- return color_codes[color]
- else:
- print("Invalid color selected, defaulting to black.")
- return color_codes["white"]
- def print_text(self, text, color_code):
- paragraphs = text.split('\n\n')
- for paragraph in paragraphs:
- print(color_code, end="")
- lines = paragraph.split('\n')
- for line in lines:
- for char in line:
- print(char, end='', flush=True)
- time.sleep(0.03)
- print()
- print("\033[0m") # reset color code
- input("$:")
- def main():
- filename = "instructions"
- tr = TextReader(filename)
- text, color_code = tr.read_file()
- tr.print_text(text, color_code)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement