turbo-editors/turbo-corepublic Fork 0
v1.0.1
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

🛟 Updated. 28d5985 · on v1.0.1 · k33g · 13h ago
wrap.go · 59 lines · 1.5 KBGo Blame HistoryRaw
 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Wrapping prose to a width. Code is never wrapped — see Lines for why.

package acp

import "strings"

// Wrap breaks text into lines no wider than width runes, at spaces where it
// can and mid-word when a word is longer than the line.
//
// Existing newlines are kept: they are the author's own breaks, and a reply
// laid out in paragraphs should stay in paragraphs.
func Wrap(text string, width int) []string {
	if width < 1 {
		return nil
	}

	var out []string
	for _, line := range strings.Split(text, "\n") {
		out = append(out, wrapLine(line, width)...)
	}
	return out
}

// wrapLine wraps one line, which always yields at least one — a blank line is
// a blank line, not nothing.
func wrapLine(line string, width int) []string {
	runes := []rune(line)
	if len(runes) <= width {
		return []string{line}
	}

	var out []string
	for len(runes) > width {
		at := breakPoint(runes, width)
		out = append(out, strings.TrimRight(string(runes[:at]), " "))
		runes = runes[at:]
		for len(runes) > 0 && runes[0] == ' ' {
			runes = runes[1:]
		}
	}
	if len(runes) > 0 {
		out = append(out, string(runes))
	}
	return out
}

// breakPoint returns where to break a run of runes, preferring the last space
// that fits and falling back to the hard edge.
//
// The fallback is what stops a long path or a base64 blob from looping for
// ever, which a space-only rule would do.
func breakPoint(runes []rune, width int) int {
	for at := width; at > 0; at-- {
		if runes[at] == ' ' {
			return at
		}
	}
	return width
}