diff --git a/humanTable.go b/humanTable.go index 3b3f8c3..050e7ec 100644 --- a/humanTable.go +++ b/humanTable.go @@ -14,6 +14,7 @@ import ( // log.Info(standardTableSize10(sizes, args)) func StandardTableSize5(sizes []int, args []string) string { + WIDTH, _ := getTerminalWidth() var s string for i, si := range sizes { if si == 0 { @@ -32,19 +33,13 @@ func StandardTableSize5(sizes []int, args []string) string { arg4 := args[3] arg5 := args[4] - return fmt.Sprintf(s, arg1, arg2, arg3, arg4, arg5) + all := fmt.Sprintf(s, arg1, arg2, arg3, arg4, arg5) + return all[0:WIDTH] } -/* -func standardTable10(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10 string) string { - args := []string{arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10} - sizes := []int{40, 12, 6, 12, 16, 16, 16, 12, 12, 8} - - return standardTableSize10(sizes, args) -} -*/ - func StandardTableSize10(sizes []int, args []string) string { + WIDTH, _ := getTerminalWidth() + // log.Info("WIDTH IS", WIDTH) var s string for i, si := range sizes { if si == 0 { @@ -68,5 +63,12 @@ func StandardTableSize10(sizes []int, args []string) string { arg9 := args[8] arg10 := args[9] - return fmt.Sprintf(s, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10) + all := fmt.Sprintf(s, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10) + var small int + if len(all) > WIDTH { + small = WIDTH + } else { + small = len(all) - 3 + } + return all[0:small] } 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]) +}