36 lines
800 B
Go
36 lines
800 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"golang.org/x/term"
|
|
)
|
|
|
|
func main() {
|
|
// Check if the program is running in a terminal.
|
|
// If not, width and height will be meaningless.
|
|
if !term.IsTerminal(int(os.Stdout.Fd())) {
|
|
fmt.Println("Not running in a terminal.")
|
|
return
|
|
}
|
|
|
|
// term.GetSize(int(os.Stdout.Fd())) gets the dimensions of the given terminal.
|
|
// os.Stdout.Fd() returns the file descriptor for standard output.
|
|
width, height, err := term.GetSize(int(os.Stdout.Fd()))
|
|
if err != nil {
|
|
log.Fatalf("failed to get terminal size: %v", err)
|
|
}
|
|
|
|
fmt.Printf("Terminal size:\n")
|
|
fmt.Printf("Width: %d\n", width)
|
|
fmt.Printf("Height: %d\n", height)
|
|
|
|
fmt.Println("\nPrinting a long line to demonstrate wrapping:")
|
|
for i := 0; i < width*2; i++ {
|
|
fmt.Print("#")
|
|
}
|
|
fmt.Println()
|
|
}
|