package gui import ( "fmt" "github.com/liamg/aminal/buffer" ) func (gui *GUI) textbox(col uint16, row uint16, text string) { lines := []string{} line := "" word := "" maxWidth := int(gui.terminal.ActiveBuffer().ViewWidth()) - 4 maxHeight := (int(gui.terminal.ActiveBuffer().ViewHeight()) / 2) - 2 if maxHeight < 1 { return } longestLine := 0 addWord := func() { if len(line)+len(word) <= maxWidth { line = fmt.Sprintf("%s%s", line, word) if len(line) < maxWidth { line = fmt.Sprintf("%s ", line) } else { lines = append(lines, line) line = "" } } else { lines = append(lines, line) line = word for len(line) > maxWidth { // break word into bits } } word = "" } addLine := func() bool { addWord() if len(line) > longestLine { longestLine = len(line) } lines = append(lines, line) if len(lines) >= maxHeight-1 { lines = append(lines, "...") return true } line = "" return false } var done = false DONE: for _, c := range text { switch c { case 0x0d: continue case 0x0a: if done = addLine(); done { break DONE } case ' ': addWord() default: word = fmt.Sprintf("%s%c", word, c) } } if word != "" { addWord() } if line != "" && !done { addLine() } for hx := col; hx < col+uint16(longestLine)+2; hx++ { for hy := row - 1; hy < row+uint16(len(lines))+1; hy++ { gui.renderer.DrawCellBg(buffer.NewBackgroundCell([3]float32{0, 0, 0}), uint(hx), uint(hy), false, nil, true) } } x := (float32(col) * gui.renderer.cellWidth) gui.font.SetColor(0.2, 1, 0.2, 1) for i, line := range lines { y := (float32(row+1+uint16(i)) * gui.renderer.cellHeight) - (gui.font.LinePadding()) gui.font.Print(x, y, fmt.Sprintf(" %s", line)) } }