shithub: hell

ref: 613aaa101f1ca88bbfcd9d6fa168a69115c16109
dir: /format.go/

View raw version
package main

//import "fmt"
import "strings"
import "github.com/chzyer/readline"

func hyphenate(input string) string {
	width := readline.GetScreenWidth()
	result, remainder := hyphenateline(input, width)
	var cresult string
	for len(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(begin) > width-1 {
		begin = begin[:width-1]
		hyphenate = true
	}

	remainder := input[len(begin):]

	if hyphenate == true && begin[width-2] != ' ' {
		begin += "-"
	}
	if len(remainder) > width-1 {
		begin += "\n"
	}
	return begin, remainder
}