paragraph

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2023 License: MIT Imports: 7 Imported by: 2

README

paragraph

What is paragraph?

paragraph is a small library to manipulate multiple strings as a whole.

Usage

import (
    "github.com/tpfeiffer67/paragraph"
)

Functions

  • New creates a Paragraph with a given capacity.
  • NewWithGivenLen creates a Paragraph with a given length.
  • NewFromStringSlice creates a Paragraph from a string slice.
  • NewWithPresetContent creates a Paragraph with a preset line count and content.
  • NewFromString creates a Paragraph from a string.
  • WriteToFile writes the Paragraph to a file.
  • ToString concatenates the Paragraph into a string with a given line separator.
  • String, the Stringer interface.
  • Width returns the length of the longest string in the Paragraph.
  • Cut truncates the Paragraph to a given maximum width by cutting strings that exceed it.
  • Limit truncates the Paragraph to a given maximum width by splitting strings that exceed it.
  • PadRight pads the Paragraph on the right side of each string with a fill pattern to achieve a given width.
  • Sort sorts the Paragraph in lexicographically increasing order.

Dependencies

The package runesstr is imported to work with Unicode characters in the strings.

Examples

Example 1

package main

import (
	"fmt"

	"github.com/tpfeiffer67/paragraph"
)

const loremipsum = `Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`

func main() {
	ms := paragraph.NewFromString(loremipsum)
	fmt.Println(ms)
}

Contributing

If you'd like to contribute to this repository, feel free to submit a pull request. Please include tests for any new functions or changes to existing functions.

License

This repository is licensed under the MIT License. See the LICENSE file for more information.

Author

Documentation

Index

Examples

Constants

View Source
const (
	AccoladesStyleCount     = 3
	AccoladesStyleMaxIndex  = int(AccoladesStyleUnicode)
	AccoladesStyleLastValue = AccoladesStyleUnicode
)
View Source
const (
	BoxStyleCount     = 21
	BoxStyleMaxIndex  = int(BoxStyleFantasy4)
	BoxStyleLastValue = BoxStyleFantasy4
)
View Source
const (
	LabelAlignCount     = 3
	LabelAlignMaxIndex  = int(LabelAlignRight)
	LabelAlignLastValue = LabelAlignRight
)
View Source
const MultiStringsMaxWidth = 1000

Variables

This section is empty.

Functions

This section is empty.

Types

type AccoladesStyle

type AccoladesStyle int
Example
for i := 0; i < 3; i++ {
	fmt.Println(AccoladesStyle(i))
}
Output:
AccoladesStyleNone
AccoladesStyleAscii
AccoladesStyleUnicode
const (
	AccoladesStyleNone AccoladesStyle = iota
	AccoladesStyleAscii
	AccoladesStyleUnicode
)

func AccoladesStyleFromString

func AccoladesStyleFromString(s string) (AccoladesStyle, error)

func (AccoladesStyle) String

func (v AccoladesStyle) String() string

type BoxPattern

type BoxPattern struct {
	TopLeftCorner     string
	TopBorder         string
	TopRightCorner    string
	LeftBorder        string
	RightBorder       string
	BottomLeftCorner  string
	BottomBorder      string
	BottomRightCorner string
}

func GetBoxPattern

func GetBoxPattern(style BoxStyle) BoxPattern

type BoxSettings

type BoxSettings struct {
	Width            int
	TopLabel         string
	TopLabelAlign    LabelAlign
	BottomLabel      string
	BottomLabelAlign LabelAlign
}

BoxSettings{30, "", Left, "", Left}

type BoxStyle

type BoxStyle int
Example
for i := 2; i <= BoxStyleMaxIndex; i++ {
	fmt.Println(NewFromString(BoxStyle(i).String()).PadRight(" ", 38).Surround(" ", " ").AutoBox(BoxSettings{40, "", LabelAlignLeft, "", LabelAlignLeft}, GetBoxPattern(BoxStyle(i))))
}
Output:
┌────────────────────────────────────────┐
│ BoxStyleSingleLine                     │
└────────────────────────────────────────┘

╭────────────────────────────────────────╮
│ BoxStyleSingleLineRounded              │
╰────────────────────────────────────────╯

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ BoxStyleBold                           ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

╒════════════════════════════════════════╕
│ BoxStyleSingleVDoubleH                 │
╘════════════════════════════════════════╛

╓────────────────────────────────────────╖
║ BoxStyleSingleHDoubleV                 ║
╙────────────────────────────────────────╜

╔════════════════════════════════════════╗
║ BoxStyleDoubleLine                     ║
╚════════════════════════════════════════╝

▛▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▜
▌ BoxStyleExtraBold                      ▐
▙▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▟

▞▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▚
▌ BoxStyleExtraBoldRounded               ▐
▚▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▞

█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█
█ BoxStyleMaxBold                        █
█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█

░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░ BoxStyleBlocksLightShade               ░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒ BoxStyleBlocksMediumShade              ▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
▓ BoxStyleBlocksDarkShade                ▓
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

██████████████████████████████████████████
█ BoxStyleBlocks                         █
██████████████████████████████████████████

..........................................
: BoxStyleDots                           :
:........................................:

◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆
◆ BoxStyleDiamonds                       ◆
◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆

╭╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╼╮
╽ BoxStyleFantasy1                       ╿
╰╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╾╯

╱▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔╲
│ BoxStyleFantasy2                       │
╲▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁╱

▁▂▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▂▁
▌ BoxStyleFantasy3                       ▐
▜▃▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▃▛

▁▂▃▂▁▂▃▂▁▂▃▂▁▂▃▂▁▂▃▂▁▂▃▂▁▂▃▂▁▂▃▂▁▂▃▂▁▂▃▂▁▂
█ BoxStyleFantasy4                       █
█▃▂▁▂▃▂▁▂▃▂▁▂▃▂▁▂▃▂▁▂▃▂▁▂▃▂▁▂▃▂▁▂▃▂▁▂▃▂▁▂█
const (
	BoxStyleNone BoxStyle = iota
	BoxStyleSpaceChar
	BoxStyleSingleLine
	BoxStyleSingleLineRounded
	BoxStyleBold
	BoxStyleSingleVDoubleH
	BoxStyleSingleHDoubleV
	BoxStyleDoubleLine
	BoxStyleExtraBold
	BoxStyleExtraBoldRounded
	BoxStyleMaxBold
	BoxStyleBlocksLightShade
	BoxStyleBlocksMediumShade
	BoxStyleBlocksDarkShade
	BoxStyleBlocks
	BoxStyleDots
	BoxStyleDiamonds
	BoxStyleFantasy1
	BoxStyleFantasy2
	BoxStyleFantasy3
	BoxStyleFantasy4
)

func BoxStyleFromString

func BoxStyleFromString(s string) (BoxStyle, error)

func (BoxStyle) String

func (v BoxStyle) String() string

type LabelAlign

type LabelAlign int
const (
	LabelAlignLeft LabelAlign = iota
	LabelAlignCenter
	LabelAlignRight
)

func LabelAlignFromString

func LabelAlignFromString(s string) (LabelAlign, error)

func (LabelAlign) String

func (v LabelAlign) String() string

type Paragraph

type Paragraph []string

func New

func New(cap int) Paragraph

New creates and returns a new Paragraph slice with a given capacity. - cap is an integer representing the desired capacity of the slice.

func NewFromString

func NewFromString(s string) Paragraph

NewFromString creates and returns a new Paragraph slice from a string. The given string is split into separate substrings at each occurrence of a newline character. - s is a string from which to create the new Paragraph slice.

func NewFromStringSlice

func NewFromStringSlice(s []string) (linesOut Paragraph)

NewFromStringSlice creates and returns a new Paragraph slice from a string slice. - s is a slice of strings from which to create the new Paragraph slice.

func NewWithGivenLen

func NewWithGivenLen(length int) Paragraph

NewWithGivenLen creates and returns a new Paragraph slice with a given length. - l is the desired length of the slice.

func NewWithPresetContent

func NewWithPresetContent(line string, count int) (linesOut Paragraph)

NewWithPresetContent creates and returns a new Paragraph slice with a preset line count and content. - line is a string representing the content to be added to each line of the new slice. - count is the desired number of lines in the new slice.

func (Paragraph) Accolades

func (linesIn Paragraph) Accolades(style AccoladesStyle) (linesOut Paragraph)

func (Paragraph) Append

func (linesIn Paragraph) Append(linesToAppend Paragraph) Paragraph

Append appends the strings from another Paragraph slice to the end of the current one.

func (Paragraph) AutoAccolades

func (linesIn Paragraph) AutoAccolades(style AccoladesStyle) (linesOut Paragraph)

func (Paragraph) AutoBox

func (linesIn Paragraph) AutoBox(settings BoxSettings, pattern BoxPattern) Paragraph

func (Paragraph) Box

func (linesIn Paragraph) Box(settings BoxSettings, pattern BoxPattern) (linesOut Paragraph)

func (Paragraph) Cut

func (linesIn Paragraph) Cut(maxWidth int) (linesOut Paragraph)

Cut truncates the Paragraph slice to a given maximum width by cutting strings that exceed it. - maxWidth is the maximum width to which to truncate the strings.

func (Paragraph) Limit

func (linesIn Paragraph) Limit(maxWidth int) (linesOut Paragraph)

Limit truncates the Paragraph slice to a given maximum width by splitting strings that exceed it. - maxWidth is the maximum width to which to truncate the strings.

func (Paragraph) Mustache

func (linesIn Paragraph) Mustache(m map[string]interface{}) (linesOut Paragraph, err error)

func (Paragraph) MustacheNoErr

func (linesIn Paragraph) MustacheNoErr(m map[string]interface{}) (linesOut Paragraph)

func (Paragraph) PadRight

func (linesIn Paragraph) PadRight(fillPattern string, width int) (linesOut Paragraph)

PadRight pads the Paragraph slice on the right side with a given fill pattern to a given width. - fillPattern represents the pattern to use for padding. - width is the desired width of each line after padding.

func (Paragraph) Sort

func (linesIn Paragraph) Sort() Paragraph

Sort sorts the Paragraph slice in lexicographic order and returns the sorted slice.

func (Paragraph) String

func (lines Paragraph) String() string

String implements the Stringer interface for Paragraph.

func (Paragraph) Surround

func (linesIn Paragraph) Surround(left string, right string) (linesOut Paragraph)

Surround surrounds each line of the Paragraph slice with a given left and right string. - left is the left-side surround for each line. - right is the right-side surround.

func (Paragraph) ToString

func (lines Paragraph) ToString(linesSeparator string) (result string)

ToString concatenates the Paragraph slice into a string with a given line separator. - linesSeparator is a string representing the separator to use between each line.

func (Paragraph) Width

func (lines Paragraph) Width() (width int)

Width returns the width of the Paragraph slice, which is the length of the longest string in the slice.

func (Paragraph) WriteToFile

func (lines Paragraph) WriteToFile(fileName string) (err error)

WriteToFile writes the Paragraph slice to a file with a given filename. - fileName is the name of the file to which to write the slice.

Jump to

Keyboard shortcuts

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