From a9692b87f139a6f11756f6abb078f29111d499f7 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 20 Aug 2025 07:41:56 -0500 Subject: [PATCH] gemini repo day1 --- .gitignore | 3 +++ terminal_width.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 .gitignore create mode 100644 terminal_width.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a630ed4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.swp +go.mod +go.sum diff --git a/terminal_width.go b/terminal_width.go new file mode 100644 index 0000000..42726d3 --- /dev/null +++ b/terminal_width.go @@ -0,0 +1,35 @@ +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() +}