How to center figlet text in terminal

LinuxLinuxBeginner
Practice Now

Introduction

In the world of Linux terminal customization, creating visually appealing text displays can enhance your command-line experience. This tutorial explores techniques for centering figlet text, providing developers and Linux enthusiasts with practical methods to stylize terminal output effectively.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/clear("`Screen Clearing`") linux/BasicSystemCommandsGroup -.-> linux/printf("`Text Formatting`") linux/InputandOutputRedirectionGroup -.-> linux/tee("`Output Multiplexing`") subgraph Lab Skills linux/echo -.-> lab-419328{{"`How to center figlet text in terminal`"}} linux/clear -.-> lab-419328{{"`How to center figlet text in terminal`"}} linux/printf -.-> lab-419328{{"`How to center figlet text in terminal`"}} linux/tee -.-> lab-419328{{"`How to center figlet text in terminal`"}} end

Figlet Basics

What is Figlet?

Figlet is a command-line utility for creating large, decorative ASCII text banners in the Linux terminal. It transforms plain text into stylized, large-scale characters using various font styles and configurations.

Installation on Ubuntu

To install Figlet on Ubuntu 22.04, use the following command:

sudo apt-get update
sudo apt-get install figlet

Basic Usage

Simple Text Rendering

Create a basic text banner with default settings:

figlet "Hello World"

Font Styles

Figlet supports multiple font styles. List available fonts:

ls /usr/share/figlet

Select a specific font using the -f option:

figlet -f slant "LabEx"

Font Types

Font Category Description Example Fonts
Standard Default block letters standard, small
Decorative Stylized and artistic banner, big, script
Themed Special design patterns 3d, bubble, digital

Figlet Configuration

graph TD A[Figlet Command] --> B{Configuration Options} B --> C[Font Selection] B --> D[Text Styling] B --> E[Size Adjustment]

Key Parameters

  • -f: Select font style
  • -w: Set banner width
  • -c: Center text horizontally
  • -d: Specify font directory

By mastering these basics, you'll be ready to create impressive ASCII text banners in your Linux terminal with Figlet.

Text Centering Methods

Native Figlet Centering

Using -c Option

Figlet provides a built-in centering option:

figlet -c "LabEx"

This automatically centers the text horizontally in the terminal.

Terminal-Based Centering Techniques

Method 1: Using tput Command

Calculate terminal width and center figlet text:

#!/bin/bash
COLUMNS=$(tput cols)
TEXT="LabEx"
figlet "$TEXT" | sed -e :a -e "s/^.\{1,$(($COLUMNS/2))\}$/ & /;ta"

Method 2: Padding Technique

Manually add padding to center text:

#!/bin/bash
printf "%*s\n" $(( ($(tput cols) + ${#TEXT}) / 2 )) "$(figlet "$TEXT")"

Centering Strategies

graph TD A[Text Centering Methods] --> B[Native Figlet Option] A --> C[Terminal Width Calculation] A --> D[Manual Padding Technique]

Comparison of Centering Methods

Method Pros Cons Complexity
-c Option Simple, Built-in Limited customization Low
tput Method Dynamic width Requires scripting Medium
Padding Technique Precise control More complex High

Advanced Centering Script

#!/bin/bash
center_figlet() {
    local text="$1"
    local width=$(tput cols)
    local figlet_output=$(figlet "$text")
    printf "%*s\n" $(( (width + ${#figlet_output}) / 2 )) "$figlet_output"
}

center_figlet "LabEx Rocks!"

Best Practices

  • Choose method based on specific requirements
  • Consider terminal width variability
  • Test across different terminal sizes
  • Use scripting for complex centering needs

By understanding these methods, you can effectively center figlet text in various Linux terminal environments.

Terminal Styling Tips

Color Enhancement

Using ANSI Color Codes

Combine figlet with color for visual impact:

## Red figlet text
echo -e "\e[31m$(figlet 'LabEx')\e[0m"

## Gradient color effect
echo -e "\e[38;5;196m$(figlet 'LabEx')\e[0m"

Advanced Styling Techniques

Combining Tools

graph TD A[Terminal Styling] --> B[Color Codes] A --> C[Text Effects] A --> D[External Tools] B --> E[ANSI Colors] C --> F[Bold/Italic] D --> G[lolcat/toilet]

Styling Tools Comparison

Tool Function Color Support Complexity
figlet Text Banner Basic Low
lolcat Rainbow Colors Advanced Medium
toilet Text Effects Rich High

Creative Styling Examples

Rainbow Text with lolcat

figlet "LabEx" | lolcat

Multiple Effects Script

#!/bin/bash
style_text() {
    local text="$1"
    figlet "$text" | toilet -f term --gay
}

style_text "Welcome"

Performance Considerations

  • Minimize complex styling in scripts
  • Use color sparingly
  • Test performance on different terminals

Terminal Compatibility

  • Verify color support in different environments
  • Use escape sequences for broad compatibility
  • Consider terminal emulator limitations

By mastering these styling techniques, you can create visually engaging terminal text presentations with figlet and complementary tools.

Summary

By mastering figlet text centering techniques in Linux, you can transform plain terminal text into eye-catching displays. These methods not only improve visual presentation but also demonstrate advanced command-line text manipulation skills that can be applied across various Linux environments and scripting scenarios.

Other Linux Tutorials you may like