Skip to content

Commit

Permalink
cmdr: help-screen: fix the right part alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Mar 9, 2025
1 parent 828e16d commit 2e63664
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions cli/worker/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"slices"
"strconv"
"strings"

"github.com/hedzr/evendeep"
Expand Down Expand Up @@ -420,11 +419,13 @@ func (s *helpPrinter) printCommand(ctx context.Context, sb *strings.Builder, ver
}
}

_, _ = sb.WriteString(strings.Repeat(" ", level+groupedInc))
indentSpaces := strings.Repeat(" ", level+groupedInc)
_, _ = sb.WriteString(indentSpaces)

w := tabbedW - (level+groupedInc)*2
ttl := cc.GetTitleNames()
if ttl == "" {
ttl = "(empty)"
ttl = "(not-specified)"
}

if !grouped && group != "" {
Expand All @@ -440,7 +441,20 @@ func (s *helpPrinter) printCommand(ctx context.Context, sb *strings.Builder, ver
return ss
}

left, right := fmt.Sprintf("%-"+strconv.Itoa(w)+"s", ttl), cc.Desc()
if w >= len(ttl) {
w -= len(ttl)
}
if root := cc.Root(); root != nil && root.RedirectTo() == cc.Name() {
var ss strings.Builder
s.Translator.HighlightFast(&ss, ttl)
s.Translator.DimFast(&ss, " <- (root)")
if w >= 10 {
w -= 10
}
ttl = ss.String()
}

left, right := fmt.Sprintf("%s%s", ttl, strings.Repeat(" ", w)), cc.Desc()
dep, depPlain := cc.DeprecatedHelpString(trans, CurrentDeprecatedColor, CurrentDescColor)

if (cc.Hidden() && *verboseCount > 0) || (cc.VendorHidden() && *verboseCount >= 3) { //nolint:revive
Expand Down Expand Up @@ -523,9 +537,10 @@ func (s *helpPrinter) printFlag(ctx context.Context, sb *strings.Builder, verbos
groupedInc++
}

if grouped && group != "" {
if group != s.lastGroup {
s.lastGroup = group
ofs := 0
if group != "" {
if group != s.lastFlagGroup {
s.lastFlagGroup = group
_, _ = sb.WriteString(strings.Repeat(" ", level+groupedInc))
s.WriteColor(sb, CurrentGroupTitleColor)
s.WriteBgColor(sb, CurrentGroupTitleBgColor)
Expand All @@ -536,9 +551,23 @@ func (s *helpPrinter) printFlag(ctx context.Context, sb *strings.Builder, verbos
_, _ = sb.WriteString("\n")
}
groupedInc++
if ff.Required() {
ofs = -1
}
} else if grouped && !ff.OwnerIsNotNil() { // don't apply on a detached flag item
groupedInc++
if ff.Required() {
ofs = -1
}
}

indentSpaces := strings.Repeat(" ", level+groupedInc+ofs)
_, _ = sb.WriteString(indentSpaces)

if ff.Required() {
_, _ = sb.WriteString("* ")
}

_, _ = sb.WriteString(strings.Repeat(" ", level+groupedInc))
// ttl := strings.Join(ff.GetTitleZshFlagNamesArray(), ",")
ttl := ff.GetTitleFlagNamesBy(",")
w := tabbedW - (level+groupedInc)*2 // - len(ttl)
Expand All @@ -556,7 +585,11 @@ func (s *helpPrinter) printFlag(ctx context.Context, sb *strings.Builder, verbos
return ss
}

left, right := fmt.Sprintf("%-"+strconv.Itoa(w)+"s", ttl), ff.Desc()
// left, right := fmt.Sprintf("%-"+strconv.Itoa(w)+"s", ttl), ff.Desc()
if w >= len(ttl) {
w -= len(ttl)
}
left, right := fmt.Sprintf("%s%s", ttl, strings.Repeat(" ", w)), ff.Desc()
tg := ff.ToggleGroupLeadHelpString()
def, defPlain := ff.DefaultValueHelpString(trans, CurrentDefaultValueColor, CurrentDescColor)
dep, depPlain := ff.DeprecatedHelpString(trans, CurrentDeprecatedColor, CurrentDescColor)
Expand Down

0 comments on commit 2e63664

Please sign in to comment.