/CLIPrintTool

Improve the readability and appearance of your print statements.

Primary LanguagePython

Python - CLI Print Tool

Improve the readability and appearance of your print statements.


How to get started

If you would rather take a look at the code instead of the documentation, open directly the example.py file.

Step 1 - Import the class

Import the CLIPrintTool class from cli_print_tool.py

from cli_print_tool import CLIPrintTool

(Optional) Import the TextAlignment class from cli_print_tool.py to change the text alignment (by default left-aligned).

from cli_print_tool import CLIPrintTool, TextAlignment

Step 2 - Create a class object

Create a CLIPrintTool object.

CLIPrintTool()

(Optional) Specify the maximum line length for your content (by default 100 characters).

CLIPrintTool(120)  # custom max length of 120 characters

Step 3 - Use the public object methods

Use the public methods from CLIPrintTool to print your formatted text content.

CLIPrintTool().textbox("Hello World!")

List of public methods

  1. Text
  2. Textbox
  3. Heading
  4. List

Text

Takes two parameters and prints long text wrapped to respect the maximum line length.

Parameters

Parameter Type Required Default Value
text String Yes None
alignment TextAlignment No TextAlignment.left

Examples

Example 1

Prints long text wrapped to respect the maximum line length and left-aligned.

long_text = "This is a very very very very very long sentence. And here is another one, but don't worry about that \
CLIPrintTool will automatically wrap the text to respect the maximum line length (by default 100 characters)."

CLIPrintTool().text(long_text)

Screenshot of the console output from the example above.

Example 2

Prints long text wrapped to respect the maximum line length and centered.

Note: The TextAlignment class needs to be imported from cli_print_tool.py to change the text alignment.

long_text = "This is a very very very very very long sentence. And here is another one, but don't worry about that \
CLIPrintTool will automatically wrap the text to respect the maximum line length (by default 100 characters)."

CLIPrintTool().text(long_text, TextAlignment.center)

Screenshot of the console output from the example above.

Example 3

Prints long text wrapped to respect the maximum line length and right-aligned.

Note: The TextAlignment class needs to be imported from cli_print_tool.py to change the text alignment.

long_text = "This is a very very very very very long sentence. And here is another one, but don't worry about that \
CLIPrintTool will automatically wrap the text to respect the maximum line length (by default 100 characters)."

CLIPrintTool().text(long_text, TextAlignment.right)

Screenshot of the console output from the example above.


Textbox

Takes two parameters and prints text inside a box.

Parameters

Parameter Type Required Default Value
text String Yes None
alignment TextAlignment No TextAlignment.left

Examples

Example 1

Prints text inside a box and left-aligned.

CLIPrintTool().textbox("The quick, brown fox jumps over a lazy dog.")

Screenshot of the console output from the example above.

Example 2

Prints text inside a box and centered.

Note: The TextAlignment class needs to be imported from cli_print_tool.py to change the text alignment.

CLIPrintTool().textbox("The quick, brown fox jumps over a lazy dog.", TextAlignment.center)

Screenshot of the console output from the example above.

Example 3

Prints text inside a box and right-aligned.

Note: The TextAlignment class needs to be imported from cli_print_tool.py to change the text alignment.

CLIPrintTool().textbox("The quick, brown fox jumps over a lazy dog.", TextAlignment.right)

Screenshot of the console output from the example above.


Heading

Takes four parameters and prints two texts vertically separated by a line (divider).

Alignment of title and subtitle could be optionally altered using the TextAlignment enum.

Parameters

Parameter Type Required Default Value
title String Yes None
subtitle String Yes None
title_alignment TextAlignment No TextAlignment.left
subtitle_alignment TextAlignment No TextAlignment.left

Examples

Example 1

Prints two texts vertically separated by a line (divider) and left-aligned.

CLIPrintTool().heading("This is a Title", "This is a Subtitle")

Screenshot of the console output from the example above.

Example 2

Prints two texts vertically separated by a line (divider) and centered.

Note: The TextAlignment class needs to be imported from cli_print_tool.py to change the text alignment.

CLIPrintTool().heading("This is a Title", "This is a Subtitle", TextAlignment.center, TextAlignment.center)

Screenshot of the console output from the example above.

Example 3

Prints two texts vertically separated by a line (divider) and right-aligned.

Note: The TextAlignment class needs to be imported from cli_print_tool.py to change the text alignment.

CLIPrintTool().heading("This is a Title", "This is a Subtitle", TextAlignment.right, TextAlignment.right)

Screenshot of the console output from the example above.

Example 4

Prints two texts separated by a line (divider), with title vertically centred, and subtitle left-aligned.

Note: The TextAlignment class needs to be imported from cli_print_tool.py to change the text alignment.

CLIPrintTool().heading("This is a Title", "This is a Subtitle", TextAlignment.center, TextAlignment.left)

Screenshot of the console output from the example above.


List

Takes four parameters and prints a list of elements vertically with a title (optional).

Alignment of elements and title could be optionally altered using the TextAlignment enum.

Note: Blank lines can be added between elements by using the CLIPrintTool.NEW_LINE constant. To see how it's used, refer to example 4.1.

Parameters

Parameter Type Required Default Value
elements List of String Yes None
title String No ""
title_alignment TextAlignment No TextAlignment.left
elements_alignment TextAlignment No TextAlignment.left

Examples

Example 1

Prints a list of elements vertically without title and left-aligned.

CLIPrintTool().list(["System Information", "Crash Reports", "Exit"])

Screenshot of the console output from the example above.

Example 2

Prints a list of elements vertically without title and centered.

Note: The TextAlignment class needs to be imported from cli_print_tool.py to change the text alignment.

CLIPrintTool().list(["System Information", "Crash Reports", "Exit"], elements_alignment=TextAlignment.center)

Screenshot of the console output from the example above.

Example 3

Prints a list of elements vertically without title and right-aligned.

Note: The TextAlignment class needs to be imported from cli_print_tool.py to change the text alignment.

CLIPrintTool().list(["System Information", "Crash Reports", "Exit"], elements_alignment=TextAlignment.right)

Screenshot of the console output from the example above.

Example 4

Prints a list of elements vertically with title and left-aligned.

CLIPrintTool().list(["System Information", "Crash Reports", "Exit"], "MAIN MENU")

Screenshot of the console output from the example above.

Example 4.1

Prints a list of elements vertically with title and left-aligned.

Note: The CLIPrintTool.NEW_LINE constant was added inside our list of menu options to display a blank line.

menu_options = ["System Information", "Crash Reports", CLIPrintTool.NEW_LINE, "Exit"]
CLIPrintTool().list(menu_options, "MAIN MENU")

Screenshot of the console output from the example above.

Example 5

Prints a list of elements vertically with title and centered.

Note: The TextAlignment class needs to be imported from cli_print_tool.py to change the text alignment.

CLIPrintTool().list(["System Information", "Crash Reports", "Exit"], "MAIN MENU", TextAlignment.center, TextAlignment.center)

Screenshot of the console output from the example above.

Example 6

Prints a list of elements vertically with title and right-aligned.

Note: The TextAlignment class needs to be imported from cli_print_tool.py to change the text alignment.

CLIPrintTool().list(["System Information", "Crash Reports", "Exit"], "MAIN MENU", TextAlignment.right, TextAlignment.right)

Screenshot of the console output from the example above.

Example 7

Prints a list of elements left-aligned, with title center-aligned.

Note: The TextAlignment class needs to be imported from cli_print_tool.py to change the text alignment.

CLIPrintTool().list(["System Information", "Crash Reports", "Exit"], "MAIN MENU", TextAlignment.center)

Screenshot of the console output from the example above.