summaryrefslogtreecommitdiff
path: root/termSize.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-09-04 00:13:24 -0500
committerJeff Carr <[email protected]>2025-09-04 00:13:24 -0500
commit6c58ab5e15104569b802681cacfb6882269dfb7f (patch)
treeae87ce9e19d4db53c10361910ade15e9a4a2d47f /termSize.go
parentf5b513fa05ddbaaa2149cf0527ae1206e653266b (diff)
Diffstat (limited to 'termSize.go')
-rw-r--r--termSize.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/termSize.go b/termSize.go
new file mode 100644
index 0000000..a3201f1
--- /dev/null
+++ b/termSize.go
@@ -0,0 +1,41 @@
+package main
+
+import (
+ "log"
+ "os"
+
+ "golang.org/x/term"
+)
+
+// getTerminalWidth returns the width of the active terminal.
+// If the output is not an interactive terminal (e.g., it's being piped to a file
+// or another command), it returns a default width and false.
+func getTerminalWidth() (int, bool) {
+ // term.IsTerminal checks if the given file descriptor is connected to a terminal.
+ // We use os.Stdout.Fd() to check the standard output.
+ if term.IsTerminal(int(os.Stdout.Fd())) {
+ // term.GetSize returns the dimensions of the given terminal.
+ width, _, err := term.GetSize(int(os.Stdout.Fd()))
+ if err != nil {
+ // If we can't get the size for some reason, fall back to the default.
+ log.Printf("could not get terminal size: %v", err)
+ return 120, false
+ }
+ return width, true
+ }
+
+ // If it's not a terminal, return the default width.
+ return 120, false
+}
+
+// truncateString shortens a string to the specified length, adding an ellipsis if truncated.
+func truncateString(s string, maxLength int) string {
+ if len(s) <= maxLength {
+ return s
+ }
+ // Subtract 3 to make room for the ellipsis "..."
+ if maxLength < 3 {
+ return "..."
+ }
+ return s[:maxLength-3] + "..."
+}