Skip to content

Commit f0efbf6

Browse files
authored
fix(message): solve the terminal UI issue of truncating the message if it is long (#1242)
1 parent ec2d14a commit f0efbf6

File tree

4 files changed

+48
-41
lines changed

4 files changed

+48
-41
lines changed

cmd/troubleshoot/cli/interactive_results.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import (
77
"path"
88
"time"
99

10+
"github.com/mitchellh/go-wordwrap"
1011
"github.com/pkg/errors"
1112
ui "github.com/replicatedhq/termui/v3"
1213
"github.com/replicatedhq/termui/v3/widgets"
1314
"github.com/replicatedhq/troubleshoot/internal/util"
1415
analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
16+
"github.com/replicatedhq/troubleshoot/pkg/constants"
1517
)
1618

1719
var (
@@ -178,7 +180,10 @@ func drawDetails(analysisResult *analyzerunner.AnalyzeResult) {
178180

179181
currentTop := 4
180182
title := widgets.NewParagraph()
181-
title.Text = analysisResult.Title
183+
// WrapText is set to false to prevent internal wordwrap which is not accounting for the padding
184+
title.WrapText = false
185+
// For long title that lead to wrapping text, the terminal width is divided by 2 and deducted by MESSAGE_TEXT_PADDING to account for the padding
186+
title.Text = wordwrap.WrapString(analysisResult.Title, uint(termWidth/2-constants.MESSAGE_TEXT_PADDING))
182187
title.Border = false
183188
if analysisResult.IsPass {
184189
title.TextStyle = ui.NewStyle(ui.ColorGreen, ui.ColorClear, ui.ModifierBold)
@@ -187,34 +192,26 @@ func drawDetails(analysisResult *analyzerunner.AnalyzeResult) {
187192
} else if analysisResult.IsFail {
188193
title.TextStyle = ui.NewStyle(ui.ColorRed, ui.ColorClear, ui.ModifierBold)
189194
}
190-
height := estimateNumberOfLines(title.Text, termWidth/2)
195+
height := util.EstimateNumberOfLines(title.Text)
191196
title.SetRect(termWidth/2, currentTop, termWidth, currentTop+height)
192197
ui.Render(title)
193198
currentTop = currentTop + height + 1
194199

195200
message := widgets.NewParagraph()
196-
message.Text = analysisResult.Message
201+
// WrapText is set to false to prevent internal wordwrap which is not accounting for the padding
202+
message.WrapText = false
203+
// For long text that lead to wrapping text, the terminal width is divided by 2 and deducted by MESSAGE_TEXT_PADDING to account for the padding
204+
message.Text = wordwrap.WrapString(analysisResult.Message, uint(termWidth/2-constants.MESSAGE_TEXT_PADDING))
205+
if analysisResult.URI != "" {
206+
// Add URL to the message with wordwrap
207+
// Add emply lines as separator
208+
urlText := wordwrap.WrapString(fmt.Sprintf("For more information: %s", analysisResult.URI), uint(termWidth/2-constants.MESSAGE_TEXT_PADDING))
209+
message.Text = message.Text + "\n\n" + urlText
210+
}
211+
height = util.EstimateNumberOfLines(message.Text) + constants.MESSAGE_TEXT_LINES_MARGIN_TO_BOTTOM
197212
message.Border = false
198-
height = estimateNumberOfLines(message.Text, termWidth/2) + 2
199213
message.SetRect(termWidth/2, currentTop, termWidth, currentTop+height)
200214
ui.Render(message)
201-
currentTop = currentTop + height + 1
202-
203-
if analysisResult.URI != "" {
204-
uri := widgets.NewParagraph()
205-
uri.Text = fmt.Sprintf("For more information: %s", analysisResult.URI)
206-
uri.Border = false
207-
// For long urls that lead to wrapping text, make the rectangle bigger by
208-
// increasing the calculated height by 2
209-
height = estimateNumberOfLines(uri.Text, termWidth/2) + 2
210-
uri.SetRect(termWidth/2, currentTop, termWidth, currentTop+height)
211-
ui.Render(uri)
212-
}
213-
}
214-
215-
func estimateNumberOfLines(text string, width int) int {
216-
lines := len(text)/width + 1
217-
return lines
218215
}
219216

220217
func showSaved(filename string) {

internal/util/util.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ func AppName(name string) string {
4444
func SplitYAML(doc string) []string {
4545
return strings.Split(doc, "\n---\n")
4646
}
47+
48+
func EstimateNumberOfLines(text string) int {
49+
n := strings.Count(text, "\n")
50+
if len(text) > 0 && !strings.HasSuffix(text, "\n") {
51+
n++
52+
}
53+
return n
54+
}

pkg/constants/constants.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,8 @@ const (
7373

7474
// Troubleshoot spec constants
7575
Troubleshootv1beta2Kind = "troubleshoot.sh/v1beta2"
76+
77+
// TermUI Display Constants
78+
MESSAGE_TEXT_PADDING = 4
79+
MESSAGE_TEXT_LINES_MARGIN_TO_BOTTOM = 4
7680
)

pkg/preflight/interactive_results.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import (
44
"fmt"
55
"time"
66

7+
"github.com/mitchellh/go-wordwrap"
78
"github.com/pkg/errors"
89
ui "github.com/replicatedhq/termui/v3"
910
"github.com/replicatedhq/termui/v3/widgets"
1011
"github.com/replicatedhq/troubleshoot/internal/util"
1112
analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
13+
"github.com/replicatedhq/troubleshoot/pkg/constants"
1214
)
1315

1416
var (
@@ -179,7 +181,10 @@ func drawDetails(analysisResult *analyzerunner.AnalyzeResult) {
179181

180182
currentTop := 4
181183
title := widgets.NewParagraph()
182-
title.Text = analysisResult.Title
184+
// WrapText is set to false to prevent internal wordwrap which is not accounting for the padding
185+
title.WrapText = false
186+
// For long title that lead to wrapping text, the terminal width is divided by 2 and deducted by MESSAGE_TEXT_PADDING to account for the padding
187+
title.Text = wordwrap.WrapString(analysisResult.Title, uint(termWidth/2-constants.MESSAGE_TEXT_PADDING))
183188
title.Border = false
184189
if analysisResult.IsPass {
185190
title.TextStyle = ui.NewStyle(ui.ColorGreen, ui.ColorClear, ui.ModifierBold)
@@ -188,33 +193,26 @@ func drawDetails(analysisResult *analyzerunner.AnalyzeResult) {
188193
} else if analysisResult.IsFail {
189194
title.TextStyle = ui.NewStyle(ui.ColorRed, ui.ColorClear, ui.ModifierBold)
190195
}
191-
height := estimateNumberOfLines(title.Text, termWidth/2)
196+
height := util.EstimateNumberOfLines(title.Text)
192197
title.SetRect(termWidth/2, currentTop, termWidth, currentTop+height)
193198
ui.Render(title)
194199
currentTop = currentTop + height + 1
195200

196201
message := widgets.NewParagraph()
197-
message.Text = analysisResult.Message
202+
// WrapText is set to false to prevent internal wordwrap which is not accounting for the padding
203+
message.WrapText = false
204+
// For long text that lead to wrapping text, the terminal width is divided by 2 and deducted by MESSAGE_TEXT_PADDING to account for the padding
205+
message.Text = wordwrap.WrapString(analysisResult.Message, uint(termWidth/2-constants.MESSAGE_TEXT_PADDING))
206+
if analysisResult.URI != "" {
207+
// Add URL to the message with wordwrap
208+
// Add emply lines as separator
209+
urlText := wordwrap.WrapString(fmt.Sprintf("For more information: %s", analysisResult.URI), uint(termWidth/2-constants.MESSAGE_TEXT_PADDING))
210+
message.Text = message.Text + "\n\n" + urlText
211+
}
212+
height = util.EstimateNumberOfLines(message.Text) + constants.MESSAGE_TEXT_LINES_MARGIN_TO_BOTTOM
198213
message.Border = false
199-
height = estimateNumberOfLines(message.Text, termWidth/2) + 2
200214
message.SetRect(termWidth/2, currentTop, termWidth, currentTop+height)
201215
ui.Render(message)
202-
currentTop = currentTop + height + 1
203-
204-
if analysisResult.URI != "" {
205-
uri := widgets.NewParagraph()
206-
uri.Text = fmt.Sprintf("For more information: %s", analysisResult.URI)
207-
uri.Border = false
208-
height = estimateNumberOfLines(uri.Text, termWidth/2)
209-
uri.SetRect(termWidth/2, currentTop, termWidth, currentTop+height)
210-
ui.Render(uri)
211-
currentTop = currentTop + height + 1
212-
}
213-
}
214-
215-
func estimateNumberOfLines(text string, width int) int {
216-
lines := len(text)/width + 1
217-
return lines
218216
}
219217

220218
func showSaved(filename string) {

0 commit comments

Comments
 (0)