linebreak

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2024 License: MIT Imports: 6 Imported by: 1

README

linebreak Go Reference CI Status MIT License

A library for breaking a given text into lines within a specified width. This library also supports per-line indentation.

Import declaration

To use this package in your code, the following import declaration is necessary.

import "github.com/sttk/linebreak"

Usage

The following code breaks the argument text into lines within the terminal width, and outputs them to stdout.

iter := linebreak.New(text, linebreak.TermCols())
for {
	line, more := iter.Next()
	fmt.Println(line)
	if !more {
		break
	}
}

Supporting Go versions

This library supports Go 1.18 or later.

Actual test results for each Go version:
% gvm-fav
Now using version go1.18.10
go version go1.18.10 darwin/amd64
ok  	github.com/sttk/linebreak	0.164s	coverage: 98.3% of statements

Now using version go1.19.13
go version go1.19.13 darwin/amd64
ok  	github.com/sttk/linebreak	0.170s	coverage: 98.3% of statements

Now using version go1.20.14
go version go1.20.14 darwin/amd64
ok  	github.com/sttk/linebreak	0.127s	coverage: 98.3% of statements

Now using version go1.21.7
go version go1.21.7 darwin/amd64
ok  	github.com/sttk/linebreak	0.159s	coverage: 98.3% of statements

Now using version go1.22
go version go1.22.0 darwin/amd64
ok  	github.com/sttk/linebreak	0.128s	coverage: 98.3% of statements

Back to go1.22
Now using version go1.22

License

Copyright (C) 2023-2024 Takayuki Sato

This program is free software under MIT License.
See the file LICENSE in this distribution for more details.

Documentation

Overview

Package github.com/sttk/linebreak is a library for breaking a given text into lines within a specified width. This library also supports per-line indentation.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RuneWidth added in v0.4.0

func RuneWidth(r rune) int

RuneWidth is the function that returns the display width of the specified rune. A display width is determined by the Unicode Standard Annex #11 (UAX11) East-Asian-Width.

func TermCols added in v0.4.0

func TermCols() int

TermCols is the function that returns the column count of the current terminal. This count is the number of ASCII printable characters. If it failed to get the count, this function returns the fixed number: 80.

func TermSize added in v0.4.0

func TermSize() (cols, rows int)

TermSize is the function that returns the column count and row count of the current terminal. These counts are the numbers of ASCII printable characters. If it failed to get these counts, this function returns the fixed numbers: 80 columns and 24 rows..

func TextWidth added in v0.3.0

func TextWidth(text string) int

TextWidth is the function that returns the display width of the spe This function calculates the width of the specified text taking into account the letter width determined by the Unicode Standard Annex #11 (UAX11) East-Asian-Width.

Types

type LineIter

type LineIter struct {
	// contains filtered or unexported fields
}

LineIter is the struct that outputs the given string line by line. This struct can control the overall line width and the indentation from any desired line.

Example
package main

import (
	"fmt"

	"github.com/sttk/linebreak"
)

func main() {
	text := "Go is a new language. Although it borrows ideas from existing " +
		"languages, it has unusual properties that make effective Go programs " +
		"different in character from programs written in its relatives. " +
		"\n\n(Quoted from 'Effective Go')"

	fmt.Println("....:....1....:....2....:....3....:....4....:....5")
	iter := linebreak.New(text, 50)
	for {
		line, more := iter.Next()
		fmt.Println(line)
		if !more {
			break
		}
	}

}
Output:

....:....1....:....2....:....3....:....4....:....5
Go is a new language. Although it borrows ideas
from existing languages, it has unusual properties
that make effective Go programs different in
character from programs written in its relatives.

(Quoted from 'Effective Go')

func New

func New(text string, lineWidth int) LineIter

New is the function that creates a LineIter instance which outputs the given string line by line. The second arguument is the width of the output lines.

func (*LineIter) Init

func (iter *LineIter) Init(text string)

Init is the method to re-initialize with an argument string for reusing this instance.

func (*LineIter) Next

func (iter *LineIter) Next() (string, bool)

Next is the method that returns a string of the next line and a bool which indicates whether there are more next lines or not.

func (*LineIter) SetIndent

func (iter *LineIter) SetIndent(indent string)

SetIndent is the method to set an indentation for the subsequent lines.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/sttk/linebreak"
)

func main() {
	text := "Go is a new language. Although it borrows ideas from existing " +
		"languages, it has unusual properties that make effective Go programs " +
		"different in character from programs written in its relatives. " +
		"\n\n(Quoted from 'Effective Go')"

	fmt.Println("....:....1....:....2....:....3....:....4....:....5")

	iter := linebreak.New(text, 50)
	line, more := iter.Next()
	fmt.Println(line)

	if more {
		for i := 1; ; i++ {
			iter.SetIndent(strings.Repeat(" ", i*2))
			line, more := iter.Next()
			fmt.Println(line)
			if !more {
				break
			}
		}
	}

}
Output:

....:....1....:....2....:....3....:....4....:....5
Go is a new language. Although it borrows ideas
  from existing languages, it has unusual
    properties that make effective Go programs
      different in character from programs written
        in its relatives.

            (Quoted from 'Effective Go')

Jump to

Keyboard shortcuts

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