Skip to content

Commit 9f226f8

Browse files
committed
Enhancing the print method
1 parent 9a764d0 commit 9f226f8

File tree

1 file changed

+8
-48
lines changed

1 file changed

+8
-48
lines changed

utils.go

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"os"
77
"os/exec"
88
"runtime"
9-
"strings"
109
"time"
1110

1211
"github.com/gookit/color"
@@ -44,56 +43,17 @@ func clearConsole() {
4443
}
4544

4645
func printColoredArray(c Color, array []int, idx1, idx2 int) {
47-
var firstSlice string
48-
var secondSlice string
49-
var thirdSlice string
50-
51-
if len(array[:idx1]) > 0 {
52-
firstSlice = strings.Replace(fmt.Sprintf("%v ", array[:idx1]), "[", "", -1)
53-
firstSlice = strings.Replace(firstSlice, "]", "", -1)
54-
}
55-
if idx1 != idx2 && len(array[idx1+1:idx2]) > 0 {
56-
secondSlice = strings.Replace(fmt.Sprintf("%v ", array[idx1+1:idx2]), "[", "", -1)
57-
secondSlice = strings.Replace(secondSlice, "]", "", -1)
58-
}
59-
if len(array[idx2+1:]) > 0 {
60-
thirdSlice = strings.Replace(fmt.Sprintf(" %v", array[idx2+1:]), "[", "", -1)
61-
thirdSlice = strings.Replace(thirdSlice, "]", "", -1)
62-
}
63-
64-
var str string
65-
switch c {
66-
case LightYellow:
67-
str = firstSlice +
68-
color.OpUnderscore.Sprint(color.LightYellow.Sprint(array[idx1])) +
69-
" " +
70-
secondSlice +
71-
color.OpUnderscore.Sprint(color.LightYellow.Sprint(array[idx2])) +
72-
thirdSlice
73-
case LightBlue:
74-
if idx1 != idx2 {
75-
str = firstSlice +
76-
color.OpUnderscore.Sprint(color.LightBlue.Sprint(array[idx1])) +
77-
" " +
78-
secondSlice +
79-
color.OpUnderscore.Sprint(color.LightBlue.Sprint(array[idx2])) +
80-
thirdSlice
46+
fmt.Print("[")
47+
for i, _ := range array {
48+
if idx1 == i {
49+
fmt.Print(color.OpUnderscore.Sprint(color.LightYellow.Sprint(array[idx1]), " "))
50+
} else if idx2 == i {
51+
fmt.Print(color.OpUnderscore.Sprint(color.LightYellow.Sprint(array[idx2]), " "))
8152
} else {
82-
str = firstSlice +
83-
color.OpUnderscore.Sprint(color.LightBlue.Sprint(array[idx1])) +
84-
" " +
85-
thirdSlice
53+
fmt.Print(color.OpUnderscore.Sprint(array[i], " "))
8654
}
87-
case White:
88-
str = firstSlice +
89-
color.OpUnderscore.Sprint(array[idx1]) +
90-
" " +
91-
secondSlice +
92-
color.OpUnderscore.Sprint(array[idx2]) +
93-
thirdSlice
9455
}
95-
96-
fmt.Printf("[ %v ]", str)
56+
fmt.Println("]")
9757
}
9858

9959
func visualizeIteration(c Color, array []int, idx1, idx2 int, delay time.Duration) {

0 commit comments

Comments
 (0)