Skip to content

Commit 866c776

Browse files
committed
refactor(terminal): remove functions superseded by IOStreams
Remove IsTTY, IsColorDisabled, and Width from the terminal package — these are now per-stream methods on IOStreams (IsStdoutTTY, ColorEnabled, TerminalWidth). OpenBrowser uses isatty directly instead of IsTTY. The terminal package retains OS-level utilities: IsCI, OpenBrowser, Pager, IsUnderHomebrew, HasHomebrew.
1 parent 4837a4f commit 866c776

2 files changed

Lines changed: 20 additions & 56 deletions

File tree

cli/terminal/browser.go

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,23 @@ import (
66
"strings"
77
)
88

9-
// OpenBrowser opens the default web browser at the specified URL.
10-
//
11-
// Parameters:
12-
// - goos: The operating system name (e.g., "darwin", "windows", or "linux").
13-
// - url: The URL to open in the web browser.
14-
//
15-
// Returns:
16-
// - An *exec.Cmd configured to open the URL. Note that you must call `cmd.Run()`
17-
// or `cmd.Start()` on the returned command to execute it.
18-
//
19-
// Panics:
20-
// - This function will panic if called without a TTY (e.g., not running in a terminal).
21-
func OpenBrowser(goos, url string) *exec.Cmd {
22-
if !IsTTY() {
23-
panic("OpenBrowser called without a TTY")
24-
}
25-
9+
// openBrowserCmd returns an exec.Cmd configured to open the URL.
10+
func openBrowserCmd(goos, url string) *exec.Cmd {
2611
exe := "open"
2712
var args []string
2813

2914
switch goos {
3015
case "darwin":
31-
// macOS: Use the "open" command to open the URL.
3216
args = append(args, url)
3317
case "windows":
34-
// Windows: Use "cmd /c start" to open the URL.
3518
exe, _ = exec.LookPath("cmd")
3619
replacer := strings.NewReplacer("&", "^&")
3720
args = append(args, "/c", "start", replacer.Replace(url))
3821
default:
39-
// Linux: Use "xdg-open" or fallback to "wslview" for WSL environments.
4022
exe = linuxExe()
4123
args = append(args, url)
4224
}
4325

44-
// Create the command to open the browser and set stderr for error reporting.
4526
cmd := exec.Command(exe, args...)
4627
cmd.Stderr = os.Stderr
4728
return cmd
@@ -50,14 +31,10 @@ func OpenBrowser(goos, url string) *exec.Cmd {
5031
// linuxExe determines the appropriate command to open a web browser on Linux.
5132
func linuxExe() string {
5233
exe := "xdg-open"
53-
54-
_, err := exec.LookPath(exe)
55-
if err != nil {
56-
_, err := exec.LookPath("wslview")
57-
if err == nil {
34+
if _, err := exec.LookPath(exe); err != nil {
35+
if _, err := exec.LookPath("wslview"); err == nil {
5836
exe = "wslview"
5937
}
6038
}
61-
6239
return exe
6340
}

cli/terminal/term.go

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,28 @@ package terminal
22

33
import (
44
"os"
5+
"os/exec"
56

67
"github.com/mattn/go-isatty"
7-
"github.com/muesli/termenv"
8-
"golang.org/x/term"
98
)
109

11-
// IsTTY checks if the current output is a TTY (teletypewriter) or a Cygwin terminal.
12-
// This function is useful for determining if the program is running in a terminal
13-
// environment, which is important for features like colored output or interactive prompts.
14-
func IsTTY() bool {
15-
return isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
16-
}
17-
18-
// IsColorDisabled checks if color output is disabled based on the environment settings.
19-
// This function uses the `termenv` library to determine if the NO_COLOR environment
20-
// variable is set, which is a common way to disable colored output.
21-
func IsColorDisabled() bool {
22-
return termenv.EnvNoColor()
23-
}
24-
25-
// Width returns the terminal width in columns. Returns 80 if the width
26-
// cannot be determined (e.g. non-TTY, piped output).
27-
func Width() int {
28-
w, _, err := term.GetSize(int(os.Stdout.Fd()))
29-
if err == nil && w > 0 {
30-
return w
31-
}
32-
return 80
33-
}
34-
35-
// IsCI checks if the code is running in a Continuous Integration (CI) environment.
36-
// This function checks for common environment variables used by popular CI systems
37-
// like GitHub Actions, Travis CI, CircleCI, Jenkins, TeamCity, and others.
10+
// IsCI reports whether the process is running in a CI environment.
11+
// Checks common environment variables used by GitHub Actions, Travis CI,
12+
// CircleCI, Jenkins, TeamCity, and others.
3813
func IsCI() bool {
3914
return os.Getenv("CI") != "" || // GitHub Actions, Travis CI, CircleCI, Cirrus CI, GitLab CI, AppVeyor, CodeShip, dsari
4015
os.Getenv("BUILD_NUMBER") != "" || // Jenkins, TeamCity
4116
os.Getenv("RUN_ID") != "" // TaskCluster, dsari
4217
}
18+
19+
// OpenBrowser opens the default web browser at the specified URL.
20+
// The goos parameter should be runtime.GOOS (e.g. "darwin", "windows", "linux").
21+
//
22+
// Returns an *exec.Cmd — call cmd.Run() or cmd.Start() to execute it.
23+
// Panics if stdout is not a terminal.
24+
func OpenBrowser(goos, url string) *exec.Cmd {
25+
if !isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()) {
26+
panic("OpenBrowser called without a TTY")
27+
}
28+
return openBrowserCmd(goos, url)
29+
}

0 commit comments

Comments
 (0)