diff options
| -rw-r--r-- | tablePB.go | 1 | ||||
| -rw-r--r-- | termSize.go | 19 | ||||
| -rw-r--r-- | termSize_darwin.go | 27 | ||||
| -rw-r--r-- | termSize_linux.go | 44 | ||||
| -rw-r--r-- | termSize_windows.go | 27 |
5 files changed, 101 insertions, 17 deletions
@@ -30,6 +30,7 @@ func PrintTable(pb *guipb.Table) { var args []string var sizes []int + log.Info("INFO: table len=", len(pb.AnyCols)) // first print the table header for _, col := range pb.AnyCols { args = append(args, col.Header.Name) diff --git a/termSize.go b/termSize.go index 85594e0..8595c5e 100644 --- a/termSize.go +++ b/termSize.go @@ -1,12 +1,10 @@ package cobol import ( - "os" "strings" "unicode" "go.wit.com/log" - "golang.org/x/term" ) var WIDTH int = 120 @@ -16,22 +14,9 @@ var TERMSIZE int = 80 // 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 - } + newWidth, ok := osTerminalWidth() - // If it's not a terminal, return the default width. - return WIDTH, false + return newWidth, ok } // like the perl Chomp but with the terminal width diff --git a/termSize_darwin.go b/termSize_darwin.go new file mode 100644 index 0000000..0702792 --- /dev/null +++ b/termSize_darwin.go @@ -0,0 +1,27 @@ +package cobol + +import ( + "os" + + "go.wit.com/log" + "golang.org/x/term" +) + +func osTerminalWidth() (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 +} diff --git a/termSize_linux.go b/termSize_linux.go new file mode 100644 index 0000000..34201ac --- /dev/null +++ b/termSize_linux.go @@ -0,0 +1,44 @@ +package cobol + +import ( + "os" + + "go.wit.com/log" + "golang.org/x/term" +) + +func osTerminalWidth() (int, bool) { + w, _, err := ttyGetSize() + if err == nil { + return w, true + } + + // 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 ttyGetSize() (int, int, error) { + tty, err := os.Open("/dev/tty") + if err != nil { + return 0, 0, err + } + w, h, err := term.GetSize(int(tty.Fd())) + if err != nil { + log.Info("linux.osTerminalWidth tty.Fd() err", w, h, err) + } + return w, h, err +} diff --git a/termSize_windows.go b/termSize_windows.go new file mode 100644 index 0000000..0702792 --- /dev/null +++ b/termSize_windows.go @@ -0,0 +1,27 @@ +package cobol + +import ( + "os" + + "go.wit.com/log" + "golang.org/x/term" +) + +func osTerminalWidth() (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 +} |
