ref: 0a7455c6a861565d74e3bff9d0cd87bd391db14f
dir: /format.go/
package main
import (
"strings"
"github.com/chzyer/readline"
)
func hyphenate(input string) string {
width := readline.GetScreenWidth()
result, remainder := hyphenateline(input, width)
var cresult string
for len([]rune(remainder)) > width-1 {
cresult, remainder = hyphenateline(remainder, width)
result += cresult
}
return result + remainder
}
func hyphenateline(input string, width int) (string, string) {
if strings.HasPrefix(input, "\n") {
return "", input[1:]
}
begin, _, _ := strings.Cut(input, "\n")
hyphenate := false
if len([]rune(begin)) > width-1 {
begin = string([]rune(begin)[:width-1])
hyphenate = true
}
remainder := string([]rune(input)[len([]rune(begin)):])
if hyphenate == true && string([]rune(begin)[width-2:]) != " " {
begin += "-"
}
if len(remainder) > width-1 {
begin += "\n"
}
return begin, remainder
}