terminal

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

README

Terminal

The terminal package provides a collection of utilities to manage terminal interactions, including pager handling, TTY detection, and environment configuration for better command-line application support.

Features

  • Pager Management: Easily manage pagers like less or more to display output in a paginated format.
  • TTY Detection: Check if the program is running in a terminal environment.
  • Color Management: Determine if color output is disabled based on environment variables.
  • CI Detection: Identify if the program is running in a Continuous Integration (CI) environment.
  • Homebrew Utilities: Check for Homebrew installation and verify binary paths.
  • Browser Launching: Open URLs in the default web browser, with cross-platform support.

Installation

To include this package in your Go project, use:

go get github.com/raystack/salt

Usage

1. Creating and Using a Pager

The Pager struct manages a pager process for displaying output in a paginated format.

package main

import (
    "fmt"
    "github.com/raystack/salt/terminal"
)

func main() {
    // Create a new Pager instance
    pager := terminal.NewPager()

    // Optionally, set a custom pager command
    pager.Set("less -R")

    // Start the pager
    err := pager.Start()
    if err != nil {
        fmt.Println("Error starting pager:", err)
        return
    }
    defer pager.Stop()

    // Output text to the pager
    fmt.Fprintln(pager.Out, "This is a sample text output to the pager.")
}
2. Checking if the Terminal is a TTY

Use IsTTY to check if the output is a TTY (teletypewriter).

if terminal.IsTTY() {
    fmt.Println("Running in a terminal!")
} else {
    fmt.Println("Not running in a terminal.")
}
3. Checking if Color Output is Disabled

Use IsColorDisabled to determine if color output should be suppressed.

if terminal.IsColorDisabled() {
    fmt.Println("Color output is disabled.")
} else {
    fmt.Println("Color output is enabled.")
}
4. Checking if Running in a CI Environment

Use IsCI to check if the program is running in a CI environment.

if terminal.IsCI() {
    fmt.Println("Running in a Continuous Integration environment.")
} else {
    fmt.Println("Not running in a CI environment.")
}
4. Checking if Running in a CI Environment

Use IsCI to check if the program is running in a CI environment.

if terminal.IsCI() {
    fmt.Println("Running in a Continuous Integration environment.")
} else {
    fmt.Println("Not running in a CI environment.")
}
5. Checking for Homebrew Installation

Use HasHomebrew to check if Homebrew is installed on the system.

if terminal.HasHomebrew() {
    fmt.Println("Homebrew is installed!")
} else {
    fmt.Println("Homebrew is not installed.")
}
6. Checking if a Binary is Under Homebrew Path

Use IsUnderHomebrew to determine if a binary is managed by Homebrew.

binaryPath := "/usr/local/bin/somebinary"
if terminal.IsUnderHomebrew(binaryPath) {
    fmt.Println("The binary is under the Homebrew path.")
} else {
    fmt.Println("The binary is not under the Homebrew path.")
}
7. Opening a URL in the Default Web Browser

Use OpenBrowser to launch the default web browser with a specified URL.

goos := "darwin" // Use runtime.GOOS to get the current OS in a real scenario
url := "https://www.example.com"
cmd := terminal.OpenBrowser(goos, url)
if err := cmd.Run(); err != nil {
    fmt.Println("Failed to open browser:", err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasHomebrew

func HasHomebrew() bool

HasHomebrew checks if Homebrew is installed on the user's system.

This function determines the presence of Homebrew by looking for the "brew" executable in the system's PATH. It is useful to ensure Homebrew dependencies can be managed before executing related commands.

Returns:

  • A boolean value indicating whether Homebrew is installed.

func IsCI

func IsCI() bool

IsCI checks if the code is running in a Continuous Integration (CI) environment.

This function checks for common environment variables used by popular CI systems like GitHub Actions, Travis CI, CircleCI, Jenkins, TeamCity, and others.

Returns:

  • A boolean value indicating whether the code is running in a CI environment.

func IsColorDisabled

func IsColorDisabled() bool

IsColorDisabled checks if color output is disabled based on the environment settings.

This function uses the `termenv` library to determine if the NO_COLOR environment variable is set, which is a common way to disable colored output.

Returns:

  • A boolean value indicating whether color output is disabled.

func IsTTY

func IsTTY() bool

IsTTY checks if the current output is a TTY (teletypewriter) or a Cygwin terminal.

This function is useful for determining if the program is running in a terminal environment, which is important for features like colored output or interactive prompts.

Returns:

  • A boolean value indicating whether the current output is a TTY or Cygwin terminal.

func IsUnderHomebrew

func IsUnderHomebrew(path string) bool

IsUnderHomebrew checks if a given binary path is managed under the Homebrew path.

This function is useful to verify if a binary is installed via Homebrew by comparing its location to the Homebrew binary directory.

Parameters:

  • path: The path of the binary to check.

Returns:

  • A boolean value indicating whether the binary is located under the Homebrew path.

func OpenBrowser

func OpenBrowser(goos, url string) *exec.Cmd

OpenBrowser opens the default web browser at the specified URL.

Parameters:

  • goos: The operating system name (e.g., "darwin", "windows", or "linux").
  • url: The URL to open in the web browser.

Returns:

  • An *exec.Cmd configured to open the URL. Note that you must call `cmd.Run()` or `cmd.Start()` on the returned command to execute it.

Panics:

  • This function will panic if called without a TTY (e.g., not running in a terminal).

Types

type ErrClosedPagerPipe

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

ErrClosedPagerPipe is an error type returned when writing to a closed pager pipe.

type Pager

type Pager struct {
	Out    io.Writer // The writer to send output to the pager.
	ErrOut io.Writer // The writer to send error output to.
	// contains filtered or unexported fields
}

Pager manages a pager process for displaying output in a paginated format.

It supports configuring the pager command, starting the pager process, and ensuring proper cleanup when the pager is no longer needed.

func NewPager

func NewPager() *Pager

NewPager creates a new Pager instance with default settings.

If the "PAGER" environment variable is not set, the default command is "more".

func (*Pager) Get

func (p *Pager) Get() string

Get returns the current pager command.

Returns:

  • The pager command as a string.

func (*Pager) Set

func (p *Pager) Set(cmd string)

Set updates the pager command used to display output.

Parameters:

  • cmd: The pager command (e.g., "less", "more").

func (*Pager) Start

func (p *Pager) Start() error

Start begins the pager process to display output.

If the pager command is "cat" or empty, it does nothing. The function also sets environment variables to optimize the behavior of certain pagers, like "less" and "lv".

Returns:

  • An error if the pager command fails to start or if arguments cannot be parsed.

func (*Pager) Stop

func (p *Pager) Stop()

Stop terminates the running pager process and cleans up resources.

Jump to

Keyboard shortcuts

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