From d98cdf5303176d97e3c1ddf52176c127917125a9 Mon Sep 17 00:00:00 2001 From: Aaron Johnon Date: Mon, 2 Sep 2024 01:26:57 -0500 Subject: [PATCH] Added a function to make output multiline --- nsencode.go | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/nsencode.go b/nsencode.go index e303af0..55cb2fb 100644 --- a/nsencode.go +++ b/nsencode.go @@ -31,8 +31,8 @@ func applyShift(value, shift int) int { return shifted } -// Function to format the output based on group size -func formatOutput(encoded []int, groupSize int) string { +// Function to format the output based on group size and line size +func formatOutput(encoded []int, groupSize int, lineSize int) string { var output strings.Builder for _, num := range encoded { output.WriteString(fmt.Sprintf("%02d", num)) @@ -40,25 +40,34 @@ func formatOutput(encoded []int, groupSize int) string { // Apply grouping finalOutput := output.String() - if groupSize > 0 { - var groupedOutput strings.Builder - for i := 0; i < len(finalOutput); i += groupSize { - if i+groupSize < len(finalOutput) { - groupedOutput.WriteString(finalOutput[i:i+groupSize] + " ") - } else { - groupedOutput.WriteString(finalOutput[i:]) - } + var groupedOutput strings.Builder + counter := 0 + groupCounter := 0 + + for i := 0; i < len(finalOutput); i += groupSize { + if i+groupSize < len(finalOutput) { + groupedOutput.WriteString(finalOutput[i:i+groupSize] + " ") + } else { + groupedOutput.WriteString(finalOutput[i:]) + } + counter++ + groupCounter++ + + // Check if we reached the line size + if lineSize > 0 && groupCounter >= lineSize && i+groupSize < len(finalOutput) { + groupedOutput.WriteString("\n") + groupCounter = 0 } - return strings.TrimSpace(groupedOutput.String()) } - return finalOutput + return strings.TrimSpace(groupedOutput.String()) } func main() { // Define command-line flags shift := flag.Int("s", 0, "Shift amount for the cipher") groupSize := flag.Int("g", 0, "Group size for output formatting") + lineSize := flag.Int("l", 0, "Line size by number of groups") flag.Parse() // Read input from stdin @@ -82,7 +91,7 @@ func main() { } // Format the output - formattedOutput := formatOutput(encoded, *groupSize) + formattedOutput := formatOutput(encoded, *groupSize, *lineSize) fmt.Println(formattedOutput) }