Skip to content

Commit 26e7fe3

Browse files
committed
Console enhancements
1 parent 8d98ebf commit 26e7fe3

File tree

1 file changed

+72
-29
lines changed

1 file changed

+72
-29
lines changed

main.go

+72-29
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,73 @@ package main
22

33
import (
44
"fmt"
5+
"strings"
56
"time"
67

78
"github.com/gookit/color"
89
"github.com/manifoldco/promptui"
910
)
1011

12+
const (
13+
BubbleSort int = 1
14+
SelectionSort int = 2
15+
InsertionSort int = 3
16+
GnomeSort int = 4
17+
CocktailShakerSort int = 5
18+
CombSort int = 6
19+
OddEvenSort int = 7
20+
)
21+
22+
type options struct {
23+
Name string
24+
Uuid int
25+
}
26+
1127
func main() {
28+
29+
clearConsole()
30+
1231
arr := generateRandomArray(10)
1332
fmt.Println("Initial array:", arr)
1433

34+
algorithms := []options{
35+
{Name: "Bubble Sort", Uuid: 1},
36+
{Name: "Selection Sort", Uuid: 2},
37+
{Name: "Insertion Sort", Uuid: 3},
38+
{Name: "Gnome Sort", Uuid: 4},
39+
{Name: "Cocktail Shaker Sort", Uuid: 5},
40+
{Name: "Comb Sort", Uuid: 6},
41+
{Name: "Odd-Even Sort", Uuid: 7},
42+
}
43+
44+
templates := &promptui.SelectTemplates{
45+
Label: "{{ . }}?",
46+
Active: "\U00002728 {{ .Name | cyan }}",
47+
Inactive: " {{ .Name | cyan }}",
48+
Selected: "\U00002728 {{ .Name | red | cyan }}",
49+
Details: `
50+
--------- Algorithm ----------
51+
{{ "Name:" | faint }} {{ .Name }}`,
52+
}
53+
54+
searcher := func(input string, index int) bool {
55+
algorithm := algorithms[index]
56+
name := strings.Replace(strings.ToLower(algorithm.Name), " ", "", -1)
57+
input = strings.Replace(strings.ToLower(input), " ", "", -1)
58+
59+
return strings.Contains(name, input)
60+
}
61+
1562
algorithmPrompt := promptui.Select{
16-
Label: "Select Sorting Algorithm",
17-
Items: []string{
18-
"Bubble Sort",
19-
"Selection Sort",
20-
"Insertion Sort",
21-
"Gnome Sort",
22-
"Cocktail Shaker Sort",
23-
"Comb Sort",
24-
"Odd-Even Sort",
25-
},
63+
Label: "Select a sorting algorithm!",
64+
Items: algorithms,
65+
Templates: templates,
66+
Size: 4,
67+
Searcher: searcher,
2668
}
2769

28-
_, algorithm, err := algorithmPrompt.Run()
70+
i, _, err := algorithmPrompt.Run()
71+
2972
if err != nil {
3073
fmt.Printf("Prompt failed %v\n", err)
3174
return
@@ -46,7 +89,7 @@ func main() {
4689

4790
switch action {
4891
case "Description":
49-
printAlgorithmDescription(algorithm)
92+
printAlgorithmDescription(algorithms[i].Uuid)
5093
case "Run":
5194
speedPrompt := promptui.Select{
5295
Label: "Select Visualization Speed",
@@ -75,52 +118,52 @@ func main() {
75118

76119
clearConsole()
77120

78-
runAlgorithm(algorithm, arr, delay)
121+
runAlgorithm(algorithms[i].Uuid, arr, delay)
79122
default:
80123
fmt.Println("Invalid choice")
81124
return
82125
}
83126
}
84127

85-
func printAlgorithmDescription(algorithm string) {
128+
func printAlgorithmDescription(algorithm int) {
86129
switch algorithm {
87-
case "Bubble Sort":
130+
case BubbleSort:
88131
color.Bold.Println(color.Yellow.Sprint("Bubble Sort"))
89132
fmt.Println()
90133
fmt.Println(color.Cyan.Sprint("Bubble Sort is a simple comparison-based sorting algorithm. It works by repeatedly stepping through the list to be sorted,"))
91134
fmt.Println(color.Cyan.Sprint("compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list"))
92135
fmt.Println(color.Cyan.Sprint("is sorted. The algorithm gets its name from the way smaller elements 'bubble' to the top of the list."))
93-
case "Selection Sort":
136+
case SelectionSort:
94137
color.Bold.Println(color.Yellow.Sprint("Selection Sort"))
95138
fmt.Println()
96139
fmt.Println(color.Cyan.Sprint("Selection Sort is an in-place comparison-based sorting algorithm. It works by dividing the input list into two parts:"))
97140
fmt.Println(color.Cyan.Sprint("the sorted part and the unsorted part. The algorithm repeatedly selects the minimum element from the unsorted part"))
98141
fmt.Println(color.Cyan.Sprint("and moves it to the end of the sorted part. This process continues until the entire list is sorted."))
99-
case "Insertion Sort":
142+
case InsertionSort:
100143
color.Bold.Println(color.Yellow.Sprint("Insertion Sort"))
101144
fmt.Println()
102145
fmt.Println(color.Cyan.Sprint("Insertion Sort is an in-place comparison-based sorting algorithm. It works by dividing the input list into two parts:"))
103146
fmt.Println(color.Cyan.Sprint("the sorted part and the unsorted part. The algorithm repeatedly picks an element from the unsorted part and inserts"))
104147
fmt.Println(color.Cyan.Sprint("it into the correct position in the sorted part. This process continues until the entire list is sorted."))
105-
case "Gnome Sort":
148+
case GnomeSort:
106149
color.Bold.Println(color.Yellow.Sprint("Gnome Sort"))
107150
fmt.Println()
108151
fmt.Println(color.Cyan.Sprint("Gnome Sort is an in-place comparison-based sorting algorithm. It works by repeatedly comparing adjacent elements."))
109152
fmt.Println(color.Cyan.Sprint("If the two elements are in the wrong order, it swaps them and moves one step backward. If the two elements"))
110153
fmt.Println(color.Cyan.Sprint("are in the correct order, it moves one step forward. This process continues until the entire list is sorted."))
111-
case "Cocktail Shaker Sort":
154+
case CocktailShakerSort:
112155
color.Bold.Println(color.Yellow.Sprint("Cocktail Shaker Sort"))
113156
fmt.Println()
114157
fmt.Println(color.Cyan.Sprint("Cocktail Shaker Sort, also known as Bidirectional Bubble Sort, is a variation of Bubble Sort. It works by repeatedly"))
115158
fmt.Println(color.Cyan.Sprint("sorting the list in both directions, first from the beginning to the end (like Bubble Sort), and then from the end"))
116159
fmt.Println(color.Cyan.Sprint("to the beginning. The algorithm stops when the list becomes sorted."))
117-
case "Comb Sort":
160+
case CombSort:
118161
color.Bold.Println(color.Yellow.Sprint("Comb Sort"))
119162
fmt.Println()
120163
fmt.Println(color.Cyan.Sprint("Comb Sort is an in-place comparison-based sorting algorithm. It works by dividing the input list into a series of"))
121164
fmt.Println(color.Cyan.Sprint("gaps and repeatedly sorting the list with a specific shrink factor. The shrink factor reduces the gaps until it becomes 1."))
122165
fmt.Println(color.Cyan.Sprint("At this point, the algorithm behaves similar to Bubble Sort. Comb Sort is an improvement over Bubble Sort for large lists."))
123-
case "Odd-Even Sort":
166+
case OddEvenSort:
124167
color.Bold.Println(color.Yellow.Sprint("Odd-Even Sort"))
125168
fmt.Println()
126169
fmt.Println(color.Cyan.Sprint("Odd-Even Sort is an in-place comparison-based sorting algorithm. It works by repeatedly comparing and swapping"))
@@ -131,21 +174,21 @@ func printAlgorithmDescription(algorithm string) {
131174
}
132175
}
133176

134-
func runAlgorithm(algorithm string, arr []int, delay time.Duration) {
177+
func runAlgorithm(algorithm int, arr []int, delay time.Duration) {
135178
switch algorithm {
136-
case "Bubble Sort":
179+
case BubbleSort:
137180
bubbleSortVisualizer(arr, delay)
138-
case "Selection Sort":
181+
case SelectionSort:
139182
selectionSortVisualizer(arr, delay)
140-
case "Insertion Sort":
183+
case InsertionSort:
141184
insertionSortVisualizer(arr, delay)
142-
case "Gnome Sort":
185+
case GnomeSort:
143186
gnomeSortVisualizer(arr, delay)
144-
case "Cocktail Shaker Sort":
187+
case CocktailShakerSort:
145188
cocktailShakerSortVisualizer(arr, delay)
146-
case "Comb Sort":
189+
case CombSort:
147190
combSortVisualizer(arr, delay)
148-
case "Odd-Even Sort":
191+
case OddEvenSort:
149192
oddEvenSortVisualizer(arr, delay)
150193
default:
151194
fmt.Println("Invalid selection")

0 commit comments

Comments
 (0)