Open In App

Python terminal processing with TerminalDesigner module

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we’ll look at how to use TerminalDesigner to produce colorful text for the terminal in Python. This module is quite compact. This module can be used to change the font’s color, style, and background, print images onto terminals for video playback, convert images to ASCII art and serve a variety of other functions. You can install the TerminalDesigner module in Python 3 by using the following command:

pip install TerminalDesigner

This module contains 5 Python files:

  • BackGroundColor Module
  • ForeGroundColor Module
  • HexName Module
  • RandomColor Module
  • General Module

The BackGroundColor Module

The set_default_stylebg() method in the Background Module:

It only requires one parameter of the type int, and its return type is None. It is employed to alter the default values or set the style’s value to all colors. Using this function we can set a single style to all of the functions.

Styles: Normal = 0, Bold = 1, Light = 2, Italicized = 3, UnderLined  = 4, Blink = 5

Example 1:  

Python program to print colored text.

Python3




from Designer.BackGroundColor import *
 
print(red1bg('some red text with Italicized', 3))
print(green1bg('and with a green background with bold', 1))
print(blue1bg('blue text'), whitebg('hello'))
print(greenbg("calling the functions will allow") +
      yellow1bg(" you to adjust the colours."))
print('back to normal now')


Output:

 

Example 2

To make all of the text bold, use this set_default_stylebg() method.

Python3




from Designer.BackGroundColor import *
 
set_default_stylebg(1)
print(greenbg("This is"),bluebg("the")+brown3bg("example"))
print(wheat2bg("to"),pinkbg("set the default"),greenbg("style"))


Output:

To make all of the text bold

The FrontGroundColor Module

It has 610 methods with color names and an additional function called set_default_style(). It is simple to utilize; all we have to do is call the names of the colors and import the ForeGroundColor function from Designer. The colorful ForeGround text will be returned.

BackGround Module set_default_style() function

It only requires one parameter of the type int, and its return type is None. It is employed to alter the default values or set the style’s value to all colors. Using this function we can set a single style to all of the functions.

Example 1:

To print the colorful foreground text

Python3




from Designer.ForeGroundColor import *
 
print(red("geeks")+brown("for")+blue("geeks"))
print(brown("it's"),pink("pink"),green("interesting"))
print(red("hello"+pink("world")))


Output:

 

Example 2: 

To make all of the text bold, use this set_default_style() method.

Python3




from Designer.ForeGroundColor import *
 
set_default_style(3)
print(green("This is"),blue("the")+brown3(" example"))
print(wheat2("to"),pink("set the default"),green("style"))
 
set_default_style(1)
print(green("This is"),blue("the")+brown3(" example"))
print(wheat2("to"),pink("set the default"),green("style"))
 
set_default_style(2)
print(green("This is"),blue("the")+brown3(" example"))
print(wheat2("to"),pink("set the default"),green("style"))
 
set_default_style(4)
print(green("This is"),blue("the")+brown3("example"))
print(wheat2("to"),pink("set the default"),green("style"))
 
set_default_style(5)
print(green("This is"),blue("the")+brown3("example"))
print(wheat2("to"),pink("set the default"),green("style"))


Output:

diverse kinds of styles

HaxName Module

It is similar to these modules as well, however, we may alter the colors using hexadecimal names.

Example 1: 

Change the color using the hexadecimal names module.

Python3




from Designer.HaxName import *
 
print(h000000("This is"),h002266("the")+h00CD66(" example"))
print(h030303("for "),h030303("hexadecimal"),h00EE00("style"))


Output:

Example for hexadecimal Module

Example 2:

Print colors using for loop.

Python3




from Designer.HaxName import *
 
print(h09F911("another"),h002266("example"))
print(h030303("for "),h23238E("hexadecimal"),h292929("style"))
 
for i in range(1,6):
    print(h20B2AA(str(i)),h292929(str(i)),hEEE9E9(str(i)),h00688B(str(i)))


Output:

some other example

RandomColor Module

The performance of the six functions ( frd2b, frb2d, brd2b, brb2d, bgrandomclr, and fgrandomclr ) is dependent on randomness.

The RandomColor Module’s fgrandomclr() function

Based on the provided text, it returns a string of randomly colored ForeGround text (stringType).fgandomclr means foreground random color.

Function’s structure:
Defined as: def fgrandomclr(String: str) -> str

Example:

Python3




from Designer.RandomColor import *
 
print(fgrandomclr(""" Python is a high-level, \
general-purpose programming language.
Its design philosophy emphasizes code readability \
with the use of significant indentation.
Python is dynamically-typed and garbage-collected. \
It supports multiple programming paradigms,
including structured (particularly procedural), \
object-oriented and functional programming.
It is often described as a "batteries included" \
language due to its comprehensive standard library.
"""))


Output:

sample output for the above code

The RandomColor Module’s bgrandomclr() function :

Using the provided text as the basis, it returns random colored BackGround(stringType) text. bgrandomclr means background random color.

Function’s structure:
Defined as: def bgrandomclr(String: str) -> str

Example:

Python3




from Designer.RandomColor import *
 
print(bgrandomclr(""" Python is a high-level, \
general-purpose programming language.
Its design philosophy emphasizes code readability \
with the use of significant indentation.
Python is dynamically-typed and garbage-collected. \
It supports multiple programming paradigms,
including structured (particularly procedural), \
object-oriented and functional programming.
It is often described as a "batteries included" \
language due to its comprehensive standard library.
"""))


Output:

 

The RandomColor Module’s brb2d() function

It will return the specified text’s bright to dull color as the background color. brb2d means background bright to a dull color.

Function’s structure: brb2d(String: str) -> str

Example:

Python3




from Designer.RandomColor import *
 
print(brb2d(""" Python is a high-level,
general-purpose
programming language."""))


Output:

 

The RandomColor Module’s brd2b() function

It will restore the specified text’s dull to bright color as the background. brd2b means background bright to dull text.

Function’s structure: brd2b(String: str) -> str

Example:

Python3




from Designer.RandomColor import *
 
print(brd2b(""" Python is a high-level,
general-purpose
programming language."""))


Output:

 

The RandomColor Module’s frb2d() function

It will change the text color for ForeGround from bright to dull. frb2d means foreground bright to dull text.

Function’s structure: frb2d(String: str) -> str

Example:

Python3




from Designer.RandomColor import *
 
print(frb2d(""" Python is a high-level,
general-purpose
programming language."""))


 Output:

 

The RandomColor Module’s frd2b() function :

It will change the text for ForeGround from a dull to a bright color. frd2b means foreground dull to a bright color text.

Function’s structure:
Defined as: def frd2b(String: str) -> str

Example:

Python3




from Designer.RandomColor import *
 
print(frd2b(""" Python is a high-level, \
general-purpose programming language."""))


Output:

 

General Module

This model includes frequently employed functionalities. There are 14 functions and one class in it.

The General Module’s img_to_ascii() function

This module, which converts images into ASCII code and returns a string, is really intriguing.

Function’s structure: img_to_ascii(Path: str) -> str

Example

To convert an image into ASCII art.

Python3




from Designer.General import *
 
a = img_to_ascii(".../.../.../.../path")
 
with open("sample.txt",'w') as f:
    f.writelines(a)
f.close()


Output:

 

The General Module’s image_art() function

The program, which converts images into colorful text code, is also extremely intriguing. The colored text in the terminal replicates the image. The string is the return data type.

Function’s structure: image_art(Character: str, Path: str, Type: str = “fg”) -> str

  • Character = it accepts a string (or) character, and using this output is used to replicate the provided image.
  • path = Its necessity is the path. File’s location
  • Type = inputs like Foreground[“fg”] or Background[“bg”] are required. Consequently, the outcome replicated that. to illustrate, use image art(“*”, /../../)

Example: 

Image to colorful text art conversion.

Python3




from Designer.General import *
 
image_art = image_art("*",".../.../.../file","fg")
#                      |   |                  |
#                      |   |                  |======= > You can alter the
                                # Pixel's foreground or background behaviour.
#                      |   |=====> File Path
#                      |
#                      |======= > Any charter is acceptable here.
                        # replicated into the output. It behaves like a Pixel.
print(image_art)


Output:

 

Original image:

original image

The General Module’s start() and stop() function

start(): The specified colors are applied in the terminal after calling this start function. Applying the specified color to text will continue until the stop function is called.

  • Function’s structure:  start( String : str ,Type_ : str =”fg”, style : int = 0) -> str :

stop(): It halts the coloring process, which is initiated by the start function.

  • Function’s structure: def stop() -> str :

Example: 

Printing text using the start and stop function.

Python3




from Designer.General import start,stop
 
a = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
 
# two attr
start("red","fg")
print("hello everyone")
stop()
# two attr
start("green",'bg',4)
print("it's print green background text with underline")
stop()
# single attr
start("blue")
print(a)
stop()


Output:

 

The General Module’s clrscreen() function

It just cleared the terminal’s display.

Example:

Python3




from Designer.General import clrscreen
 
a = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
 
print(a)
clrscreen()


Output:

It’s clear the terminal’s display.

 

The General Module’s Fullbgclr() function

According to the specified parameters, it applies background and foreground colors to the entire terminal screen.

Function’s structure: Fullbgclr( FColor : str, BColor :str ) -> None:

These Values make for suitable inputs

{‘BLACK’: 0, ‘BLUE’: 1, ‘GREEN’: 2,’AQUA’: 3, ‘RED’: 4, ‘PURPLE’: 5, ‘YELLOW’:6, ‘WHITE ‘: 7, ‘GRAY’: 8, ‘LIGHT BLUE: 9,’LIGHT GREEN’: ‘A’, ‘BRIGHT WHITE’: ‘F’,’LIGHT AQUA’: ‘B’, ‘LIGHT YELLOW’: ‘E’,’LIGHT RED’: ‘C’, ‘LIGHT PURPLE’: ‘D’}

Example:

Python3




from Designer.General import Fullbgclr
 
a = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
 
Fullbgclr("C","7")
print(a)


Output:

 

The General Module’s color() function :

It just returns the text’s colored text value after applying the specified color string.

Function’s structure: color(TColor : str, String:str)->str:

Example:

Python3




from Designer.General import color
 
a = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
 
colored_txt = color("brown",a)
print(colored_txt)
print("When the text for that function is given, the colour will be applied.")
print()


Output :

 

The General Module’s color convention functions

There are 6 different convention functions available in this module.

  • rgb_to_txt() function: It is utilized to print colored text using a specified RGB value.
  • rgb_to_name() function: The name of the data is returned along with the RGB values otherwise it returns 0.
  •  name_to_rgb() function: The data’s return value depends on the specified name; otherwise, it returns None.
  • hex_to_name() function: The specified hex in data will return the value of the module that is currently being developed; otherwise, it will return None.
  • rgb_to_hex() function: If RGB data is provided, that value is returned; otherwise, None is returned.
  • hex_to_rgb() function: The data’s return value is the supplied hex value; otherwise, None is returned.

Example:

Python3




from Designer.General import *
 
hex_to_name = hex_to_name("#FFFFFF")
hex_to_rgb=hex_to_rgb("#545454")
rgb_to_hex=rgb_to_hex((255,255,255))
rgb_to_name=rgb_to_name((1,1,1))
rgb_to_txt=rgb_to_txt("geeksforgeeks",(0,0,0))
 
print(" hex_to_name",hex_to_name,"\n",
      "hex_to_rgb",hex_to_rgb,"\n",
      "rgb_to_hex",rgb_to_hex,"\n",
      "rgb_to_name",rgb_to_name,"\n",
      "rgb_to_txt",rgb_to_txt,"\n",     
      )


Output:

 



Last Updated : 01 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads