Skip to content

Commit 3afa4f8

Browse files
authored
Merge pull request #8 from savioxavier/rewrite
[major version + breaking changes included] termlink v2
2 parents b30c31e + 710c88f commit 3afa4f8

File tree

6 files changed

+284
-152
lines changed

6 files changed

+284
-152
lines changed

README.md

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
**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](https://github.com/sindresorhus/terminal-link/) library.**
1515

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

1818
## 🛠️ Install
1919

@@ -55,7 +55,22 @@ func main() {
5555
}
5656
```
5757

58-
- You can also use this package in combination with another popular Go package [fatih/color](https://github.com/fatih/color)
58+
- Check if your current terminal supports hyperlinks:
59+
60+
```go
61+
import (
62+
"fmt"
63+
64+
"github.com/savioxavier/termlink"
65+
)
66+
67+
func main() {
68+
fmt.Println(termlink.SupportsHyperlinks())
69+
// prints either true or false, depending on your terminal
70+
}
71+
```
72+
73+
- Since `termlink.Link` returns a string, you can use it with your favorite text color formatting libraries such as [fatih/color](https://github.com/fatih/color), [mgutz/ansi](https://github.com/mgutz/ansi), etc. Alternatively, you can use `termlink.ColorLink` as well.
5974

6075
```go
6176
import (
@@ -67,11 +82,12 @@ import (
6782

6883
func main() {
6984
// With fatih/color package
70-
color.Cyan(termlink.Link("Example link using the colors package", "https://example.com"))
85+
color.Cyan(termlink.Link("Example link using the fatih/color package", "https://example.com"))
7186
}
7287
```
7388

74-
> #### Note: For unsupported terminals, the link will be printed in parentheses after the text (see below image)
89+
> **Note**
90+
> **For unsupported terminals, the link will be printed in parentheses after the text (see below image)**
7591
>
7692
> ![image](https://user-images.githubusercontent.com/38729705/163216009-abb81d39-aff0-4fb5-8c5f-da36e241b395.png)
7793
@@ -81,6 +97,15 @@ func main() {
8197

8298
More examples can be found in the [`examples/`](examples/) directory.
8399

100+
For a quick demo, execute the following commands in your terminal:
101+
102+
```bash
103+
git clone https://github.com/savioxavier/termlink.git
104+
cd termlink/
105+
go get github.com/savioxavier/termlink
106+
go run examples/start.go
107+
```
108+
84109
---
85110

86111
## 🔮 Features
@@ -101,6 +126,15 @@ More examples can be found in the [`examples/`](examples/) directory.
101126
- With formatting: `green bold`, `red bgGreen italic`, `italic blue bgGreen`, etc.
102127
- 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
103128

129+
> `shouldForce` can be used in the following manner:
130+
>
131+
> ```go
132+
> termlink.Link(text, url, true)
133+
> termlink.ColorLink(text, url, color, true)
134+
> ```
135+
>
136+
> You don't always need to specify this argument. By default, this parameter is `false`
137+
104138
- **`termlink.SupportsHyperlinks()`**:
105139
106140
- Returns `true` if the terminal supports hyperlinks, `false` otherwise.
@@ -109,7 +143,7 @@ More examples can be found in the [`examples/`](examples/) directory.
109143
110144
## 🧪 Tests
111145
112-
You can run unit tests _locally_ by running the following command
146+
You can run unit tests _locally_ by running the following command:
113147
114148
```bash
115149
go test -v
@@ -136,6 +170,6 @@ You can support further development of this project by **giving it a 🌟** and
136170
## 💫 Attributions and special thanks
137171

138172
- [terminal-link](https://github.com/sindresorhus/terminal-link) - Sindre Sorhus' original package for providing inspiration for this package.
139-
- [go-supportscolor](https://github.com/jwalton/go-supportscolor) - A package for detecting terminal color support.
173+
- [supports-hyperlinks](https://github.com/zkat/supports-hyperlinks) - Zkat's package for additional hyperlink handling support.
140174

141175
<sub><sup>* 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.</sup></sub>

examples/start.go

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,59 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/fatih/color"
76
"github.com/savioxavier/termlink"
87
)
98

10-
func main() {
9+
func printSupportsHyperlinks() {
10+
fmt.Printf("Does this terminal support hyperlinks: %t\n", termlink.SupportsHyperlinks())
11+
}
12+
13+
func printExample() {
1114
name := "John"
1215
age := 21
1316

14-
// With regular termlink package
1517
fmt.Printf(
1618
`
17-
Hello I'm %s.
18-
I'm %d years old.
19-
Here's Twitter: %s.
20-
And here's Twitter's Twitter: %s.
21-
Anyways, see ya!
19+
Here's an example to get you started:
20+
21+
Hello I'm %s.
22+
I'm %d years old.
23+
Here's Twitter: %s.
24+
And here's Twitter's Twitter: %s.
2225
`,
2326
name,
2427
age,
2528
termlink.Link("Twitter", "https://twitter.com"),
26-
termlink.ColorLink("@twitter", "https://twitter.com/twitter", "italic green"),
29+
termlink.ColorLink("@twitter", "https://twitter.com/twitter", "italic blue"),
30+
)
31+
}
32+
33+
func printHelpLinks() {
34+
fmt.Printf(
35+
`
36+
Also, here are some other links to help you out:
37+
38+
🐹 %s
39+
💻 %s
40+
41+
🔗 %s
42+
💖 %s
43+
`,
44+
termlink.Link("Official Go Website", "https://go.dev"),
45+
termlink.ColorLink("A Tour of Go", "https://go.dev/tour", "cyan"),
46+
termlink.ColorLink("termlink's source code", "https://github.com/savioxavier/termlink", "italic magenta"),
47+
termlink.ColorLink("termlink's donate page", "https://www.buymeacoffee.com/savioxavier", "italic red"),
2748
)
49+
}
50+
51+
func printGoodbye() {
52+
fmt.Println()
53+
fmt.Println("👋 Goodbye!")
54+
}
2855

29-
// With fatih/color package
30-
color.Cyan(termlink.Link("Example link using the colors package!", "https://example.com"))
56+
func main() {
57+
printSupportsHyperlinks()
58+
printExample()
59+
printHelpLinks()
60+
printGoodbye()
3161
}

go.mod

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,10 @@ module github.com/savioxavier/termlink
22

33
go 1.17
44

5-
require (
6-
github.com/fatih/color v1.13.0
7-
github.com/jwalton/go-supportscolor v1.1.0
8-
)
5+
require github.com/stretchr/testify v1.7.1
96

107
require (
118
github.com/davecgh/go-spew v1.1.0 // indirect
12-
github.com/mattn/go-colorable v0.1.9 // indirect
13-
github.com/mattn/go-isatty v0.0.14 // indirect
149
github.com/pmezard/go-difflib v1.0.0 // indirect
15-
github.com/stretchr/testify v1.7.1 // indirect
16-
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
17-
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
1810
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
1911
)

go.sum

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
4-
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
5-
github.com/jwalton/go-supportscolor v1.1.0 h1:HsXFJdMPjRUAx8cIW6g30hVSFYaxh9yRQwEWgkAR7lQ=
6-
github.com/jwalton/go-supportscolor v1.1.0/go.mod h1:hFVUAZV2cWg+WFFC4v8pT2X/S2qUUBYMioBD9AINXGs=
7-
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
8-
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
9-
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
10-
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
11-
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
123
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
134
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
145
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
156
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
167
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
17-
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
18-
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
19-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
20-
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
21-
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
22-
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
23-
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
24-
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
259
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2610
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
2711
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)