Skip to content

Commit c2bef28

Browse files
authored
Merge pull request #6 from savioxavier/force-link
feat: add `shouldForce` parameter to functions
2 parents 0796277 + 634621d commit c2bef28

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- markdownlint-disable MD010 MD033 -->
1+
<!-- markdownlint-disable MD010 MD033 MD001 -->
22

33
# termlink
44

@@ -67,9 +67,9 @@ func main() {
6767
```
6868

6969
> #### Note: For unsupported terminals, the link will be printed in parentheses after the text (see below image)
70+
>
7071
> ![image](https://user-images.githubusercontent.com/38729705/163216009-abb81d39-aff0-4fb5-8c5f-da36e241b395.png)
7172
72-
7373
---
7474

7575
## 🍵 Examples
@@ -80,19 +80,21 @@ More examples can be found in the [`examples/`](examples/) directory.
8080

8181
## 🔮 Features
8282

83-
- **`termlink.Link(text, url)`**
83+
- **`termlink.Link(text, url, [shouldForce])`**
8484

8585
- Creates a regular, clickable link in the terminal
8686
- For unsupported terminals, the link will be printed in parentheses after the text: `Example Link (https://example.com)`.
87+
- 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
8788

88-
- **`termlink.ColorLink(text, url, color)`**
89+
- **`termlink.ColorLink(text, url, color, [shouldForce])`**
8990

9091
- Creates a clickable link in the terminal with custom color formatting
9192
- Examples of color options include:
9293
- Foreground only: `green`, `red`, `blue`, etc.
9394
- Background only: `bgGreen`, `bgRed`, `bgBlue`, etc.
9495
- Foreground and background: `green bgRed`, `bgBlue red`, etc.
9596
- With formatting: `green bold`, `red bgGreen italic`, `italic blue bgGreen`, etc.
97+
- 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
9698

9799
- **`termlink.SupportsHyperlinks()`**:
98100

termlink.go

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,41 +139,64 @@ func supportsColor() bool {
139139

140140
// Function Link creates a clickable link in the terminal's stdout.
141141
//
142-
// The function takes two parameters: text and url.
142+
// The function takes two required parameters: text and url
143+
// and one optional parameter: shouldForce
143144
//
144145
// The text parameter is the text to be displayed.
145146
// The url parameter is the URL to be opened when the link is clicked.
147+
// The shouldForce parameter indicates whether to force the non-hyperlink supported behavior (i.e., text (url))
146148
//
147149
// The function returns the clickable link.
148-
func Link(text string, url string) string {
149-
if supportsHyperlinks() {
150-
return "\x1b]8;;" + url + "\x07" + text + "\x1b]8;;\x07" + parseColor("reset")
150+
func Link(text string, url string, shouldForce ...bool) string {
151+
shouldForceDefault := false
152+
153+
if len(shouldForce) > 0 {
154+
shouldForceDefault = shouldForce[0]
155+
}
156+
157+
if shouldForceDefault {
158+
return text + " (" + url + ")" + parseColor("reset")
151159
} else {
152-
return text + " (\u200B" + url + ")" + parseColor("reset")
160+
if supportsHyperlinks() {
161+
return "\x1b]8;;" + url + "\x07" + text + "\x1b]8;;\x07" + parseColor("reset")
162+
}
163+
return text + " (" + url + ")" + parseColor("reset")
153164
}
154165
}
155166

156167
// Function LinkColor creates a colored clickable link in the terminal's stdout.
157168
//
158-
// The function takes three parameters: text, url and color.
169+
// The function takes three required parameters: text, url and color
170+
// and one optional parameter: shouldForce
159171
//
160172
// The text parameter is the text to be displayed.
161173
// The url parameter is the URL to be opened when the link is clicked.
162174
// The color parameter is the color of the link.
175+
// The shouldForce parameter indicates whether to force the non-hyperlink supported behavior (i.e., text (url))
163176
//
164177
// The function returns the clickable link.
165-
func ColorLink(text string, url string, color string) string {
178+
func ColorLink(text string, url string, color string, shouldForce ...bool) string {
166179
var textColor string
167180

168181
if supportsColor() {
169182
textColor = parseColor(color)
170183
} else {
171184
textColor = ""
172185
}
173-
if supportsHyperlinks() {
174-
return "\x1b]8;;" + url + "\x07" + textColor + text + "\x1b]8;;\x07" + parseColor("reset")
186+
187+
shouldForceDefault := false
188+
189+
if len(shouldForce) > 0 {
190+
shouldForceDefault = shouldForce[0]
191+
}
192+
193+
if shouldForceDefault {
194+
return textColor + text + " (" + url + ")" + parseColor("reset")
175195
} else {
176-
return textColor + text + " (\u200B" + url + ")" + parseColor("reset")
196+
if supportsHyperlinks() {
197+
return "\x1b]8;;" + url + "\x07" + textColor + text + "\x1b]8;;\x07" + parseColor("reset")
198+
}
199+
return textColor + text + " (" + url + ")" + parseColor("reset")
177200
}
178201
}
179202

0 commit comments

Comments
 (0)