Vectrum
Sign In
35 lines · 996 Bytes · Go
package tools

import (
	"fmt"
	"strings"
)

// maxLineRunes caps a single line so one minified bundle cannot flood the
// context window.
const maxLineRunes = 2000

// capLine shortens an over-long line, saying by how much.
func capLine(s string) string {
	r := []rune(s)
	if len(r) <= maxLineRunes {
		return s
	}
	return fmt.Sprintf("%s… (%d more characters on this line)", string(r[:maxLineRunes]), len(r)-maxLineRunes)
}

// headTail caps output by keeping the first and last n lines. Head-only
// truncation loses the end of the output, which for a test run is exactly the
// part that matters.
func headTail(s string, head, tail int) string {
	lines := strings.Split(s, "\n")
	if len(lines) <= head+tail {
		return s
	}
	elided := len(lines) - head - tail
	kept := make([]string, 0, head+tail+1)
	kept = append(kept, lines[:head]...)
	kept = append(kept, fmt.Sprintf("… %d lines elided …", elided))
	kept = append(kept, lines[len(lines)-tail:]...)
	return strings.Join(kept, "\n")
}