link

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2022 License: MIT Imports: 4 Imported by: 0

README

Create clickable hyperlinks in the terminal

Result

Install

$ go get -u github.com/luisnquin/go-terminal-hyperlink

Usage

package main

import (
	"os"

	"github.com/fatih/color"
	link "github.com/luisnquin/go-terminal-hyperlink"
)

var (
	url  = "https://github.com/luisnquin/go-terminal-hyperlink"
	text = "Go terminal hyperlink"
)

func main() {
	// flag.Set("hyperlinks", "true")

	link.Stderr.Println(url, text)

	println(link.Stdout.Sprintf(url, text+" (%s)", "this comes with the text"))

	link.Stdout.SetColors(color.FgHiGreen).Println(url, text)

	link.New(os.Stdout).Println(url, text)

	link.New(os.Stderr).SetColors(color.BgWhite, color.FgHiMagenta).Println(url, text)

	new(link.Hyperlink).Println(url, text) // Default -> os.Stdout
}

Why?

  • Some terminals are not fully compatible, sometimes with versions impossible to infer despite the existence of future compatible ones. Check this for more

dasdqanoduqbwd.png

  • Sometimes the incompatibility could come from the mere fact that the terminal interprets it as invisible characters, offering results like:

issue-2.png

To prevent that issues, on incompatible terminals, the result is replaced by:

but-1.png

Obviusly, this without force the behavior as the first image of the section.

Still, if you just want to force this without setting a new flag, put it in your code before to use the package

_ = flag.Set("hyperlinks", "true")

API

It's about assimilating to projects created in JavaScript like:

So this guide(supported terminals) might not be excluded

Variables

  • Stdout
package main

import link "github.com/luisnquin/go-terminal-hyperlink"

func main() {
    moreText := "Yes, more text"
    println(link.Stdout.Sprintf(url, text, moreText))
}
  • Stderr
package main

import link "github.com/luisnquin/go-terminal-hyperlink"

func main() {
    link.Stderr.Println(url, text)
}

Functions

  • New
package main

import (
    "os"

    link "github.com/luisnquin/go-terminal-hyperlink"
)

func main() {
    println(link.New(os.Stdout).Sprintln(url, text))
}

Methods

  • SetColors
package main

import (
    "os"

    "github.com/fatih/color"
    link "github.com/luisnquin/go-terminal-hyperlink"
)

func main() {
    link.Stdout.SetColors(color.FgHiGreen, color.ReverseVideo).Println(url, text)
}

Structs

  • Hyperlink

    Fields
    • Colors []color.Attribute
    • Stream *os.File
    Methods
    • SetColors(colors ...color.Attribute) Hyperlinker
    • Sprint(url, text string) string
    • Sprintln(url, text string) string
    • Sprintf(url, text string, a ...interface{}) string
    • Print(url, text string)
    • Println(url, text string)
    • Printf(url, text string, a ...interface{})

    Note: By default Stream is os.Stdout, this field is required if you want to create a new instance

Interfaces

  • Hyperlinker
    • Sprint(url, text string) string
    • Sprintln(url, text string) string
    • Sprintf(url, text string, a ...interface{}) string
    • Print(url, text string)
    • Println(url, text string)
    • Printf(url, text string, a ...interface{})

A cat

Result

License

MIT © Luis Quiñones Requelme

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Stdout = Hyperlink{Stream: os.Stdout}
	Stderr = Hyperlink{Stream: os.Stderr}
)

Default option

Functions

This section is empty.

Types

type Hyperlink struct {
	Colors []color.Attribute
	Stream *os.File `default:"os.Stdout"`
}

Per default the stream uses stdout, less syntax, of course. Avoid using it, use New(... *os.File) function

func New

func New(stream *os.File) *Hyperlink

Create a new instance of Hyperlink without create a new variable, just receives an *os.File

func (*Hyperlink) Print

func (h *Hyperlink) Print(url, text string)

func (*Hyperlink) Printf

func (h *Hyperlink) Printf(url, text string, a ...interface{})

func (*Hyperlink) Println

func (h *Hyperlink) Println(url, text string)

func (*Hyperlink) SetColors

func (h *Hyperlink) SetColors(colors ...color.Attribute) Hyperlinker

Set any number of color attributes, it returns a Hyperlinker that allows you to have a family of fmt methods

func (*Hyperlink) Sprint

func (h *Hyperlink) Sprint(url, text string) string

Returns a clickable string, works like HTML <a> tags. All of this, without alt attribute, obviusly

func (*Hyperlink) Sprintf

func (h *Hyperlink) Sprintf(url, text string, a ...interface{}) string

Returns the same clickable string, but also allows you to format the text, amazing or not?

func (*Hyperlink) Sprintln

func (h *Hyperlink) Sprintln(url, text string) string

type Hyperlinker

type Hyperlinker interface {
	Sprint(url, text string) string
	Sprintln(url, text string) string
	Sprintf(url, text string, a ...interface{}) string
	Print(url, text string)
	Println(url, text string)
	Printf(url, text string, a ...interface{})
}

Like fmt family but evaluates and transforms the required inputs to a hyperlink

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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