summaryrefslogtreecommitdiff
path: root/termSize.go
blob: 97a402afaa80e3c271c425e1d616a29648aa0a20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package cobol

import (
	"fmt"
	"strings"
	"unicode"
)

var WIDTH int = 120
var TERMSIZE int = 80

// 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) {
	newWidth, ok := osTerminalWidth()

	return newWidth, ok
}

// like the perl Chomp but with the terminal width
func TerminalChomp(cut string) string {
	i, _ := getTerminalWidth()
	// fmt.Info("cobol.TerminalCut() at ", i)

	// TrimRightFunc removes all trailing runes r from the string s that satisfy f(r).
	// unicode.IsSpace reports whether the rune is a space character.
	cut = strings.TrimRightFunc(cut, unicode.IsSpace)

	if i >= len(cut) {
		return cut
	} else {
		return fmt.Sprintf("%s", cut[0:i])
	}
}