Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A collection of (ANSI-sequence aware) text reflow operations & algorithms

License

NotificationsYou must be signed in to change notification settings

muesli/reflow

Repository files navigation

Latest ReleaseBuild StatusCoverage StatusGo ReportCardGoDoc

A collection of ANSI-aware methods andio.Writers helping you to transformblocks of text. This means you can still style your terminal output with ANSIescape sequences without them affecting the reflow operations & algorithms.

Word-Wrapping

Thewordwrap package lets you word-wrap strings or entire blocks of text.

import"github.com/muesli/reflow/wordwrap"s:=wordwrap.String("Hello World!",5)fmt.Println(s)

Result:

HelloWorld!

The word-wrapping Writer is compatible with theio.Writer /io.WriteCloser interfaces:

f:=wordwrap.NewWriter(limit)f.Write(b)f.Close()fmt.Println(f.String())

Customize word-wrapping behavior:

f:=wordwrap.NewWriter(limit)f.Breakpoints= []rune{':',','}f.Newline= []rune{'\r'}

Unconditional Wrapping

Thewrap package lets you unconditionally wrap strings or entire blocks of text.

import"github.com/muesli/reflow/wrap"s:=wrap.String("Hello World!",7)fmt.Println(s)

Result:

Hello World!

The unconditional wrapping Writer is compatible with theio.Writer interfaces:

f:=wrap.NewWriter(limit)f.Write(b)fmt.Println(f.String())

Customize word-wrapping behavior:

f:=wrap.NewWriter(limit)f.Newline= []rune{'\r'}f.KeepNewlines=falsef.PreserveSpace=truef.TabWidth=2

Tip: This wrapping method can be used in conjunction with word-wrapping when word-wrapping is preferred but a line limit has to be enforced:

wrapped:=wrap.String(wordwrap.String("Just an example",5),5)fmt.Println(wrapped)

Result:

Justanexample

ANSI Example

s:=wordwrap.String("I really\x1B[38;2;249;38;114mlove\x1B[0m Go!",8)fmt.Println(s)

Result:

ANSI Example Output

Indentation

Theindent package lets you indent strings or entire blocks of text.

import"github.com/muesli/reflow/indent"s:=indent.String("Hello World!",4)fmt.Println(s)

Result:

    Hello World!

There is also an indenting Writer, which is compatible with theio.Writerinterface:

// indent uses spaces per default:f:=indent.NewWriter(width,nil)// but you can also use a custom indentation function:f=indent.NewWriter(width,func(w io.Writer) {w.Write([]byte("."))})f.Write(b)f.Close()fmt.Println(f.String())

Dedentation

Thededent package lets you dedent strings or entire blocks of text.

import"github.com/muesli/reflow/dedent"input:=`    Hello World!  Hello World!`s:=dedent.String(input)fmt.Println(s)

Result:

  Hello World!Hello World!

Padding

Thepadding package lets you pad strings or entire blocks of text.

import"github.com/muesli/reflow/padding"s:=padding.String("Hello",8)fmt.Println(s)

Result:Hello___ (the underlined portion represents 3 spaces)

There is also a padding Writer, which is compatible with theio.WriteCloserinterface:

// padding uses spaces per default:f:=padding.NewWriter(width,nil)// but you can also use a custom padding function:f=padding.NewWriter(width,func(w io.Writer) {w.Write([]byte("."))})f.Write(b)f.Close()fmt.Println(f.String())

About

A collection of (ANSI-sequence aware) text reflow operations & algorithms

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp