shithub: hell

ref: e5b736cb7f152d66efcc2c8b5e33f372dd8db2f5
dir: /format.go/

View raw version
package main

import (
	"strings"

	//"github.com/chzyer/readline"
)


func hyphenate(input string) string {
	width := readline.GetScreenWidth()
	result, remainder := hyphenateline(input, width)
	var cresult string
	var result string
	for len([]rune(remainder)) > width-1 {
		cresult, remainder = hyphenateline(remainder, width)
		result += cresult
		if len([]rune(cresult)) == width-1 {
			if(!strings.HasSuffix(cresult, " ")) {
			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)
}