terminal

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 2 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 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 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