Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ require (
github.com/go-enry/go-enry/v2 v2.9.2
github.com/go-git/go-billy/v5 v5.7.0
github.com/go-git/go-git/v5 v5.16.4
github.com/google/jsonschema-go v0.4.3
github.com/google/uuid v1.6.0
github.com/lib/pq v1.10.9
github.com/mattn/go-runewidth v0.0.23
github.com/modelcontextprotocol/go-sdk v1.7.0
github.com/muesli/termenv v0.16.0
github.com/redis/go-redis/v9 v9.18.0
Expand Down Expand Up @@ -55,7 +57,6 @@ require (
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/google/go-github/v74 v74.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/jsonschema-go v0.4.3 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.8 // indirect
github.com/hashicorp/go-version v1.8.0 // indirect
Expand All @@ -65,7 +66,6 @@ require (
github.com/lucasb-eyer/go-colorful v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.23 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/pjbgf/sha1cd v0.3.2 // indirect
Expand Down
77 changes: 46 additions & 31 deletions internal/tui/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,32 @@ func (l issueList) view() string {
dimStyle := lipgloss.NewStyle().Foreground(colorDim)

const sevW = 10
const fileW = 38
const catW = 16
const gap = 2
// Every row opens with a focus marker so the selected finding is identifiable
// from text alone, without relying on the highlight colour.
const markerW = 2

// The file column gives back width on narrow terminals so the message stays
// readable rather than being cut to a few characters.
fileW := 38
if layoutFor(l.width, 0).compact() {
fileW = 24
}

// The category column is only worth its width once the message column can
// still show a useful amount of text alongside it.
showCategory := l.width >= sevW+fileW+catW+40
fixedW := sevW + fileW + (gap * 4) + 2
showCategory := l.width >= markerW+sevW+fileW+catW+40
fixedW := markerW + sevW + fileW + (gap * 3)
if showCategory {
fixedW += catW + gap
}
msgW := max(l.width-fixedW, 8)

header := fmt.Sprintf(" %s %s", headerStyle.Width(sevW).Render("Severity"), headerStyle.Width(fileW).Render("File"))
header := fmt.Sprintf("%s%s %s",
strings.Repeat(" ", markerW),
headerStyle.Width(sevW).Render("Severity"),
headerStyle.Width(fileW).Render("File"))
if showCategory {
header += " " + headerStyle.Width(catW).Render("Category")
}
Expand All @@ -137,46 +149,49 @@ func (l issueList) view() string {
sev = "low"
}

sevStr := lipgloss.NewStyle().
Foreground(severityColor(sev)).
Bold(true).
Width(sevW).
Render(sev)
selected := i == l.cursor

// Each cell carries the selection background itself. A style that only
// wrapped the finished row would be cancelled by the reset sequence
// every inner style emits, leaving the highlight painted on the
// padding alone.
cell := func(colour lipgloss.Color) lipgloss.Style {
style := lipgloss.NewStyle().Foreground(colour)
if selected {
style = style.Background(colorSelectedBg)
}
return style
}

marker := " "
if selected {
marker = "› "
}

sevStr := cell(severityColor(sev)).Bold(true).Width(sevW).Render(sev)

base := filepath.Base(issue.FilePath)
if issue.LineNumber != nil {
base = fmt.Sprintf("%s:%d", base, *issue.LineNumber)
}
fileStr := lipgloss.NewStyle().
Foreground(colorFilePath).
Width(fileW).
Render(truncate(base, fileW-1))
fileStr := cell(colorFilePath).Width(fileW).Render(truncate(base, fileW-1))
msgStr := cell(colorText).Render(truncate(issue.Message, msgW))

msgStr := lipgloss.NewStyle().
Foreground(colorText).
Render(truncate(issue.Message, msgW))

row := fmt.Sprintf(" %s %s", sevStr, fileStr)
row := cell(colorAccentBlue).Bold(true).Render(marker) + sevStr + cell(colorText).Render(" ") + fileStr
if showCategory {
category := issue.Category
if category == "" {
category = "—"
}
row += " " + lipgloss.NewStyle().
Foreground(colorDim).
Width(catW).
Render(truncate(category, catW-1))
row += cell(colorText).Render(" ") + cell(colorDim).Width(catW).Render(truncate(category, catW-1))
}
row += " " + msgStr

if i == l.cursor {
row = lipgloss.NewStyle().
Background(colorSelectedBg).
Width(l.width).
Render(row)
} else {
row = lipgloss.NewStyle().Width(l.width).Render(row)
row += cell(colorText).Render(" ") + msgStr

rowStyle := lipgloss.NewStyle().Width(l.width)
if selected {
rowStyle = rowStyle.Background(colorSelectedBg)
}
row = rowStyle.Render(row)
lines = append(lines, row)
}

Expand Down
59 changes: 47 additions & 12 deletions internal/tui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,21 @@ func (m *ConfigModel) View() tea.View {
}

func (m *ConfigModel) render() string {
const boxWidth = 104
const innerWidth = boxWidth - 6
// The editor fills the terminal up to its comfortable maximum, so it never
// renders wider than the window it is drawn in.
boxWidth := min(104, max(m.width-2, 20))
innerWidth := boxWidth - 6

const keyW = 22
const valW = 20
const gap = 2
descW := innerWidth - keyW - valW - (gap * 2)
const markerW = 2
// The description column is the first thing to go when the terminal cannot
// hold the key and value columns alongside it. Kept below its minimum it
// would wrap one word per line, which is far less readable than dropping it.
const minimumDescW = 18
descW := innerWidth - markerW - keyW - valW - (gap * 2)
showDescription := descW >= minimumDescW

titleStyle := lipgloss.NewStyle().Foreground(colorAccentBlue).Bold(true)

Expand Down Expand Up @@ -350,6 +358,7 @@ func (m *ConfigModel) render() string {
normalRow := lipgloss.NewStyle().Width(innerWidth)

var b strings.Builder
cursorLine := 0
b.WriteString(titleStyle.Render("Settings"))
b.WriteString("\n\n")

Expand Down Expand Up @@ -382,21 +391,29 @@ func (m *ConfigModel) render() string {
keyRendered = keyNormalStyle.Render(item.Key)
}

row := keyRendered +
strings.Repeat(" ", gap) +
descStyle.Render(item.Description) +
strings.Repeat(" ", gap) +
valueBadge(item, i)
marker := " "
if i == m.cursor {
marker = "› "
}
row := marker + keyRendered + strings.Repeat(" ", gap)
if showDescription {
// Truncated rather than wrapped: a description that spills onto a
// second line turns every setting into a two-row entry.
row += descStyle.Render(truncate(item.Description, descW)) + strings.Repeat(" ", gap)
}
row += valueBadge(item, i)

if i == m.cursor {
// Recorded so the scrolling window below can keep the focused row
// on screen.
cursorLine = strings.Count(b.String(), "\n")
b.WriteString(rowBg.Render(row))
} else {
b.WriteString(normalRow.Render(row))
}
b.WriteString("\n")
}

b.WriteString("\n")
hintStyle := lipgloss.NewStyle().Foreground(colorDim)
sep := lipgloss.NewStyle().Foreground(lipgloss.Color("#3a3f58")).Render(" · ")
k := func(s string) string { return lipgloss.NewStyle().Foreground(colorText).Render(s) }
Expand All @@ -417,11 +434,29 @@ func (m *ConfigModel) render() string {
ke("esc") + hintStyle.Render(" cancel"),
)
}
b.WriteString(hints)
// The settings list scrolls when it is taller than the terminal, keeping the
// row being edited on screen. Without this the lower sections render past
// the bottom edge, where they can be selected but never read.
settings := strings.Split(strings.TrimSuffix(b.String(), "\n"), "\n")
const boxChrome = 4 // border and vertical padding
// Measured at the box's inner width: the hint line wraps on a narrow
// terminal, and an unwrapped measurement would under-reserve its rows.
hintHeight := lipgloss.Height(lipgloss.NewStyle().Width(innerWidth).Render(hints))
errorHeight := 0
if m.validationError != "" {
b.WriteString("\n")
b.WriteString(lipgloss.NewStyle().Foreground(colorCritical).Render(m.validationError))
errorHeight = lipgloss.Height(lipgloss.NewStyle().Width(innerWidth).Render(m.validationError)) + 1
}
visible := windowLines(settings, m.height-boxChrome-hintHeight-errorHeight-1, cursorLine)

var content strings.Builder
content.WriteString(strings.Join(visible, "\n"))
content.WriteString("\n\n")
content.WriteString(hints)
if m.validationError != "" {
content.WriteString("\n")
content.WriteString(lipgloss.NewStyle().Foreground(colorCritical).Width(innerWidth).Render(m.validationError))
}
b = content

box := lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
Expand Down
91 changes: 75 additions & 16 deletions internal/tui/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,65 @@ import (

"github.com/charmbracelet/lipgloss"
"github.com/endrilickollari/debtdrone-cli/v2/internal/models"
"github.com/mattn/go-runewidth"
)

func truncate(s string, maxLen int) string {
if len(s) <= maxLen {
// truncate shortens a value to fit maxWidth terminal columns, marking the cut
// with an ellipsis. It measures display width rather than bytes, so accented
// and full-width characters are counted as the terminal draws them and a
// multi-byte character is never split into invalid output.
func truncate(s string, maxWidth int) string {
if maxWidth <= 0 {
return ""
}
if runewidth.StringWidth(s) <= maxWidth {
return s
}
return s[:maxLen-1] + "…"
if maxWidth <= 1 {
return "…"
}

// One column is reserved for the ellipsis.
var (
builder strings.Builder
width int
)
for _, r := range s {
runeWidth := runewidth.RuneWidth(r)
if width+runeWidth > maxWidth-1 {
break
}
builder.WriteRune(r)
width += runeWidth
}
return builder.String() + "…"
}

// truncateLeft keeps the end of a value visible. It is used for editable input
// when the cursor has moved beyond the right edge of the available space.
func truncateLeft(s string, maxWidth int) string {
if maxWidth <= 0 {
return ""
}
if runewidth.StringWidth(s) <= maxWidth {
return s
}
if maxWidth == 1 {
return "…"
}

runes := []rune(s)
width := 0
start := len(runes)
for start > 0 {
runeWidth := runewidth.RuneWidth(runes[start-1])
if width+runeWidth > maxWidth-1 {
break
}
start--
width += runeWidth
}
return "…" + string(runes[start:])
}

func min(a, b int) int {
Expand Down Expand Up @@ -49,21 +101,26 @@ func splitHeight(totalHeight int) (listH, detailH int) {
// summary band, so the two panes shrink instead of overflowing the viewport.
func splitHeightWithChrome(totalHeight, extraChrome int) (listH, detailH int) {
const chrome = 8
available := totalHeight - chrome - extraChrome
if available < 10 {
available = 10
}
listH = available * 6 / 10
detailH = available - listH
if listH < 4 {
listH = 4
}
if detailH < 3 {
detailH = 3
}
// The panes shrink to whatever the terminal leaves rather than holding a
// comfortable minimum that would push the screen past the bottom edge.
available := max(totalHeight-chrome-extraChrome, 2)
listH = max(available*6/10, 1)
detailH = max(available-listH, 1)
return
}

// hangingValue wraps a value beside its label, indenting every continuation
// line to the value column. Without the indent a wrapped path resumes under the
// labels and reads as though it were one.
func hangingValue(text string, labelWidth, wrapWidth int) string {
wrapped := lipgloss.NewStyle().Foreground(colorText).Width(wrapWidth).Render(text)
lines := strings.Split(wrapped, "\n")
for index := 1; index < len(lines); index++ {
lines[index] = strings.Repeat(" ", labelWidth) + lines[index]
}
return strings.Join(lines, "\n")
}

func countBySeverity(issues []models.TechnicalDebtIssue, sev string) int {
n := 0
for _, iss := range issues {
Expand Down Expand Up @@ -117,7 +174,9 @@ func formatIssueDetail(issue *models.TechnicalDebtIssue, width int) string {

var b strings.Builder
b.WriteString("\n")
b.WriteString(label("Full Path") + lipgloss.NewStyle().Foreground(colorText).Width(wrapW).Render(issue.FilePath) + "\n")
// The full path is wrapped rather than truncated: the findings table shows
// only the file name, so this is where a reader inspects the whole value.
b.WriteString(label("Full Path") + hangingValue(issue.FilePath, labelW, wrapW) + "\n")
b.WriteString(label("Line") + value(lineNum) + "\n")
b.WriteString(label("Severity") + sevRendered + "\n")
b.WriteString(label("Category") + value(issue.Category) + "\n")
Expand Down
Loading
Loading