Skip to main content

Console/Terminal functionality

Project description

ConsoleMasterPy

ConsoleMasterPy is a Python library that simplifies console manipulation on Windows systems. It provides functionalities for changing console colors, hiding/showing the cursor, setting console window size, and more.

Installation

You can install ConsoleMasterPy using pip:

pip install ConsoleMasterPy

Usage

from ConsoleMasterPy import ConsoleMaster

Create a new console window

console = ConsoleMaster(title="My Console", font_size=17, window_width=30, window_height=25)

Change console colors

console.change_console_color(background_color=(0, 0, 0), foreground_color=(255, 255, 255))

Print with custom colors

console.print_with_color(rgb_background=(0, 0, 0), rgb_foreground=(255, 255, 255), element_to_print="Hello, World!")

Change console window size

console.change_windows_size(width=40, height=30)

Hide the cursor

console.hide_cursor()

Pause execution until user input

console.pause()

Go to specified position (x, y) in the console window

console.go_xy(x=10, y=5)

Change pixel size of the console font

console.change_pixel_size(size=20)

Clean a specified zone in the console window

console.clean_zone(x_coord_init=5, y_coord_init=5, x_coord_end=15, y_coord_end=10)

Clean the entire console window

console.clean_windows()

Pause execution until user input

console.pause()

Additional Functions

generate_name()

This function generates a random name with a mix of vowels and consonants.

from ConsoleMasterPy import generate_name

name = generate_name()
print(name)

exception_handler Function

The exception_handler function is a decorator used to handle exceptions within functions. It catches any exceptions that occur during the execution of the decorated function and logs the error message, along with a timestamp, to a file named log.txt. This decorator ensures that the program can continue running smoothly even if an exception occurs, providing better error handling and debugging.

Parameters

  • func: The function to be decorated with exception handling.

Example Usage

@exception_handler
def my_function():
    # Code that might raise an exception
    pass

Example usage - Game of Life of John Horton Conway

from ConsoleMasterPy import ConsoleMaster, exception_handler
import random
from time import sleep

def copy_matrix(matrix):
    return [row[:] for row in matrix]

width, height = 35, 35
console_manager = ConsoleMaster("GAME OF LIFE", 13, width, height)

@exception_handler
def main():
    generation_counter = 0
    
    while True:
        if generation_counter == 0:
            current_matrix = [[0 for x in range(width)] for y in range(height)]
            next_matrix = copy_matrix(current_matrix)
            cell_color = [random.randint(0, 255) for _ in range(3)]

            live_cell_rules = [random.randint(0, 1) for _ in range(9)]
            birth_rules = [random.randint(0, 1) for _ in range(9)]

            for x in range(width):
                next_matrix[x][0] = 0
                next_matrix[x][height - 1] = 0
                next_matrix[x][1] = 1
                next_matrix[x][height - 2] = 1

            for y in range(height):
                next_matrix[0][y] = 0
                next_matrix[width - 1][y] = 0
                next_matrix[1][y] = 1
                next_matrix[width - 2][y] = 1

            for x in range(1, width - 1):
                for y in range(1, height - 1):
                    cell_value = next_matrix[x][y]
                    console_manager.go_xy(x, y)
                    color = cell_color if cell_value else [0, 0, 0]
                    console_manager.print_with_color(color, [0, 0, 0], " ")
            generation_counter = 300

        generation_counter -= 1

        current_matrix = copy_matrix(next_matrix)
        for x in range(1, width - 1):
            for y in range(1, height - 1):
                neighbors = [(x + i, y + j) for i in range(-1, 2) for j in range(-1, 2) if not (i == 0 and j == 0)]
                count = sum(1 for i, j in neighbors if 0 <= i < width and 0 <= j < height and current_matrix[i][j] == 1)

                if current_matrix[x][y] == 1:
                    if not live_cell_rules[count]:
                        next_matrix[x][y] = 0
                        console_manager.go_xy(x, y)
                        console_manager.print_with_color([0, 0, 0], [0, 0, 0], " ")
                    else:
                        next_matrix[x][y] = 1
                else:
                    if birth_rules[count]:
                        next_matrix[x][y] = 1
                        console_manager.go_xy(x, y)
                        console_manager.print_with_color(cell_color, [0, 0, 0], " ")
                    else:
                        next_matrix[x][y] = 0
        sleep(0.05)

main()

License

ConsoleMasterPy is licensed under the MIT.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ConsoleMasterPy-1.0.7.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ConsoleMasterPy-1.0.7.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file ConsoleMasterPy-1.0.7.1.tar.gz.

File metadata

  • Download URL: ConsoleMasterPy-1.0.7.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for ConsoleMasterPy-1.0.7.1.tar.gz
Algorithm Hash digest
SHA256 47b920bd7f3e3e1ff154b4ec66379e0f7f1426ac704467f5f9b8d64ce711819d
MD5 d1794e33d27ef7957a40b7d894bfbe87
BLAKE2b-256 382a231656b0f470fe8bab219f7c5cf1d29f95f4d8084a3f286b6f8c33421cc4

See more details on using hashes here.

File details

Details for the file ConsoleMasterPy-1.0.7.1-py3-none-any.whl.

File metadata

File hashes

Hashes for ConsoleMasterPy-1.0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 118062b1cd00350b969dbb8fc94440d23ad5e091a02ce7eb5cb72c567ac2db20
MD5 f9a9f2a81a0566c1342e80966982cc41
BLAKE2b-256 d42c86f701fbfcb2edf47e8e38397e1440b7015b842212755324fb7ebb3de4fb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page