escapes

package module
v0.0.0-...-3a5d8be Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: MIT Imports: 6 Imported by: 0

README

escapes

Package escapes has a handful of useful functions for when you're wanting to send formatting escape sequences to the terminal while tracking the width of your output for alignment purposes.

The escape sequences always restore things back to 'normal' when done, which could be considered suboptimal when adding styles but keeps things sane.

This does not use terminfo, and just relies on ECMA-48 and related ANSI standards; see https://en.wikipedia.org/wiki/ANSI_escape_code for too much and yet too little detail.

Currently implemented:

  • Hyperlinks
  • 24-bit colour
  • Reverse video
  • Bold
  • Italic

Documentation

Overview

Package escapes has a handful of useful functions for when you're wanting to send formatting escape sequences to the terminal while tracking the width of your output for alignment purposes.

The escape sequences always restore things back to 'normal' when done, which could be considered suboptimal when adding styles but keeps things sane.

This does not use terminfo, and just relies on ECMA-48 and related ANSI standards; see https://en.wikipedia.org/wiki/ANSI_escape_code for too much and yet too little detail.

Index

Constants

View Source
const (
	BeginBold      = CSI + "1m"
	BeginDim       = CSI + "2m"
	BeginItalic    = CSI + "3m"
	BeginUnderline = CSI + "4m"
	BeginReverse   = CSI + "7m"
	SGR0           = CSI + "0m"
	ClrEOL         = CSI + "K"
)

Several constants that might be handy to have (and are used by us)

View Source
const CSI = "\x1b["

CSI or Control Sequence Introducer is the start of most things we deal with here

Variables

This section is empty.

Functions

func Bold

func Bold(s string) width.StringWidther

Bold returns a string that turns on bold mode for the text given.

func Colorized

func Colorized(s string, fg, bg *color.Color) width.StringWidther

Colorized returns a string with escape sequences that shows the text t with foreground and backgroun colours as given.

It assumes 24-bit colour support.

func Dim

func Dim(s string) width.StringWidther

Dim returns a string that turns on dim mode for the text given.

func Hyperlink(u, t string) width.StringWidther

Hyperlink returns an escape sequence that links to the url u with the text t.

func Italic

func Italic(s string) width.StringWidther

Italic returns a string that turns on italic mode for the text given.

NOTE many terminals don't implement this (or have support for it

turned off by default).

func Reverse

func Reverse(s string) width.StringWidther

Reverse returns a string that turns on reverse video mode for the text given.

func ReverseLine

func ReverseLine(s string) string

ReverseLine returns a string that uses reverse video and clears to the end of the line.

This includes a newline to avoid confusion (otherwise the cursor maybe be in an unexpected place).

Note that the width of the string is terminal-dependent and thus not knowable from here.

func Underline

func Underline(s string) width.StringWidther

Underline returns a string that turns on underline mode for the text given.

Types

This section is empty.

Jump to

Keyboard shortcuts

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