highlight

package module
v0.0.0-...-acb0e61 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: MIT Imports: 1 Imported by: 0

README

go-highlight

PkgGoDev

go-highlight highlights substrings of text with customizable prefixes and suffixes in Go.

Examples

import (
        "fmt"

        "github.com/dieperdev/go-highlight"
)

fmt.Println(highlight.Highlight("Hello World!", "World", 1, "GitHub ", "", false))
// Output: Hello GitHub World!



// Colors can't be shown on the README.
// You will have to run an example locally like the one
// under `highlight_example_test.go`

fmt.Println(highlight.HighlightColor("Red, Yellow, and Blue.", "Yellow", 1, "Orange", true))
// Output: Red, OrangeYellowOrange, and Blue.



fmt.Println(highlight.HighlightSameReplacements("Go is a programming language.", "Go", 1, "*", true))
// Output: *Go* is a programming language.
}

Using highlighted text

If you want to print the highlighted text to the console, you need a library such as k0kubun/go-ansi to do it. go-ansi will correctly print the ANSI color escape codes (it also has Windows support). You should avoid using the %q verb (quoted string) when fmt.Sprintf'ing or manipulating the highlighted text as it will escape the highlights and not print correctly.

License

The MIT License

Documentation

Index

Examples

Constants

View Source
const (
	FgBlack   string = "\x1b[30m"
	FgRed     string = "\x1b[31m"
	FgGreen   string = "\x1b[32m"
	FgYellow  string = "\x1b[33m"
	FgBlue    string = "\x1b[34m"
	FgMagenta string = "\x1b[35m"
	FgCyan    string = "\x1b[36m"
	FgWhite   string = "\x1b[37m"

	BgBlack   string = "\x1b[40m"
	BgRed     string = "\x1b[41m"
	BgGreen   string = "\x1b[42m"
	BgYellow  string = "\x1b[43m"
	BgBlue    string = "\x1b[44m"
	BgMagenta string = "\x1b[45m"
	BgCyan    string = "\x1b[46m"
	BgWhite   string = "\x1b[47m"
)

This is a small list of colors sourced from https://talyian.github.io/ansicolors/

As stated by the webpage, "Different consoles render these codes at different values"

You may pass your own strings as replacements to the functions if you want to have more color options.

Variables

This section is empty.

Functions

func Highlight

func Highlight(original string, substring string, limit int, before string, after string, caseSensitive bool) string

Highlight prefixes up to `limit` occurrences of `substring` with `before` and suffixes up to `limit` occurrences of `substring` with `after`.

Highlight can optionally be case-sensitive.

Example
fmt.Println(highlight.Highlight("Hello World!", "World", 1, "GitHub ", "", false))
Output:
Hello GitHub World!

func HighlightColor

func HighlightColor(original string, substring string, limit int, color string, caseSensitive bool) string

HighlightColor has the same functionality as Highlight, but prefixes and suffixes up to `limit` occurences of `substring` with the same text.

The `color` passed to HighlightColor does not have to strictly be a color. You may want to use `HighlightSameReplacements` to make usage of `go-highlight` more clear in your program if `color` is not a color.

Example
// Colors can't be printed on the online Go documentation.
// You will have to run an example locally.
//
// You can copy this code example below for a quick test:
/*
	package main

	import (
		"fmt"

		"github.com/dieperdev/go-highlight"
	)

	func main() {
		text := "Red, Yellow, and Blue."
		highlighted := highlight.HighlightColor(text, "Yellow", 1, highlight.BgYellow, true)

		fmt.Println(highlighted)
	}
*/

fmt.Println(highlight.HighlightColor("Red, Yellow, and Blue.", "Yellow", 1, "Orange", true))
Output:
Red, OrangeYellowOrange, and Blue.

func HighlightSameReplacements

func HighlightSameReplacements(original string, substring string, limit int, replacement string, caseSensitive bool) string

HighlightSameReplacements has the same functionality as HighlightColor, but with different function name. It is intended to make usage of `go-highlight` more clear.

Example
fmt.Println(highlight.HighlightSameReplacements("Go is a programming language.", "Go", 1, "*", true))
Output:
*Go* is a programming language.

Types

This section is empty.

Jump to

Keyboard shortcuts

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