From 194b29b6a347bff02dca21d6d621dbd0444577d8 Mon Sep 17 00:00:00 2001 From: xypwn <54681180+xypwn@users.noreply.github.com> Date: Tue, 10 Jun 2025 21:40:12 +0200 Subject: [PATCH 1/3] Fix formatHelpRow --- utils.go | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/utils.go b/utils.go index 086cde9..84269a5 100644 --- a/utils.go +++ b/utils.go @@ -1,7 +1,6 @@ package argparse import ( - "fmt" "os" "strings" @@ -18,23 +17,31 @@ func decideTerminalWidth() int { func formatHelpRow(head, content string, bareHeadLength, maxHeadLength, terminalWidth int, withBreak bool) string { content = strings.Replace(content, "\n", "", -1) - result := fmt.Sprintf(" %s ", head) - headLeftPadding := maxHeadLength - bareHeadLength - 3 - if headLeftPadding > 0 { // fill left padding - result += strings.Repeat(" ", headLeftPadding) - } - contentPadding := strings.Repeat(" ", maxHeadLength) + + head = " " + head + " " // head content + bareHeadLength += 3 // head length without control chars + + // length of a single content row, constant + contentRowLen := terminalWidth - maxHeadLength + var rows []string - if withBreak && headLeftPadding < 0 { - rows = append(rows, result, contentPadding+content) + if withBreak { + rows = append(rows, head) } else { - rows = append(rows, result+content) + // no break -> head is on the same row + // as first content line + headRowPadding := strings.Repeat(" ", maxHeadLength-bareHeadLength) + rowLen := min(contentRowLen, len(content)) + rows = append(rows, head+headRowPadding+content[:rowLen]) + content = content[rowLen:] } - for len(rows[len(rows)-1]) > terminalWidth { // break into lines - lastIndex := len(rows) - 1 - lastOne := rows[lastIndex] - rows[lastIndex] = rows[lastIndex][0:terminalWidth] - rows = append(rows, contentPadding+lastOne[terminalWidth:]) + + rowPadding := strings.Repeat(" ", maxHeadLength) + for content != "" { + rowLen := min(contentRowLen, len(content)) + rows = append(rows, rowPadding+content[:rowLen]) + content = content[rowLen:] } + return strings.Join(rows, "\n") } From 6cdcca8d2c0ae0045884f980d27fe7f4ad401053 Mon Sep 17 00:00:00 2001 From: xypwn <54681180+xypwn@users.noreply.github.com> Date: Wed, 11 Jun 2025 20:13:28 +0200 Subject: [PATCH 2/3] Fix behavior, make tests pass --- utils.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/utils.go b/utils.go index 84269a5..a8663d5 100644 --- a/utils.go +++ b/utils.go @@ -25,12 +25,15 @@ func formatHelpRow(head, content string, bareHeadLength, maxHeadLength, terminal contentRowLen := terminalWidth - maxHeadLength var rows []string - if withBreak { + if withBreak && maxHeadLength < bareHeadLength { rows = append(rows, head) } else { // no break -> head is on the same row // as first content line - headRowPadding := strings.Repeat(" ", maxHeadLength-bareHeadLength) + var headRowPadding string + if maxHeadLength > bareHeadLength { + headRowPadding = strings.Repeat(" ", maxHeadLength-bareHeadLength) + } rowLen := min(contentRowLen, len(content)) rows = append(rows, head+headRowPadding+content[:rowLen]) content = content[rowLen:] From 2dfb002f1ad97edca11e5b32738005a18a9c0102 Mon Sep 17 00:00:00 2001 From: xypwn <54681180+xypwn@users.noreply.github.com> Date: Wed, 11 Jun 2025 21:08:08 +0200 Subject: [PATCH 3/3] Declare min function for better compatibility --- utils.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/utils.go b/utils.go index a8663d5..1dbc9db 100644 --- a/utils.go +++ b/utils.go @@ -24,6 +24,14 @@ func formatHelpRow(head, content string, bareHeadLength, maxHeadLength, terminal // length of a single content row, constant contentRowLen := terminalWidth - maxHeadLength + min := func(x, y int) int { + if x < y { + return x + } else { + return y + } + } + var rows []string if withBreak && maxHeadLength < bareHeadLength { rows = append(rows, head)