Pystyle: Mastering the Art of Pythonic Code

Pystyle is a type of Python library through which you can create an interactive TUI. But do you know how it works? Read this article to know more.

What is TUI?

Pystyle is a kind of TUI. Before knowing what Pystyle does, you should know what a TUI is. TUI stands for a text-based user interface. This interface lets you interact with the computer using text commands. Users don’t need to know how a mouse or any such device is used. These commands directly help you in establishing a connection with your device.

TUI and GUI

Visually impaired people also prefer using TUI as compared to GUI. TUI is more efficient than GUI and doesn’t require graphical elements. It just works on text commands. It also works with screen readers and provides better accessibility.

Uses of a TUI

This type of interface is commonly used in:

  • Command-line interfaces (CLIs)
  • Games
  • Text editors

About pystyle

If a user wishes to create an interface that is based entirely on text, he can use pystyle. It’s a recently launched library. Now, let’s see why it is gaining popularity lately.

Features of pystyle

This library of Python focuses on providing a text-based interface and is convenient to use. It is considered quite useful due to the following features:

  • Text Layout
  • Text formatting
  • Different Widgets
  • Input handling

The Pystyle library helps you change the text color, size, font, and alignment. This makes the interface enticing. You can create borders or justify the text you have written. You can automate key presses and mouse clicks with their help. Also, you can include buttons, menus, text boxes, and other such widgets.

Syntax of pystyle

In order to start using pystyle, after installing, you can import it. The installation takes place with the pip command only. You may enter this in the command prompt or on the terminal.

pip install pystyle
import pystyle

Working with pystyle text formatting

Pystyle provides you with multiple functions to format your text. Some of these are:

  • Color
  • Error
  • Bold
  • Underline
  • Strikethrough
  • Reset

As of now, black, red, green, yellow, blue, magenta, cyan, and white are available in the library. The Error function highlights the Error. The Reset argument restores the changes to the original settings. You can also strike a line using the Strikethrough function.

Now, let us understand all these functions with the help of an example.

import pystyle

def Color(text, color):
    """Changes the color of text.

    Args:
        text: The text to be colored.
        color: The color to change the text to.

    Returns:
        The colored text.
    """

    if color not in pystyle.Colors:
        raise ValueError("Invalid color")

    return pystyle.Colorate.Horizontal(pystyle.Colors[color], text)

def Error(text):
    """Makes the text appear as if it is an error.

    Args:
        text: The text to be displayed as an error.

    Returns:
        The error text.
    """

    return Color(text, pystyle.Colors.red)

def Bold(text):
    """Makes the text bold.

    Args:
        text: The text to be made bold.

    Returns:
        The bold text.
    """

    return pystyle.Colorate.Horizontal(pystyle.Colors.bold, text)

def Underline(text):
    """Underlines the text.

    Args:
        text: The text to be underlined.

    Returns:
        The underlined text.
    """

    return pystyle.Colorate.Horizontal(pystyle.Colors.underline, text)

def Strikethrough(text):
    """Strikes a line through the text.

    Args:
        text: The text to have a line struck through it.

    Returns:
        The text with a line struck through it.
    """

    return pystyle.Colorate.Horizontal(pystyle.Colors.strikethrough, text)

def Reset(text):
    """Resets all text formatting to its default values.

    Args:
        text: The text to have its formatting reset.

    Returns:
        The text with its formatting reset.
    """

    return pystyle.Colorate.Horizontal(pystyle.Colors.reset, text)

Text layouts in pystyle

The Pystyle library provides varied text layouts to help you align your text and provide a border to it. The text layout functions that pystyle offers are:

  • Center.XCenter
  • Center.YCenter
  • Box.Outline
  • Box.DoubleOutline
  • Box.RoundedOutline

The XCenter function aligns the text to the center of the page in a horizontal manner, whereas the YCenter function focuses on vertical alignment. The box features provide a border to the text. There are options to add a double and rounded border, too.

Now you can check the syntax of these functions with the help of this example:

import pystyle

def Center.XCenter(text):
    """Centers the text on the screen horizontally.

    Args:
        text: The text to be centered.

    Returns:
        The centered text.
    """

    return pystyle.Colorate.Horizontal(pystyle.Colors.center, text)

def Center.YCenter(text):
    """Centers the text on the screen vertically.

    Args:
        text: The text to be centered.

    Returns:
        The centered text.
    """

    return pystyle.Colorate.Vertical(pystyle.Colors.center, text)

def Box.Outline(text):
    """Creates a border around the text.

    Args:
        text: The text to be bordered.

    Returns:
        The text with a border around it.
    """

    return pystyle.Box.Lines(text)

def Box.DoubleOutline(text):
    """Creates a double border around the text.

    Args:
        text: The text to be double bordered.

    Returns:
        The text with a double border around it.
    """

    return pystyle.Box.DoubleCube(text)

def Box.RoundedOutline(text):
    """Creates a rounded border around the text.

    Args:
        text: The text to have a rounded border around it.

    Returns:
        The text with a rounded border around it.
    """

    return pystyle.Box.RoundedLines(text)

Handling of inputs in pystyle

For input handling, the given functions are present:

  • Input.GetKey
  • Input.WaitForEnter
  • Input.GetMouseClick
  • Input.WaitForMouseClick

These functions perform as the name suggests. You can check the syntax of these functions using the code given below:

import pystyle

def Input.GetKey():
    """Gets a key press from the user.

    Returns:
        The key that was pressed.
    """

    return pystyle.Input.GetKey()

def Input.WaitForEnter():
    """Waits for the user to press the Enter key."""

    pystyle.Input.WaitForEnter()

def Input.GetMouseClick():
    """Gets a mouse click from the user.

    Returns:
        The coordinates of the mouse click.
    """

    return pystyle.Input.GetMouseClick()

def Input.WaitForMouseClick():
    """Waits for the user to click the mouse."""

    pystyle.Input.WaitForMouseClick()

Other functions of pystyle

The pystyle library has also introduced some features related to animations. Some of them are:

  • Animation.FadeIn
  • Animation.FadeOut
  • Animation.Typewrite
  • System.ClearScreen
  • System.Pause

The first two functions will fade in or fade out the text. The typewrite function is going to type the text, and the last two functions clear the screen and pause it for a couple of seconds. Now let us see how these functions actually work in pystyle with the help of the given example:

import pystyle

def Animation.FadeIn(text):
    """Fades in the text.

    Args:
        text: The text to fade in.

    Returns:
        The fading

Pystyle on Anaconda

If you are an Anaconda user, you need not worry. Using this command, you can use pystyle in the conda environment. Once this is done, you need to simply import the library. The rest of the workflow is the same.

conda install -c conda-forge
import pystyle

# Create a border around the screen
print(pystyle.Box.Outline("This is a TUI"))

# Center some text on the screen
print(pystyle.Center.XCenter("Hello, World!"))

# Wait for the user to press the Enter key
pystyle.Input.WaitForEnter()

# Clear the screen
pystyle.System.ClearScreen()

FAQs

Is Pystyle free to use?

Yes, it is free to use.

Which license does this library have?

It has the Eclipse Public License 2.0.

Conclusion

This blog introduces users to the Python pystyle library. It covers the top features this library provides in addition to details of a Text-based interface, commonly known as TUI. The code examples provide a lucid understanding of the attributes of this library.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments