terminal

package
v0.3.9 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 3 Imported by: 0

README

terminal

import "github.com/gechr/x/terminal"

Package terminal provides terminal detection and size queries.

Index

func Height

func Height(f *os.File) int

Height returns the height of the terminal connected to f, or 0 if f is nil or not a terminal.

Example

Height returns 0 when the file is nil or not connected to a terminal.

fmt.Println(terminal.Height(nil))

Output:

0

func Is

func Is(f *os.File) bool

Is returns true if the given file is a terminal. Returns false for nil files.

Example

A pipe is not a terminal, and nil files are always reported as non-terminals.

r, w, err := os.Pipe()
if err != nil {
    panic(err)
}
defer r.Close()
defer w.Close()

fmt.Println(terminal.Is(r))
fmt.Println(terminal.Is(w))
fmt.Println(terminal.Is(nil))

Output:

false
false
false

func IsDark

func IsDark() (bool, bool)

IsDark reports (dark, ok) for the controlling terminal. ok is false if no standard stream is a terminal or the terminal does not respond to the background-color query, in which case the first result is meaningless.

Example

IsDark reports ok=false when no standard stream is connected to a terminal, since there is no background to query.

dark, ok := terminal.IsDark()
switch {
case !ok:
    fmt.Println("no terminal detected")
case dark:
    fmt.Println("dark")
default:
    fmt.Println("light")
}

Output:

no terminal detected

func IsLight

func IsLight() (bool, bool)

IsLight reports (light, ok) for the controlling terminal. ok is false if no standard stream is a terminal or the terminal does not respond to the background-color query, in which case the first result is meaningless.

func Size

func Size(f *os.File) (int, int)

Size returns the (width, height) of the terminal connected to f in cells, or (0, 0) if f is nil or not a terminal.

Example

Size returns (0, 0) when the file is nil or not connected to a terminal.

w, h := terminal.Size(nil)
fmt.Println(w, h)

Output:

0 0

func Width

func Width(f *os.File) int

Width returns the width of the terminal connected to f. Returns 0 if f is nil or not a terminal.

Example

Width returns 0 when the file is nil or not connected to a terminal.

fmt.Println(terminal.Width(nil))

Output:

0

Documentation

Overview

Package terminal provides terminal detection and size queries.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Height

func Height(f *os.File) int

Height returns the height of the terminal connected to `f`, or 0 if `f` is nil or not a terminal.

Example

Height returns 0 when the file is nil or not connected to a terminal.

package main

import (
	"fmt"

	"github.com/gechr/x/terminal"
)

func main() {
	fmt.Println(terminal.Height(nil))
}
Output:
0

func Is

func Is(f *os.File) bool

Is returns true if the given file is a terminal. Returns false for nil files.

Example

A pipe is not a terminal, and nil files are always reported as non-terminals.

package main

import (
	"fmt"
	"os"

	"github.com/gechr/x/terminal"
)

func main() {
	r, w, err := os.Pipe()
	if err != nil {
		panic(err)
	}
	defer r.Close()
	defer w.Close()

	fmt.Println(terminal.Is(r))
	fmt.Println(terminal.Is(w))
	fmt.Println(terminal.Is(nil))
}
Output:
false
false
false

func IsDark added in v0.3.4

func IsDark() (bool, bool)

IsDark reports (dark, ok) for the controlling terminal. `ok` is false if no standard stream is a terminal or the terminal does not respond to the background-color query, in which case the first result is meaningless.

Example

IsDark reports ok=false when no standard stream is connected to a terminal, since there is no background to query.

package main

import (
	"fmt"

	"github.com/gechr/x/terminal"
)

func main() {
	dark, ok := terminal.IsDark()
	switch {
	case !ok:
		fmt.Println("no terminal detected")
	case dark:
		fmt.Println("dark")
	default:
		fmt.Println("light")
	}
}
Output:
no terminal detected

func IsLight added in v0.3.4

func IsLight() (bool, bool)

IsLight reports (light, ok) for the controlling terminal. `ok` is false if no standard stream is a terminal or the terminal does not respond to the background-color query, in which case the first result is meaningless.

func Size

func Size(f *os.File) (int, int)

Size returns the (width, height) of the terminal connected to `f` in cells, or (0, 0) if `f` is nil or not a terminal.

Example

Size returns (0, 0) when the file is nil or not connected to a terminal.

package main

import (
	"fmt"

	"github.com/gechr/x/terminal"
)

func main() {
	w, h := terminal.Size(nil)
	fmt.Println(w, h)
}
Output:
0 0

func Width

func Width(f *os.File) int

Width returns the width of the terminal connected to `f`. Returns 0 if `f` is nil or not a terminal.

Example

Width returns 0 when the file is nil or not connected to a terminal.

package main

import (
	"fmt"

	"github.com/gechr/x/terminal"
)

func main() {
	fmt.Println(terminal.Width(nil))
}
Output:
0

Types

This section is empty.

Directories

Path Synopsis
Package emulator identifies the terminal emulator hosting the process.
Package emulator identifies the terminal emulator hosting the process.

Jump to

Keyboard shortcuts

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