A (basic) cross-platform python console manager
Project description
PyConsole
A (basic) cross-platform python console manager
Table of Contents
About The Project
This project aims to implement a cross-platform console manager and other console related 'graphical' functions, like box containers.
It draws inpiration from the curses module and rich.
Installation
To install simply use pip:
- To install run
pip install python-console
- To uninstall run
pip uninstall python-console
Usage
To begin with, we can import the library with:
import pyconsole
Or you can import specific things like the Console class:
from pyconsole import Console
For more complete examples check the features section or run the test.py file in this repository.
Features
The main features of this library are:
- The
Consoleclass- The
Keyboardclass - The
Cursorclass
- The
- Colors with the
Colorclass - Boxes with the
BoxandTitleBoxclass - Pretty printing exceptions, warnings and interactive sessions
If you want to know more information that is not displayed here check out the docstrings in the code or using the intellisense of your IDE.
Console
The focus of this library is the Console, it can be used like so:
from pyconsole import Console
console = Console()
Or it can be customized with keyword arguments:
from pyconsole import Console, RGB
console = Console(indent=4, pass_prompt="#", color_mode=RGB)
The console methods are:
- error
- warn
- get_key
- get_pass
- print_traceback
- clear
- command
Other useful attributes are:
- system
- color
- cursor
- keyboard
- size
Some examples:
Print colored traceback with print_traceback
from pyconsole import Console, RGB
console = Console(color_mode=RGB)
try:
invalid = 1 / 0
except ZeroDivisionError:
console.print_traceback()
Print the registered key name from a key press
from pyconsole import Console
console = Console()
key = console.get_key()
console.print("The int", key, f"represents the key '{console.keyboard.Key(key)}'")
Print the terminal size and the OS name
from pyconsole import Console
console = Console()
console.print("The console dimensions are:", console.size)
console.print(f"The current system is: '{console.system}'")
Cursor
The cursor key can be used to modify some cursor properties.
It can be used like so:
import time
from pyconsole import Console
console = Console()
console.cursor.hide()
time.sleep(5)
console.cursor.restore()
Or it can wrap around a function and restore its values in case of an error, like using Ctrl+C to stop the execution:
import time
from pyconsole import Console
console = Console()
console.cursor.wrap(time.sleep, 5)
It can also be moved to any valid position on the screen using move
from pyconsole import Console
console = Console()
console.cursor.move(4, 1) # 4th row, 1st column (starting from the 1st row and the 1st column)
Keyboard
The keyboard is the class that's in charge of registering keys and associating them according to platform.
Registering a new key
from pyconsole import Console
console = Console()
console.keyboard.register_key("TILDE", "~")
The list of registered keys can be found when using intellisense.
Key
The key class is the one that does the association between key and integer, but it still needs the keyboard to know the actual symbol.
Registering a new key but omiting the keyboard instance
from pyconsole import Keyboard
Keyboard.Key.register("TILDE")
Or alternatively
from pyconsole import Keyboard
Keyboard.Key.TILDE = Keyboard.Key.next_num
It should be noted that it's not really recommended to do the registration this way and should use it through the keyboard instance instead.
The key class can also be instantiated from an integer to return the corresponding string representation.
Getting the key representation from a number
from pyconsole import Keyboard
print(Keyboard.Key(1)) # prints 'LEFT' for left arrow
Color
The color class is a class that abstracts the ansi escape codes used for colors and other effects, like underline. If your Windows console (like the ancient cmd.exe) does not support ansi escape sequences then consider using colorama for a fully cross platform option.
Do note that the new Windows Terminal does support colors.
You can import some basic colors and use them like:
from console import Console, RED
console = Console()
console.print(f"{RED}This is red text")
Or you can do a manual reset
from pyconsole import Console, RED, BLUE, RESET
console = Console()
console.print(f"{RED}This is red{RESET} and {BLUE}this is blue{RESET}", reset=False)
You can also create simple colors with ColorCodes and ColorMods
from pyconsole import Console, Color, ColorCode, ColorMod
console = Console()
color = Color(ColorCode.CYAN, [ColorMod.UNDERLINE, ColorMod.OVERLINE])
console.print(f"{color}Hello")
You can even add two colors properties together
from pyconsole import Color, ColorCode, ColorMod
color = Color(ColorCode.YELLOW, [ColorMod.BOLD])
color += Color(mods=[ColorMod.STRIKE])
Or if you want more complex colors, you can create them using rgb values
from pyconsole import Color
color = Color.from_rgb(246, 38, 129, bg=True) # Use bg=True for background color
Additionally, you can add the __color__ method to a custom class and define how you want it to look with color
from pyconsole import Console, LIME, BLUE, RESET
class Example:
def __init__(self, value):
self.value = value
def __color__(self):
return f"{LIME}Example{RESET}(value={BLUE}{self.value}{RESET})"
console = Console()
example = Example(5)
console.print(example) # Will print the colored string defined by '__color__'
Color Parser
The color parser is the classin charge of parsing different object types to different colors.
You can register a type and a color (or even change an already existing one)
from pyconsole import Console, Color, RGB
console = Console(color_mode=RGB)
console.color.set_color(bytearray, Color.from_rgb(48, 25, 52))
And you can parse an object into a colored string
from pyconsole import Console
console = Console()
console.print(f"This '{console.color.parse(12)}' is an f-string colored number")
By default it ignores string unless it's inside a container like a dictionary, but it can be changed by passing ignore_str=False
from pyconsole import Console
console = Console()
console.print(f"A {console.color.parse('colored', ignore_str=False)} string")
Do note that it will returned the colored representation of the string, meaning, it will add quotes.
Box
The Box and TitleBox classes surround your text wwith a border making a box or a titled box respectively.
You can create a box with multiple lines and border color
from pyconsole import Box, LIME
box = Box("Multiple lines\nin one box\nwith a border color :)", color=LIME)
The TitleBox class works the same way, but with an added title
from pyconsole import TitleBox, GREEN, LIME, RESET
box = TitleBox("A Cool Title", f"Just a box\nwith a title\nand {LIME}COLOR{RESET}", color=GREEN)
Other
Some other useful functions include:
install_errorsfor pretty error formatting all the time (no need to callconsole.print_tracebackinside a try-except block)install_warningsfor pretty warnings formattingpretty.installfor pretty interactive session printinginstallfor installing all of the aboveget_consoleto get the global console instance that the functions above useset_consoleto change the global console properties
License
Distributed under the MIT License. See LICENSE for more information.
Acknowledgements
None of the following are associated with the project in any way. They are mentioned as a source of learning and inspiration for parts of this project.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python-console-1.3.2.tar.gz.
File metadata
- Download URL: python-console-1.3.2.tar.gz
- Upload date:
- Size: 26.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.7.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f6ce74b95ca3da6d88c2e210ac4ce1a3cdc81ea3efcea7c93d26304fcded9d5
|
|
| MD5 |
2411aa379cf630d47565fcd5a3cab562
|
|
| BLAKE2b-256 |
9ec688c7386ed3a23c424cf47a26c7d0c1b1d1df7fed1dda9822ae0e01869add
|
File details
Details for the file python_console-1.3.2-py3-none-any.whl.
File metadata
- Download URL: python_console-1.3.2-py3-none-any.whl
- Upload date:
- Size: 26.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.7.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee3e1b09513c61218a2ab16f86f97cdeba92063c6b0f32e16062504d7c4fd406
|
|
| MD5 |
b6e274eec7ff9963a34914bcaee93452
|
|
| BLAKE2b-256 |
6423c9905ece2e046892ff4f09e149b668e12d5a38d5bcf364c55affaf55af3b
|