termlink

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2023 License: MIT Imports: 3 Imported by: 27

README

Clickable links in the terminal for Go

Release Go Reference License Go Report Card

termlink_demo_nyoom *

Termlink is a Go package that allows you to create fully customizable clickable links in the terminal. It is the Go version of Sindre Sorhus' popular terminal-link library.

It includes multiple features including dynamic and fully customizable colored links in terminal, along with smart hyperlink support detection.

🛠️ Install

Using go get:

go get github.com/savioxavier/termlink

🔗 Usage

  • Basic regular link:
import (
	"fmt"

	"github.com/savioxavier/termlink"
)

func main() {
	fmt.Println(termlink.Link("Example", "https://example.com"))
}
  • Customizable colored link:
import (
	"fmt"

	"github.com/savioxavier/termlink"
)

func main() {
	fmt.Println(termlink.ColorLink("Example", "https://example.com", "italic green"))
}
  • Check if your current terminal supports hyperlinks:
import (
	"fmt"

	"github.com/savioxavier/termlink"
)

func main() {
	fmt.Println(termlink.SupportsHyperlinks())
	// prints either true or false, depending on your terminal
}
  • Since termlink.Link returns a string, you can use it with your favorite text color formatting libraries such as fatih/color, mgutz/ansi, etc. Alternatively, you can use termlink.ColorLink as well.
import (
	"fmt"

	"github.com/fatih/color"
	"github.com/savioxavier/termlink"
)

func main() {
	// With fatih/color package
	color.Cyan(termlink.Link("Example link using the fatih/color package", "https://example.com"))
}

Note For unsupported terminals, the link will be printed in parentheses after the text (see below image)

image


🍵 Examples

More examples can be found in the examples/ directory.

For a quick demo, execute the following commands in your terminal:

git clone https://github.com/savioxavier/termlink.git
cd termlink/
go get github.com/savioxavier/termlink
go run examples/start.go

🔮 Features

  • termlink.Link(text, url, [shouldForce])

    • Creates a regular, clickable link in the terminal
    • For unsupported terminals, the link will be printed in parentheses after the text: Example Link (https://example.com).
    • The shouldForce is an optional boolean parameter which allows you to force the above unsupported terminal hyperlinks format text (url) to be printed, even in supported terminals
  • termlink.ColorLink(text, url, color, [shouldForce])

    • Creates a clickable link in the terminal with custom color formatting
    • Examples of color options include:
      • Foreground only: green, red, blue, etc.
      • Background only: bgGreen, bgRed, bgBlue, etc.
      • Foreground and background: green bgRed, bgBlue red, etc.
      • With formatting: green bold, red bgGreen italic, italic blue bgGreen, etc.
    • The shouldForce is an optional boolean parameter which allows you to force the above unsupported terminal hyperlinks format text (url) to be printed, even in supported terminals

shouldForce can be used in the following manner:

termlink.Link(text, url, true)
termlink.ColorLink(text, url, color, true)

You don't always need to specify this argument. By default, this parameter is false

  • termlink.SupportsHyperlinks():

    • Returns true if the terminal supports hyperlinks, false otherwise.

🧪 Tests

You can run unit tests locally by running the following command:

go test -v

Tests can be found in termlink_test.go


❤️ Support

You can support further development of this project by giving it a 🌟 and help me make even better stuff in the future by buying me a ☕


Also, if you liked this repo, consider checking out my other projects, that would be real cool!


💫 Attributions and special thanks

  • terminal-link - Sindre Sorhus' original package for providing inspiration for this package.
  • supports-hyperlinks - Zkat's package for additional hyperlink handling support.

* The paperclip icon shown in the demo at the top of this README isn't included when you create the link, it's purely for decorative purposes only.

Documentation

Overview

Package termlink implements a set of functions to create customizable, clickable hyperlinks in the terminal.

Index

Constants

This section is empty.

Variables

View Source
var EnvironmentVariables = []string{
	"DOMTERM",
	"WT_SESSION",
	"KONSOLE_VERSION",
}

EnvironmentVariables represent the set of standalone environment variables ie, those which do not require any special handling or version checking

Functions

func ColorLink(text string, url string, color string, shouldForce ...bool) string

ColorLink returns a colored clickable link, which can be used in the terminal

The function takes three required parameters: text, url and color and one optional parameter: shouldForce

The text parameter is the text to be displayed. The url parameter is the URL to be opened when the link is clicked. The color parameter is the color of the link. The shouldForce is an optional parameter indicates whether to force the non-hyperlink supported behavior (i.e., text (url))

The function returns the clickable link.

func Link(text string, url string, shouldForce ...bool) string

Link returns a clickable link, which can be used in the terminal

The function takes two required parameters: text and url and one optional parameter: shouldForce

The text parameter is the text to be displayed. The url parameter is the URL to be opened when the link is clicked. The shouldForce is an optional parameter indicates whether to force the non-hyperlink supported behavior (i.e., text (url))

The function returns the clickable link.

func SupportsHyperlinks() bool

SupportsHyperlinks returns true if the terminal supports hyperlinks.

The function returns true if the terminal supports hyperlinks, false otherwise.

Types

type Version added in v1.3.0

type Version struct {
	// contains filtered or unexported fields
}

Version struct represents a semver version (usually, with some exceptions) with major, minor, and patch segments

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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