Skip to content

Commit c198e32

Browse files
committed
feat: Enhance output formatting with dynamic section boxes
- Introduced a new function `printSectionBox` to create dynamically-sized boxes around section names in the output, ensuring a minimum width for better readability. - Updated the `printSectionChanges` function to utilize the new box formatting, improving the visual structure of section headers. - Corrected error variable naming for consistency in argument validation within the `runVersionsDiff` function.
1 parent 2e875f1 commit c198e32

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

cmd/diff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,11 @@ func runServerDiff(cmd *cobra.Command, args []string) error {
351351
// runVersionsDiff compares the configuration between two Aerospike server versions.
352352
func runVersionsDiff(cmd *cobra.Command, args []string) error {
353353
if len(args) < diffVersionsArgMin {
354-
return errSchemaDiffTWrongArgs
354+
return errSchemaDiffWrongArgs
355355
}
356356

357357
if len(args) > diffVersionsArgMax {
358-
return errSchemaDiffTWrongArgs
358+
return errSchemaDiffWrongArgs
359359
}
360360

361361
version1 := args[0]

cmd/diff_utils.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ func extractSection(path string, validSections map[string]bool) string {
201201
// Output formatting
202202
// ---------------
203203

204+
const (
205+
// minBoxWidth defines the minimum width for the section box.
206+
minBoxWidth = 50
207+
)
208+
204209
// printChangeSummary prints a formatted summary of schema changes.
205210
func printChangeSummary(summary ChangeSummary, options DiffOptions) {
206211
// Print header with versions
@@ -247,9 +252,7 @@ func printHeader(summary ChangeSummary, options DiffOptions) {
247252
func printSectionChanges(section string, changes SectionChanges, options DiffOptions) {
248253
// Section header
249254
if options.Verbose {
250-
fmt.Fprintf(os.Stdout, "\n╭─────────────────────────────────────────────────────────────╮\n")
251-
fmt.Fprintf(os.Stdout, "│ SECTION: %-50s │\n", strings.ToUpper(section))
252-
fmt.Fprintf(os.Stdout, "╰─────────────────────────────────────────────────────────────╯\n")
255+
printSectionBox(section)
253256
} else {
254257
fmt.Fprintf(os.Stdout, "\n[SECTION: %s]\n", strings.ToUpper(section))
255258
}
@@ -485,6 +488,40 @@ func formatValue(value interface{}) string {
485488
// Utility functions
486489
// ---------------
487490

491+
// printSectionBox prints a dynamically-sized box around the section name.
492+
// The box width adjusts to accommodate the section name while maintaining a minimum width.
493+
func printSectionBox(section string) {
494+
upperSection := strings.ToUpper(section)
495+
sectionText := "SECTION: " + upperSection
496+
497+
// Calculate the required width (text + padding)
498+
const padding = 2 // 1 space on each side
499+
requiredWidth := len(sectionText) + padding
500+
501+
// Use the larger of minimum width or required width
502+
boxWidth := minBoxWidth
503+
if requiredWidth > minBoxWidth {
504+
boxWidth = requiredWidth
505+
}
506+
507+
// Calculate padding for centering the text
508+
totalPadding := boxWidth - len(sectionText)
509+
leftPadding := totalPadding / padding
510+
rightPadding := totalPadding - leftPadding
511+
512+
// Create the box borders
513+
topBorder := "╭" + strings.Repeat("─", boxWidth) + "╮"
514+
bottomBorder := "╰" + strings.Repeat("─", boxWidth) + "╯"
515+
516+
// Print the box
517+
fmt.Fprintf(os.Stdout, "\n%s\n", topBorder)
518+
fmt.Fprintf(os.Stdout, "│%s%s%s│\n",
519+
strings.Repeat(" ", leftPadding),
520+
sectionText,
521+
strings.Repeat(" ", rightPadding))
522+
fmt.Fprintf(os.Stdout, "%s\n", bottomBorder)
523+
}
524+
488525
// formatPath formats a JSON path into a more human-readable form.
489526
func formatPath(path string) string {
490527
parts := strings.Split(path, "/")

cmd/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var (
5252
)
5353

5454
// Schema diff errors.
55-
errSchemaDiffTWrongArgs = errors.New("diff versions requires exactly 2 version arguments")
55+
errSchemaDiffWrongArgs = errors.New("diff versions requires exactly 2 version arguments")
5656
errInvalidSchemaVersion = errors.New("invalid schema version")
5757

5858
errUnableToGenerateConfigFile = errors.New("unable to generate config file")

0 commit comments

Comments
 (0)