textutils

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: MIT Imports: 7 Imported by: 0

README

textutils

go get gitlab.com/finkoper-lib/textutils

Example:

package main

import (
	"log"
	"gitlab.com/finkoper-lib/textutils"
)

func main() {
	plain, err := textutils.HtmlToPlain("<h1>Heading</h1>\n<p>Lorem ipsum &quot;dolor&#34;.</p>")
	if err != nil {
		log.Fatal(err)
	}
	println(plain)
}
func HtmlToPlain(inputHtml string) (string, error)

Example:

plain, err := textutils.HtmlToPlain("<h1>Heading</h1>\n<p>Lorem ipsum &quot;dolor&#34;.</p>")
if err != nil {
	log.Fatal(err)
}
println(plain)

prints:

Heading
Lorem ipsum "dolor".
func NewTable(columns ...string) Table

NewTable creates Table interface to form text table output.

  • Notice: Does not support multiline string values.
  • Notice: All data rows are stored in memory as already formatted strings, so beware adding tons of rows.

The columns are shortcut for Table.AddCol().

tbl := NewTable("Foo", "|Bar", ">Lorem ipsum")
tbl.FormatCol(2, "%0.2f")
tbl.AddRow("lol", true, 42.0)
tbl.AddRow("q", false, 234.0)
tbl.AddRow("xy", "-", 1234.56)
print(tbl.StringFull())
print(tbl.StringCompact())

outputs

┌─────┬───────┬─────────────┐
│ Foo │  Bar  │ Lorem ipsum │
╞═════╪═══════╪═════════════╡
│ lol │ true  │       42.00 │
├─────┼───────┼─────────────┤
│ q   │ false │      234.00 │
├─────┼───────┼─────────────┤
│ xy  │   -   │     1234.56 │
└─────┴───────┴─────────────┘
┌─────┬───────┬─────────────┐
│ Foo │  Bar  │ Lorem ipsum │
╞═════╪═══════╪═════════════╡
│ lol │ true  │       42.00 │
│ q   │ false │      234.00 │
│ xy  │   -   │     1234.56 │
└─────┴───────┴─────────────┘

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HtmlToPlain

func HtmlToPlain(inputHtml string) (string, error)

HtmlToPlain converts an HTML code to machine-readable plain text content

Primary goals are plain text preview, search indexing, etc.

plain, err := HtmlToPlain("<h1>Heading</h1>\n<p>Lorem ipsum &quot;dolor&#34;.</p>")
if err != nil {
	return err
}
println(plain)

prints:

Heading
Lorem ipsum "dolor".

Types

type Table added in v0.2.0

type Table interface {
	// AddCol adds new column to the end
	//
	// All existing rows will be kept unchanged, and so the new column will be
	// empty.
	//
	// A `spec` is a column display name optionally prefixed with
	// special any of the following control characters:
	//
	//  - `<` align left
	//  - `>` align right
	//  - `|` align center
	//  - `.` hidden by default
	AddCol(spec string)
	// AddRow adds data for further output
	//
	// All the rows added are stored in memory as already formatted output.
	// Data already added one cannot be changed later.
	AddRow(values ...any)
	// FormatCol sets format by fmt.Printf() to display the given column data
	//
	// Will have effect only for further calls to AddRow().
	//
	// Set empty string to remove format. By default, format is not defined, and
	// values will be printed with fmt.Sprint().
	//
	//	tbl := NewTable("Foo", "|Bar", ">Lorem ipsum")
	//	tbl.FormatCol(2, "%0.2f")
	FormatCol(i int, format string)
	// HideCol hides the given column from output
	HideCol(i int)
	// UnhideCol reverts hidden flag the given column
	UnhideCol(i int)
	// ColsCount returns count of defined columns
	ColsCount() int
	// ColsCountVisible returns count of visible columns
	ColsCountVisible() int
	// RowsCount gets number of all rows with headers
	//
	// It's about logical rows count rather than number of text lines with all
	// delimiters.
	RowsCount() int
	// RowsCountData gets number of data rows without header row.
	//
	// It's about logical rows count rather than number of text lines with all
	// delimiters.
	RowsCountData() int
	// StringFull returns text table output with single-line delimiters between
	// all data rows and double-line delimiter for header rows
	StringFull() string
	// StringCompact returns text table output with row delimiter only for
	// header row
	StringCompact() string
}

func NewTable added in v0.2.0

func NewTable(columns ...string) Table

NewTable creates Table interface to form text table output

The `columns` are shortcut for Table.AddCol().

tbl := NewTable("Foo", "|Bar", ">Lorem ipsum")
tbl.FormatCol(2, "%0.2f")
tbl.AddRow("lol", true, 42.0)
tbl.AddRow("q", false, 234.0)
tbl.AddRow("xy", "-", 1234.56)
print(tbl.StringFull())
print(tbl.StringCompact())

outputs

┌─────┬───────┬─────────────┐
│ Foo │  Bar  │ Lorem ipsum │
╞═════╪═══════╪═════════════╡
│ lol │ true  │       42.00 │
├─────┼───────┼─────────────┤
│ q   │ false │      234.00 │
├─────┼───────┼─────────────┤
│ xy  │   -   │     1234.56 │
└─────┴───────┴─────────────┘
┌─────┬───────┬─────────────┐
│ Foo │  Bar  │ Lorem ipsum │
╞═════╪═══════╪═════════════╡
│ lol │ true  │       42.00 │
│ q   │ false │      234.00 │
│ xy  │   -   │     1234.56 │
└─────┴───────┴─────────────┘

Jump to

Keyboard shortcuts

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