stringalign

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2025 License: MIT Imports: 6 Imported by: 0

README

stringalign logo

stringalign is a lightweight Go package for wrapping and aligning multi‑line text to a fixed width. It supports left, right, center, and full‑justify modes, preserves ANSI escape sequences (via ansiwalker), and handles full Unicode width correctly using go-runewidth.

Features

  • Left, Right, Center & Justify: wrap text then align each line within your width limit.
  • ANSI‑aware: skips over CSI, OSC, DCS, SOS, PM, APC, and C1 codes so colors and styles don’t break wrapping.
  • Unicode safe: computes visual width with full rune support (including emojis and wide CJK characters).
  • Simple API: four top‑level functions—LeftAlign, RightAlign, CenterAlign, Justify—each returning string, error.

🚀 Getting Started

go get github.com/galactixx/stringalign@latest

📚 Usage

import (
    "fmt"
    "github.com/galactixx/stringalign"
)

text := "Hello, 🌍!\nThis is a long line that will be wrapped and aligned."
width := 30

Left Align

left, _ := stringalign.LeftAlign(text, width)
fmt.Println(left)

Output:

Hello, 🌍!
This is a long line that will
be wrapped and aligned.

Right Align

right, _ := stringalign.RightAlign(text, width)
fmt.Println(right)

Output:

                    Hello, 🌍!
 This is a long line that will
       be wrapped and aligned.

Center Align

center, _ := stringalign.CenterAlign(text, width)
fmt.Println(center)

Output:

          Hello, 🌍!
This is a long line that will
   be wrapped and aligned.

Justify

justified, _ := stringalign.Justify(text, width)
fmt.Println(justified)

Output:

Hello, 🌍!
This  is a long line that will
be wrapped and aligned.

🔍 API

func LeftAlign(str string, limit int, tabSize int) (string, error)
func RightAlign(str string, limit int, tabSize int) (string, error)
func CenterAlign(str string, limit int, tabSize int) (string, error)
func Justify(str string, limit int, tabSize int) (string, error)
  • str: input multi‑line text (may include ANSI escapes).
  • limit: maximum visible width per line.
  • tabSize: number of spaces to replace tabs when aligning.
  • returns: aligned text with \n‑separated lines, or an error on wrap failure.

🤝 License

This project is licensed under the MIT License. See the LICENSE file for details.

📞 Contact

For questions or support, please open an issue on the GitHub repository.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CenterAlign

func CenterAlign(str string, limit int, tabsize int) (string, error)

CenterAlign wraps and center-aligns the input text within the given limit.

func Justify

func Justify(str string, limit int, tabsize int) (string, error)

Justify wraps and justifies the input text within the given limit.

func LeftAlign

func LeftAlign(str string, limit int, tabsize int) (string, error)

LeftAlign wraps and left-aligns the input text within the given limit.

func RightAlign

func RightAlign(str string, limit int, tabsize int) (string, error)

RightAlign wraps and right-aligns the input text within the given limit.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL