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)
}
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
- Constants
- Variables
- func CutLineShort(line string, maxLen int, addThreeDots bool) string
- func CutLinesShort(text string, maxLen int, trimTrailingSpace bool) string
- func LineCount(text string) int
- func MaximumLineLength(text string) int
- func RowAndColumnCount(text string) (int, int)
- type Blox
- func New() *Blox
- func (b *Blox) CurrentIndex() int
- func (b *Blox) DrawHorizontalLine(fromX int, toX int, char ...rune) *Blox
- func (b *Blox) DrawSeparator(char ...rune) *Blox
- func (b *Blox) DrawSplit(char ...rune) *Blox
- func (b *Blox) DrawVerticalLine(fromY int, toY int, char ...rune) *Blox
- func (b *Blox) FprintCanvas(o *os.File) *Blox
- func (b *Blox) Index(x int, y int) int
- func (b *Blox) Join(sep string) string
- func (b *Blox) Lines() [][]rune
- func (b *Blox) Move(x int, y int) *Blox
- func (b *Blox) MoveDown(n ...int) *Blox
- func (b *Blox) MoveLeft(n ...int) *Blox
- func (b *Blox) MoveRight(n ...int) *Blox
- func (b *Blox) MoveUp(n ...int) *Blox
- func (b *Blox) MoveX(x int) *Blox
- func (b *Blox) MoveY(y int) *Blox
- func (b *Blox) PopPos() *Blox
- func (b *Blox) PrintCanvas() *Blox
- func (b *Blox) PushPos() *Blox
- func (b *Blox) PutChar(r rune) *Blox
- func (b *Blox) PutLine(runes []rune) *Blox
- func (b *Blox) PutLines(lines ...string) *Blox
- func (b *Blox) PutText(text string) *Blox
- func (b *Blox) PutTextRightAligned(text string) *Blox
- func (b *Blox) ResizeCanvas() *Blox
- func (b *Blox) Runes() []rune
- func (b *Blox) SetColumns(columns int) *Blox
- func (b *Blox) SetColumnsAndRows(columns int, rows int) *Blox
- func (b *Blox) SetLineSpacing(n int) *Blox
- func (b *Blox) SetRows(rows int) *Blox
- func (b *Blox) SetTrim(trim bool) *Blox
- func (b *Blox) SetTrimFinalEmptyLines(trim bool) *Blox
- func (b *Blox) SetTrimRightSpaces(trim bool) *Blox
- func (b *Blox) String() string
- func (b *Blox) Strings() (lines []string)
- func (b *Blox) Trim() *Blox
- func (b *Blox) Wipe() *Blox
- type CursorPosition
const LineBreak string = "\n"
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(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)
}
Hello world…
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))
}
Lorem ipsum d
augue, dictum
nostra. Sagit
eleifend null
non a cursus
Primis pharet
urna turpis m
suspendisse.
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(text string) int
MaximumLineLength returns number of runes in the longest line (can be used to setup canvas Columns to fit text).
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 struct {
Columns int // X
Rows int // Y
Cursor CursorPosition
CursorStack []CursorPosition
LineSpacing int
TrimRightSpaces bool
TrimFinalEmptyLines bool
Canvas []rune
}
func New() *Blox
func (b *Blox) CurrentIndex() int
Return current index to write/read to/from on canvas based on current cursor row/column position.
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 (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()
}
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 (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)
}
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 (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 (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)
}
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 (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 (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 (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 (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 (b *Blox) MoveDown(n ...int) *Blox
func (b *Blox) MoveLeft(n ...int) *Blox
func (b *Blox) MoveRight(n ...int) *Blox
func (b *Blox) MoveUp(n ...int) *Blox
func (b *Blox) MoveX(x int) *Blox
func (b *Blox) MoveY(y int) *Blox
func (b *Blox) PopPos() *Blox
Restore (pop) last saved (pushed) cursor position from the cursor stack.
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()
}
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 (b *Blox) PushPos() *Blox
Save (push) current cursor position to the cursor stack.
func (b *Blox) PutChar(r rune) *Blox
func (b *Blox) PutLine(runes []rune) *Blox
func (b *Blox) PutLines(lines ...string) *Blox
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()
}
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)
}
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 (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()
}
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 (b *Blox) ResizeCanvas() *Blox
func (b *Blox) Runes() []rune
Joins all rows with the new-line separator and returns a rune slice.
func (b *Blox) SetColumns(columns int) *Blox
func (b *Blox) SetColumnsAndRows(columns int, rows int) *Blox
func (b *Blox) SetLineSpacing(n int) *Blox
func (b *Blox) SetRows(rows int) *Blox
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 (b *Blox) SetTrimFinalEmptyLines(trim bool) *Blox
func (b *Blox) SetTrimRightSpaces(trim bool) *Blox
func (b *Blox) String() string
func (b *Blox) Strings() (lines []string)
Returns each row in the canvas as a string slice.
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 (b *Blox) Wipe() *Blox
type CursorPosition struct {
X int // Column
Y int // Row
OffCanvas bool
}
Generated by gomarkdoc