Merge pull request #282 from openziti/tui-wordwrap

Tui wordwrap (#180)
This commit is contained in:
Michael Quigley 2023-04-25 15:14:38 -04:00 committed by GitHub
commit 395b522c3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,16 +2,20 @@ package main
import ( import (
"fmt" "fmt"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"github.com/muesli/reflow/wordwrap" "github.com/muesli/reflow/wordwrap"
"github.com/openziti/zrok/endpoints" "github.com/openziti/zrok/endpoints"
"strings"
"time"
) )
const shareTuiBacklog = 256 const shareTuiBacklog = 256
var wordwrapCharacters = " -"
var wordwrapBreakpoints = map[rune]bool{' ': true, '-': true}
type shareModel struct { type shareModel struct {
shrToken string shrToken string
frontendDescriptions []string frontendDescriptions []string
@ -144,6 +148,7 @@ func (m *shareModel) renderRequests() string {
} }
} }
} }
requestLines = wrap(requestLines, m.width-2)
maxRows := shareRequestsStyle.GetHeight() maxRows := shareRequestsStyle.GetHeight()
startRow := 0 startRow := 0
if len(requestLines) > maxRows { if len(requestLines) > maxRows {
@ -183,6 +188,7 @@ func (m *shareModel) renderLog() string {
} }
} }
} }
splitLines = wrap(splitLines, m.width-2)
maxRows := shareLogStyle.GetHeight() maxRows := shareLogStyle.GetHeight()
startRow := 0 startRow := 0
if len(splitLines) > maxRows { if len(splitLines) > maxRows {
@ -211,6 +217,38 @@ func (m *shareModel) Write(p []byte) (n int, err error) {
return len(p), nil return len(p), nil
} }
func wrap(lines []string, width int) []string {
ret := make([]string, 0)
for _, line := range lines {
if width <= 0 || len(line) <= width {
ret = append(ret, line)
continue
}
for i := 0; i <= len(line); {
max := i + width
if max > len(line) {
max = len(line)
}
if line[i:max] == "" {
continue
}
nextI := i + width
if max < len(line)-1 {
if !wordwrapBreakpoints[rune(line[max])] || !wordwrapBreakpoints[rune(line[max+1])] {
lastSpace := strings.LastIndexAny(line[:max], wordwrapCharacters)
if lastSpace > -1 {
max = lastSpace
nextI = lastSpace
}
}
}
ret = append(ret, strings.TrimSpace(line[i:max]))
i = nextI
}
}
return ret
}
var shareHeaderStyle = lipgloss.NewStyle(). var shareHeaderStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()). BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("63")). BorderForeground(lipgloss.Color("63")).