diff options
| author | Jeff Carr <[email protected]> | 2025-09-04 00:14:02 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-09-04 00:14:02 -0500 |
| commit | 041d8945c97e4045e887785b6e0d0cc2ff57183e (patch) | |
| tree | ebf0e76512992dcc4ded4f31d6653f3f14ed632b /termSize.go | |
| parent | 8e0697d0321a2d29da9b36f4320aa2fbdd4b8278 (diff) | |
truncate lines at stty -a row (or col, notsure)
Diffstat (limited to 'termSize.go')
| -rw-r--r-- | termSize.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/termSize.go b/termSize.go new file mode 100644 index 0000000..133bba7 --- /dev/null +++ b/termSize.go @@ -0,0 +1,37 @@ +package cobol + +import ( + "log" + "os" + + "golang.org/x/term" +) + +var WIDTH int = 120 + +// 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())) { + var err error + // 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 WIDTH, false + } + return WIDTH, true + } + + // If it's not a terminal, return the default width. + return WIDTH, false +} + +func TerminalCut(cut string) { + i, _ := getTerminalWidth() + log.Printf("%s\n", cut[0:i]) +} |
