add TerminalChomp() in honor of Larry Wall & Perl

This commit is contained in:
Jeff Carr 2025-09-04 08:04:47 -05:00
parent 041d8945c9
commit bcff630f5b
1 changed files with 16 additions and 3 deletions

View File

@ -1,9 +1,11 @@
package cobol package cobol
import ( import (
"log"
"os" "os"
"strings"
"unicode"
"go.wit.com/log"
"golang.org/x/term" "golang.org/x/term"
) )
@ -31,7 +33,18 @@ func getTerminalWidth() (int, bool) {
return WIDTH, false return WIDTH, false
} }
func TerminalCut(cut string) { // like the perl Chomp but with the terminal width
func TerminalChomp(cut string) string {
i, _ := getTerminalWidth() i, _ := getTerminalWidth()
log.Printf("%s\n", cut[0:i]) // log.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 log.Sprintf("%s", cut[0:i])
}
} }