mirror of
https://github.com/openziti/zrok.git
synced 2025-06-24 19:51:32 +02:00
added manual wrapping to be done after reflow package
This commit is contained in:
parent
cd9da59718
commit
2791b7d835
@ -1,97 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/muesli/reflow/wordwrap"
|
||||
"github.com/openziti/zrok/tui"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var testA = "[13.845] DEBUG sdk-golang/ziti/edge/impl. (*edgeConn) .Accept: {edgeSeq= [2] vid=[invalid- vid-size-of-0-bytes] connId= [2147483694] type= [EdgeDataType] chSeq= [7]} receivedddddd 567 bytes (msg type: 60786)"
|
||||
|
||||
var testB = "[] Hello this is a test to see if word wrapping works propery. {Need to pad space. WhathappensifIputareallylongwordwithnobreakswillitcutthewordorwillitproperlyloverflow and then (a new line)} ()ß"
|
||||
|
||||
func init() {
|
||||
testCmd.AddCommand(newShareTestCommand().cmd)
|
||||
}
|
||||
|
||||
type shareTestCommand struct {
|
||||
headless bool
|
||||
cmd *cobra.Command
|
||||
}
|
||||
|
||||
func newShareTestCommand() *shareTestCommand {
|
||||
cmd := &cobra.Command{
|
||||
Use: "share <target>",
|
||||
Short: "Share a target resource publicly",
|
||||
Args: cobra.ExactArgs(1),
|
||||
}
|
||||
command := &shareTestCommand{cmd: cmd}
|
||||
cmd.Flags().BoolVar(&command.headless, "headless", false, "Disable TUI and run headless")
|
||||
|
||||
cmd.Run = command.run
|
||||
return command
|
||||
}
|
||||
|
||||
func (cmd *shareTestCommand) run(_ *cobra.Command, args []string) {
|
||||
w := 167
|
||||
if !cmd.headless {
|
||||
mdl := newShareModel("token", []string{"Endpoints"}, "shareMode", "backendMode")
|
||||
prg := tea.NewProgram(mdl, tea.WithAltScreen())
|
||||
mdl.prg = prg
|
||||
|
||||
go func() {
|
||||
mdl.Write([]byte(testA))
|
||||
mdl.Write([]byte(testB))
|
||||
}()
|
||||
|
||||
if _, err := prg.Run(); err != nil {
|
||||
tui.Error("An error occurred", err)
|
||||
}
|
||||
} else {
|
||||
for i := w; i >= 0; i = i - 10 {
|
||||
fmt.Println("-----------------------------")
|
||||
fmt.Printf("Width: %d\n", i)
|
||||
out, n := plainTextRender(i, testA)
|
||||
fmt.Println(out)
|
||||
fmt.Printf("Expected lines: %d\n", n)
|
||||
fmt.Println("-----------------------------")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func plainTextRender(width int, logs ...string) (string, int) {
|
||||
var splitLines []string
|
||||
for _, line := range logs {
|
||||
wrapped := wordwrap.String(line, width)
|
||||
|
||||
wrappedLines := strings.Split(wrapped, "\n")
|
||||
for _, wrappedLine := range wrappedLines {
|
||||
splitLine := strings.ReplaceAll(wrappedLine, "\n", "")
|
||||
if splitLine != "" {
|
||||
splitLines = append(splitLines, splitLine)
|
||||
}
|
||||
}
|
||||
}
|
||||
maxRows := shareLogStyle.GetHeight()
|
||||
maxRows = 999
|
||||
startRow := 0
|
||||
if len(splitLines) > maxRows {
|
||||
startRow = len(splitLines) - maxRows
|
||||
}
|
||||
out := ""
|
||||
n := 0
|
||||
for i := startRow; i < len(splitLines); i++ {
|
||||
outLine := splitLines[i]
|
||||
n++
|
||||
if i < len(splitLines)-1 {
|
||||
outLine += "\n"
|
||||
}
|
||||
out += outLine
|
||||
}
|
||||
return out, n
|
||||
|
||||
}
|
@ -2,16 +2,20 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/muesli/reflow/wordwrap"
|
||||
"github.com/openziti/zrok/endpoints"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const shareTuiBacklog = 256
|
||||
|
||||
var wordwrapCharacters = " -"
|
||||
var wordwrapBreakpoints = map[rune]bool{' ': true, '-': true}
|
||||
|
||||
type shareModel struct {
|
||||
shrToken string
|
||||
frontendDescriptions []string
|
||||
@ -144,6 +148,7 @@ func (m *shareModel) renderRequests() string {
|
||||
}
|
||||
}
|
||||
}
|
||||
requestLines = wrap(requestLines, m.width-2)
|
||||
maxRows := shareRequestsStyle.GetHeight()
|
||||
startRow := 0
|
||||
if len(requestLines) > maxRows {
|
||||
@ -183,6 +188,7 @@ func (m *shareModel) renderLog() string {
|
||||
}
|
||||
}
|
||||
}
|
||||
splitLines = wrap(splitLines, m.width-2)
|
||||
maxRows := shareLogStyle.GetHeight()
|
||||
startRow := 0
|
||||
if len(splitLines) > maxRows {
|
||||
@ -211,6 +217,38 @@ func (m *shareModel) Write(p []byte) (n int, err error) {
|
||||
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().
|
||||
BorderStyle(lipgloss.RoundedBorder()).
|
||||
BorderForeground(lipgloss.Color("63")).
|
||||
|
Loading…
x
Reference in New Issue
Block a user