CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyfiglet

Pure Python implementation of FIGlet for creating ASCII art text from regular text using various fonts

Pending
Overview
Eval results
Files

string-manipulation.mddocs/

String Manipulation

Enhanced string processing capabilities for ASCII art including reversing, flipping, and whitespace normalization. FigletString extends standard string functionality with ASCII art-specific operations.

Capabilities

FigletString Class

Enhanced string class that inherits from the standard string type while adding ASCII art manipulation methods.

class FigletString:
    """
    ASCII art string with manipulation methods.
    
    Inherits from unicode string type, providing all standard string methods
    plus additional ASCII art manipulation capabilities.
    """

Text Reversal

Creates a horizontal mirror image of the ASCII art by reversing each line and translating characters to their mirrored equivalents.

def reverse(self) -> FigletString:
    """
    Create horizontal mirror image of ASCII art.
    
    Returns:
    FigletString: Horizontally reversed ASCII art
    
    Behavior:
    - Reverses character order in each line
    - Translates characters using reverse mapping (/->\\, etc.)
    - Preserves line count and overall structure
    """

Usage Example

import pyfiglet

# Create ASCII art
text = pyfiglet.figlet_format("Hello", font="standard")
print("Original:")
print(text)

# Reverse the text
reversed_text = text.reverse()
print("Reversed:")
print(reversed_text)

Text Flipping

Creates a vertical mirror image of the ASCII art by flipping lines upside down and translating characters to their flipped equivalents.

def flip(self) -> FigletString:
    """
    Create vertical mirror image of ASCII art.
    
    Returns:
    FigletString: Vertically flipped ASCII art
    
    Behavior:
    - Reverses line order (top becomes bottom)
    - Translates characters using flip mapping (^->v, etc.)
    - Preserves character positions within lines
    """

Usage Example

import pyfiglet

# Create ASCII art
text = pyfiglet.figlet_format("Hello", font="standard")
print("Original:")
print(text)

# Flip the text
flipped_text = text.flip()
print("Flipped:")
print(flipped_text)

Whitespace Normalization

Removes surrounding empty lines while preserving internal structure and leading whitespace within the ASCII art.

def strip_surrounding_newlines(self) -> str:
    """
    Remove empty leading and trailing lines.
    
    Returns:
    str: ASCII art with surrounding empty lines removed
    
    Behavior:
    - Removes empty lines at start and end
    - Preserves internal empty lines within the art
    - Preserves leading whitespace on each line
    - Returns regular string (not FigletString)
    """

Usage Example

import pyfiglet

# Create ASCII art (often has surrounding newlines)
text = pyfiglet.figlet_format("Hi", font="standard")
print(f"Original length: {len(text.splitlines())} lines")
print(repr(text))

# Strip surrounding newlines
stripped = text.strip_surrounding_newlines()
print(f"Stripped length: {len(stripped.splitlines())} lines")
print(repr(stripped))

Whitespace Standardization

Ensures ASCII art has exactly one empty line before and after the content.

def normalize_surrounding_newlines(self) -> str:
    """
    Ensure exactly one empty line before and after content.
    
    Returns:
    str: ASCII art with normalized surrounding whitespace
    
    Behavior:
    - Adds one newline at start
    - Adds one newline at end
    - Removes any existing surrounding empty lines first
    """

Usage Example

import pyfiglet

# Create ASCII art
text = pyfiglet.figlet_format("Test", font="standard")

# Normalize surrounding newlines
normalized = text.normalize_surrounding_newlines()
print("Normalized output:")
print(repr(normalized))

List-Based Construction

Creates new FigletString instances from lists of strings representing lines.

def newFromList(self, list: list[str]) -> FigletString:
    """
    Create new FigletString from list of lines.
    
    Parameters:
    - list (list[str]): List of strings representing lines
    
    Returns:
    FigletString: New FigletString instance with manipulation methods
    
    Behavior:
    - Joins lines with newlines
    - Adds trailing newline to match FIGlet format
    """

Usage Example

import pyfiglet

# Get original FigletString
text = pyfiglet.figlet_format("ABC", font="standard")

# Modify lines manually
lines = text.splitlines()
modified_lines = [line.replace('A', '@') for line in lines]

# Create new FigletString
modified_text = text.newFromList(modified_lines)
print(modified_text)

Character Translation Maps

FigletString uses internal translation maps for reverse and flip operations:

Reverse Map

  • Mirrors characters horizontally: /\, (), [], {}, <>
  • Preserves most characters unchanged
  • Used by the reverse() method

Flip Map

  • Mirrors characters vertically: ^v, Some character substitutions for visual flipping
  • Used by the flip() method

Combined Operations

FigletString methods can be chained for complex transformations:

import pyfiglet

# Create ASCII art
text = pyfiglet.figlet_format("Hello", font="standard")

# Apply multiple transformations
result = text.reverse().flip().strip_surrounding_newlines()
print(result)

# Or step by step
reversed_text = text.reverse()
flipped_text = reversed_text.flip()  
final_text = flipped_text.strip_surrounding_newlines()

Integration with Standard String Methods

Since FigletString inherits from the standard string type, all regular string methods are available:

import pyfiglet

text = pyfiglet.figlet_format("Hello", font="standard")

# Standard string methods work
lines = text.splitlines()
upper_text = text.upper()
replaced_text = text.replace('H', '@')

# Combined with FigletString methods
modified = text.replace('#', '*').reverse()

Return Types

  • reverse() and flip() return FigletString instances
  • strip_surrounding_newlines() and normalize_surrounding_newlines() return regular str
  • newFromList() returns FigletString instance

This design allows for method chaining while providing clean string output when needed.

Install with Tessl CLI

npx tessl i tessl/pypi-pyfiglet

docs

advanced-rendering.md

cli.md

color-support.md

font-management.md

index.md

string-manipulation.md

text-rendering.md

tile.json