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