Skip to content
/ blox Public

Go package to block-paste text in a character raster buffer. Text is pasted and positioned via X/Y coordinates on a canvas which can be printed as-is or delimited by line breaks at column-width.

License

Notifications You must be signed in to change notification settings

sa6mwa/blox

Repository files navigation

Go Reference

blox

import "github.com/sa6mwa/blox"

Blox is a Go package to block-paste text in a character raster buffer. Text is pasted and positioned via X/Y coordinates on a canvas which can be printed as-is or delimited by line breaks at column-width. Trailing spaces and trailing empty lines can be trimmed from the output and/or before printing the canvas.

The example below illustrates basic usage. See the PutText functions for more examples.

package main

import (
  "fmt"

  "github.com/sa6mwa/blox"
)

func main() {
  b := blox.New().Trim().SetColumnsAndRows(80, 24)

  text := "ABCDE FGHIJ KLMNO" + blox.LineBreak
  text += "PQRST UVWXY ZABCD" + blox.LineBreak

  heading := "CRYPTO" + blox.LineBreak
  heading += "GROUPS"

  str := b.PutText(heading).DrawHorizontalLine(0, 6).DrawVerticalLine(0, 1, ':').
    PutChar('+').MoveDown().MoveX(0).PutText(text).
    Move(9, 0).PutText(text).Join(blox.LineBreak)

  strEndingInNewLine := b.String()

  fmt.Println(str)
  fmt.Println("--")
  fmt.Print(strEndingInNewLine)
}

Output

CRYPTO : ABCDE FGHIJ KLMNO
GROUPS : PQRST UVWXY ZABCD
-------+
ABCDE FGHIJ KLMNO
PQRST UVWXY ZABCD
--
CRYPTO : ABCDE FGHIJ KLMNO
GROUPS : PQRST UVWXY ZABCD
-------+
ABCDE FGHIJ KLMNO
PQRST UVWXY ZABCD

Index

Constants

const LineBreak string = "\n"

Variables

var (
    InitialCanvasCapacity      int  = 80 * 24
    InitialCanvasColumns       int  = 0
    InitialCanvasRows          int  = 0
    InitialCursorPositionX     int  = 0
    InitialCursorPositionY     int  = 0
    InitialLineSpacing         int  = 1
    InitialTrimRightSpaces     bool = true
    InitialTrimFinalEmptyLines bool = false
)

func CutLineShort

func CutLineShort(line string, maxLen int, addThreeDots bool) string

CutLineShort cuts line after maxLen adding dots if addThreeDots is true. Returns a shortened or the original string.

Example

package main

import (
	"fmt"

	"github.com/sa6mwa/blox"
)

func main() {
	line := "Hello world, I am Blox."
	line = blox.CutLineShort(line, 12, true)
	fmt.Println(line)
}

Output

Hello world…

func CutLinesShort

func CutLinesShort(text string, maxLen int, trimTrailingSpace bool) string

CutLinesShort cuts several lines to maxLen and return the new text. Will trim trailing space if trimTrailingSpace is true.

Example

package main

import (
	"fmt"

	"github.com/sa6mwa/blox"
)

const loremIpsum string = "Lorem ipsum dolor sit amet consectetur adipiscing elit torquent ante tortor dui" + blox.LineBreak +
	"augue, dictumst convallis eget tempor pharetra lectus magnis lacinia lacus eu" + blox.LineBreak +
	"nostra. Sagittis dolor mattis laoreet justo mollis est varius etiam nisl, sit" + blox.LineBreak +
	"eleifend nullam magna aptent erat vitae. Nullam suspendisse quis volutpat luctus" + blox.LineBreak +
	"non a cursus dui urna, facilisis ipsum dapibus etiam odio lacus feugiat neque." + blox.LineBreak +
	"Primis pharetra cursus ultrices vel curabitur duis taciti semper, tortor nisl" + blox.LineBreak +
	"urna turpis mauris maecenas ac diam, posuere morbi mi class tincidunt cum" + blox.LineBreak +
	"suspendisse."

func main() {
	text := loremIpsum
	fmt.Print(blox.CutLinesShort(text, 13, true))
}

Output

Lorem ipsum d
augue, dictum
nostra. Sagit
eleifend null
non a cursus
Primis pharet
urna turpis m
suspendisse.

func LineCount

func LineCount(text string) int

LineCount returns the number of lines (rows) in text (can be used to setup canvas Rows to fit text).

func MaximumLineLength

func MaximumLineLength(text string) int

MaximumLineLength returns number of runes in the longest line (can be used to setup canvas Columns to fit text).

func RowAndColumnCount

func RowAndColumnCount(text string) (int, int)

RowAndColumnCount can be used to initialize a new canvas to fit Move(0,0).PutText(text) with lines of text for example. Returns x, y (column, row) where x represents the longest line count.

type Blox

type Blox struct {
    Columns             int // X
    Rows                int // Y
    Cursor              CursorPosition
    CursorStack         []CursorPosition
    LineSpacing         int
    TrimRightSpaces     bool
    TrimFinalEmptyLines bool
    Canvas              []rune
}

func New

func New() *Blox

func (*Blox) CurrentIndex

func (b *Blox) CurrentIndex() int

Return current index to write/read to/from on canvas based on current cursor row/column position.

func (*Blox) DrawHorizontalLine

func (b *Blox) DrawHorizontalLine(fromX int, toX int, char ...rune) *Blox

DrawHorizontalLine draws hyphens (-) horizontally between two X positions at the current row (Y). You can change the default rune with the optional char.

func (*Blox) DrawSeparator

func (b *Blox) DrawSeparator(char ...rune) *Blox

DrawSeparator draws a horizontal line with hyphens (-) at the current row. You can change the default rune with the optional char.

Example

package main

import (
	"github.com/sa6mwa/blox"
)

func main() {
	b := blox.New().Trim().SetColumnsAndRows(80, 15)

	text := "Lorem ipsum dolor sit amet consectetur adipiscing elit torquent ante tortor dui" + blox.LineBreak
	text += "augue, dictumst convallis eget tempor pharetra lectus magnis lacinia lacus eu" + blox.LineBreak
	text += "nostra. Sagittis dolor mattis laoreet justo mollis est varius etiam nisl, sit" + blox.LineBreak
	text += "eleifend nullam magna aptent erat vitae. Nullam suspendisse quis volutpat luctus" + blox.LineBreak
	text += "non a cursus dui urna, facilisis ipsum dapibus etiam odio lacus feugiat neque." + blox.LineBreak
	text += "Primis pharetra cursus ultrices vel curabitur duis taciti semper, tortor nisl" + blox.LineBreak
	text += "urna turpis mauris maecenas ac diam, posuere morbi mi class tincidunt cum" + blox.LineBreak
	text += "suspendisse." // With or without final new line.

	b.PutText(text).DrawSeparator().PutText("Final line.").PrintCanvas()

}

Output

Lorem ipsum dolor sit amet consectetur adipiscing elit torquent ante tortor dui
augue, dictumst convallis eget tempor pharetra lectus magnis lacinia lacus eu
nostra. Sagittis dolor mattis laoreet justo mollis est varius etiam nisl, sit
eleifend nullam magna aptent erat vitae. Nullam suspendisse quis volutpat luctus
non a cursus dui urna, facilisis ipsum dapibus etiam odio lacus feugiat neque.
Primis pharetra cursus ultrices vel curabitur duis taciti semper, tortor nisl
urna turpis mauris maecenas ac diam, posuere morbi mi class tincidunt cum
suspendisse.
--------------------------------------------------------------------------------
Final line.

func (*Blox) DrawSplit

func (b *Blox) DrawSplit(char ...rune) *Blox

DrawSplit draws a vertical line with pipes (|) at the current column from the top row to the bottom row of the canvas. You can change the default rune with the optional char.

Example

package main

import (
	"fmt"

	"github.com/sa6mwa/blox"
)

func main() {
	b := blox.New().Trim().SetColumnsAndRows(80, 9)
	text := "Lorem ipsum dolor sit amet consectetur adipiscing elit torquent ante tortor dui" + blox.LineBreak
	text += "augue, dictumst convallis eget tempor pharetra lectus magnis lacinia lacus eu" + blox.LineBreak
	text += "nostra. Sagittis dolor mattis laoreet justo mollis est varius etiam nisl, sit" + blox.LineBreak
	text += "eleifend nullam magna aptent erat vitae. Nullam suspendisse quis volutpat luctus" + blox.LineBreak
	text += "non a cursus dui urna, facilisis ipsum dapibus etiam odio lacus feugiat neque." + blox.LineBreak
	text += "Primis pharetra cursus ultrices vel curabitur duis taciti semper, tortor nisl" + blox.LineBreak
	text += "urna turpis mauris maecenas ac diam, posuere morbi mi class tincidunt cum" + blox.LineBreak
	text += "suspendisse." // With or without final new line.

	str := b.PutText(text).DrawSeparator().MoveX(20).DrawSplit().String()
	fmt.Print(str)

}

Output

Lorem ipsum dolor si| amet consectetur adipiscing elit torquent ante tortor dui
augue, dictumst conv|llis eget tempor pharetra lectus magnis lacinia lacus eu
nostra. Sagittis dol|r mattis laoreet justo mollis est varius etiam nisl, sit
eleifend nullam magn| aptent erat vitae. Nullam suspendisse quis volutpat luctus
non a cursus dui urn|, facilisis ipsum dapibus etiam odio lacus feugiat neque.
Primis pharetra curs|s ultrices vel curabitur duis taciti semper, tortor nisl
urna turpis mauris m|ecenas ac diam, posuere morbi mi class tincidunt cum
suspendisse.        |
--------------------|-----------------------------------------------------------

func (*Blox) DrawVerticalLine

func (b *Blox) DrawVerticalLine(fromY int, toY int, char ...rune) *Blox

DrawVerticalLine draws pipes (|) vertically between two Y positions at the current column (X). You can change the default rune with the optional char.

func (*Blox) FprintCanvas

func (b *Blox) FprintCanvas(o *os.File) *Blox
Example

package main

import (
	"os"

	"github.com/sa6mwa/blox"
)

func main() {
	b := blox.New().SetColumnsAndRows(80, 24).Trim()

	text := "Lorem ipsum dolor sit amet consectetur adipiscing elit torquent ante tortor dui" + blox.LineBreak
	text += "augue, dictumst convallis eget tempor pharetra lectus magnis lacinia lacus eu" + blox.LineBreak
	text += "nostra. Sagittis dolor mattis laoreet justo mollis est varius etiam nisl, sit" + blox.LineBreak
	text += "eleifend nullam magna aptent erat vitae. Nullam suspendisse quis volutpat luctus" + blox.LineBreak
	text += "non a cursus dui urna, facilisis ipsum dapibus etiam odio lacus feugiat neque." + blox.LineBreak
	text += "Primis pharetra cursus ultrices vel curabitur duis taciti semper, tortor nisl" + blox.LineBreak
	text += "urna turpis mauris maecenas ac diam, posuere morbi mi class tincidunt cum" + blox.LineBreak
	text += "suspendisse." + blox.LineBreak

	box := "+----------------------------+" + blox.LineBreak
	box += "|       A BOX WITH TEXT      |" + blox.LineBreak
	box += "+----------------------------+" + blox.LineBreak

	b.PutText(text).MoveX(23).DrawVerticalLine(0, 7, ':').
		Move(13, 3).PutText(box).FprintCanvas(os.Stdout).Move(0, 0)

}

Output

Lorem ipsum dolor sit a:et consectetur adipiscing elit torquent ante tortor dui
augue, dictumst convall:s eget tempor pharetra lectus magnis lacinia lacus eu
nostra. Sagittis dolor :attis laoreet justo mollis est varius etiam nisl, sit
eleifend null+----------------------------+llam suspendisse quis volutpat luctus
non a cursus |       A BOX WITH TEXT      |bus etiam odio lacus feugiat neque.
Primis pharet+----------------------------+ur duis taciti semper, tortor nisl
urna turpis mauris maec:nas ac diam, posuere morbi mi class tincidunt cum
suspendisse.           :

func (*Blox) Index

func (b *Blox) Index(x int, y int) int

Return index to write/read to/from on canvas by row/col representation (x is column, y is row).

func (*Blox) Join

func (b *Blox) Join(sep string) string

Join returns a string where each line is joined by sep, but does not end in sep like the String function (similar to strings.Join).

func (*Blox) Lines

func (b *Blox) Lines() [][]rune

Lines is the main function for producing printable output. Returns each row as a slice of rune slices. Each line is with or without trailing space depending on if TrimRightSpaces is true or false (SetTrimRightSpaces).

func (*Blox) Move

func (b *Blox) Move(x int, y int) *Blox

Move to a column/row position on the canvas where x is column and y is row, upp left hand corner is 0,0.

func (*Blox) MoveDown

func (b *Blox) MoveDown(n ...int) *Blox

func (*Blox) MoveLeft

func (b *Blox) MoveLeft(n ...int) *Blox

func (*Blox) MoveRight

func (b *Blox) MoveRight(n ...int) *Blox

func (*Blox) MoveUp

func (b *Blox) MoveUp(n ...int) *Blox

func (*Blox) MoveX

func (b *Blox) MoveX(x int) *Blox

func (*Blox) MoveY

func (b *Blox) MoveY(y int) *Blox

func (*Blox) PopPos

func (b *Blox) PopPos() *Blox

Restore (pop) last saved (pushed) cursor position from the cursor stack.

func (*Blox) PrintCanvas

func (b *Blox) PrintCanvas() *Blox
Example

package main

import (
	"github.com/sa6mwa/blox"
)

func main() {
	b := blox.New().SetColumnsAndRows(80, 24).SetTrimRightSpaces(true).SetTrimFinalEmptyLines(true)

	text := "Lorem ipsum dolor sit amet consectetur adipiscing elit torquent ante tortor dui" + blox.LineBreak
	text += "augue, dictumst convallis eget tempor pharetra lectus magnis lacinia lacus eu" + blox.LineBreak
	text += "nostra. Sagittis dolor mattis laoreet justo mollis est varius etiam nisl, sit" + blox.LineBreak
	text += "eleifend nullam magna aptent erat vitae. Nullam suspendisse quis volutpat luctus" + blox.LineBreak
	text += "non a cursus dui urna, facilisis ipsum dapibus etiam odio lacus feugiat neque." + blox.LineBreak
	text += "Primis pharetra cursus ultrices vel curabitur duis taciti semper, tortor nisl" + blox.LineBreak
	text += "urna turpis mauris maecenas ac diam, posuere morbi mi class tincidunt cum" + blox.LineBreak
	text += "suspendisse." + blox.LineBreak

	box := "+----------------------------+" + blox.LineBreak
	box += "|       A BOX WITH TEXT      |" + blox.LineBreak
	box += "+----------------------------+" + blox.LineBreak

	b.PutText(text).Move(13, 3).PutText(box)

	b.PrintCanvas()

}

Output

Lorem ipsum dolor sit amet consectetur adipiscing elit torquent ante tortor dui
augue, dictumst convallis eget tempor pharetra lectus magnis lacinia lacus eu
nostra. Sagittis dolor mattis laoreet justo mollis est varius etiam nisl, sit
eleifend null+----------------------------+llam suspendisse quis volutpat luctus
non a cursus |       A BOX WITH TEXT      |bus etiam odio lacus feugiat neque.
Primis pharet+----------------------------+ur duis taciti semper, tortor nisl
urna turpis mauris maecenas ac diam, posuere morbi mi class tincidunt cum
suspendisse.

func (*Blox) PushPos

func (b *Blox) PushPos() *Blox

Save (push) current cursor position to the cursor stack.

func (*Blox) PutChar

func (b *Blox) PutChar(r rune) *Blox

func (*Blox) PutLine

func (b *Blox) PutLine(runes []rune) *Blox

func (*Blox) PutLines

func (b *Blox) PutLines(lines ...string) *Blox

func (*Blox) PutText

func (b *Blox) PutText(text string) *Blox
Example

package main

import (
	"github.com/sa6mwa/blox"
)

func main() {
	b := blox.New().Trim().SetColumnsAndRows(80, 24)

	text := "ABCDE FGHIJ KLMNO" + blox.LineBreak
	text += "PQRST UVWXY ZABCD" + blox.LineBreak

	heading := "CRYPTO" + blox.LineBreak
	heading += "GROUPS"

	b.PutText(heading).DrawHorizontalLine(0, 6).DrawVerticalLine(0, 1, ':').
		PutChar('+').MoveDown().MoveX(0).PutText(text).
		Move(9, 0).PutText(text).PrintCanvas()

}

Output

CRYPTO : ABCDE FGHIJ KLMNO
GROUPS : PQRST UVWXY ZABCD
-------+
ABCDE FGHIJ KLMNO
PQRST UVWXY ZABCD

Example (Second)

package main

import (
	"fmt"

	"github.com/sa6mwa/blox"
)

func main() {
	b := blox.New().Trim().SetColumnsAndRows(80, 24)

	text := "ABCDE FGHIJ KLMNO" + blox.LineBreak
	text += "PQRST UVWXY ZABCD" + blox.LineBreak

	heading := "CRYPTO" + blox.LineBreak
	heading += "GROUPS"

	str := b.PutText(heading).DrawHorizontalLine(0, 6).DrawVerticalLine(0, 1, ':').
		PutChar('+').MoveDown().MoveX(0).PutText(text).
		Move(9, 0).PutText(text).Join(blox.LineBreak)

	strEndingInNewLine := b.String()

	fmt.Println(str)
	fmt.Println("--")
	fmt.Print(strEndingInNewLine)

}

Output

CRYPTO : ABCDE FGHIJ KLMNO
GROUPS : PQRST UVWXY ZABCD
-------+
ABCDE FGHIJ KLMNO
PQRST UVWXY ZABCD
--
CRYPTO : ABCDE FGHIJ KLMNO
GROUPS : PQRST UVWXY ZABCD
-------+
ABCDE FGHIJ KLMNO
PQRST UVWXY ZABCD

func (*Blox) PutTextRightAligned

func (b *Blox) PutTextRightAligned(text string) *Blox
Example

package main

import (
	"github.com/sa6mwa/blox"
)

func main() {
	b := blox.New().Trim().SetColumnsAndRows(80, 24)

	// blox.LineBreak is \n on Linux/Darwin and \r\n on Windows.

	text := "Lorem ipsum dolor sit amet consectetur adipiscing elit torquent ante tortor dui" + blox.LineBreak
	text += "augue, dictumst convallis eget tempor pharetra lectus magnis lacinia lacus eu" + blox.LineBreak
	text += "nostra. Sagittis dolor mattis laoreet justo mollis est varius etiam nisl, sit" + blox.LineBreak
	text += "eleifend nullam magna aptent erat vitae. Nullam suspendisse quis volutpat luctus" + blox.LineBreak
	text += "non a cursus dui urna, facilisis ipsum dapibus etiam odio lacus feugiat neque." + blox.LineBreak
	text += "Primis pharetra cursus ultrices vel curabitur duis taciti semper, tortor nisl" + blox.LineBreak
	text += "urna turpis mauris maecenas ac diam, posuere morbi mi class tincidunt cum" + blox.LineBreak
	text += "suspendisse." + blox.LineBreak

	box := "+----------------------------+" + blox.LineBreak
	box += "|       A BOX WITH TEXT      |" + blox.LineBreak
	box += "+----------------------------+" + blox.LineBreak

	b.PutText(text).MoveY(4).PutTextRightAligned(box).PrintCanvas()

}

Output

Lorem ipsum dolor sit amet consectetur adipiscing elit torquent ante tortor dui
augue, dictumst convallis eget tempor pharetra lectus magnis lacinia lacus eu
nostra. Sagittis dolor mattis laoreet justo mollis est varius etiam nisl, sit
eleifend nullam magna aptent erat vitae. Nullam suspendisse quis volutpat luctus
non a cursus dui urna, facilisis ipsum dapibus eti+----------------------------+
Primis pharetra cursus ultrices vel curabitur duis|       A BOX WITH TEXT      |
urna turpis mauris maecenas ac diam, posuere morbi+----------------------------+
suspendisse.

func (*Blox) ResizeCanvas

func (b *Blox) ResizeCanvas() *Blox

func (*Blox) Runes

func (b *Blox) Runes() []rune

Joins all rows with the new-line separator and returns a rune slice.

func (*Blox) SetColumns

func (b *Blox) SetColumns(columns int) *Blox

func (*Blox) SetColumnsAndRows

func (b *Blox) SetColumnsAndRows(columns int, rows int) *Blox

func (*Blox) SetLineSpacing

func (b *Blox) SetLineSpacing(n int) *Blox

func (*Blox) SetRows

func (b *Blox) SetRows(rows int) *Blox

func (*Blox) SetTrim

func (b *Blox) SetTrim(trim bool) *Blox

SetTrim allow you to enable/disable trimming of trailing spaces and empty lines with the same function.

func (*Blox) SetTrimFinalEmptyLines

func (b *Blox) SetTrimFinalEmptyLines(trim bool) *Blox

func (*Blox) SetTrimRightSpaces

func (b *Blox) SetTrimRightSpaces(trim bool) *Blox

func (*Blox) String

func (b *Blox) String() string

func (*Blox) Strings

func (b *Blox) Strings() (lines []string)

Returns each row in the canvas as a string slice.

func (*Blox) Trim

func (b *Blox) Trim() *Blox

Trim sets trimming of trailing spaces and trailing empty lines for String, Lines, Runes, PrintCanvas and similar output functions.

func (*Blox) Wipe

func (b *Blox) Wipe() *Blox

type CursorPosition

type CursorPosition struct {
    X         int // Column
    Y         int // Row
    OffCanvas bool
}

Generated by gomarkdoc

About

Go package to block-paste text in a character raster buffer. Text is pasted and positioned via X/Y coordinates on a canvas which can be printed as-is or delimited by line breaks at column-width.

Resources

License

Stars

Watchers

Forks

Packages

No packages published