Skip to content

Commit 76ef16a

Browse files
authored
Merge pull request #2 from go-nerds/console-enhancements
Console enhancements
2 parents 8d98ebf + a07db0d commit 76ef16a

File tree

2 files changed

+113
-71
lines changed

2 files changed

+113
-71
lines changed

main.go

+64-71
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,72 @@ package main
22

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

7-
"github.com/gookit/color"
88
"github.com/manifoldco/promptui"
99
)
1010

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

33+
algorithms := []options{
34+
{Name: "Bubble Sort", Uuid: 1},
35+
{Name: "Selection Sort", Uuid: 2},
36+
{Name: "Insertion Sort", Uuid: 3},
37+
{Name: "Gnome Sort", Uuid: 4},
38+
{Name: "Cocktail Shaker Sort", Uuid: 5},
39+
{Name: "Comb Sort", Uuid: 6},
40+
{Name: "Odd-Even Sort", Uuid: 7},
41+
}
42+
43+
templates := &promptui.SelectTemplates{
44+
Label: "{{ . }}?",
45+
Active: "\U00002728 {{ .Name | cyan }}",
46+
Inactive: " {{ .Name | cyan }}",
47+
Selected: "\U00002728 {{ .Name | red | cyan }}",
48+
Details: `
49+
--------- Algorithm ----------
50+
{{ "Name:" | faint }} {{ .Name }}`,
51+
}
52+
53+
searcher := func(input string, index int) bool {
54+
algorithm := algorithms[index]
55+
name := strings.Replace(strings.ToLower(algorithm.Name), " ", "", -1)
56+
input = strings.Replace(strings.ToLower(input), " ", "", -1)
57+
58+
return strings.Contains(name, input)
59+
}
60+
1561
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-
},
62+
Label: "Select a sorting algorithm!",
63+
Items: algorithms,
64+
Templates: templates,
65+
Size: 4,
66+
Searcher: searcher,
2667
}
2768

28-
_, algorithm, err := algorithmPrompt.Run()
69+
i, _, err := algorithmPrompt.Run()
70+
2971
if err != nil {
3072
fmt.Printf("Prompt failed %v\n", err)
3173
return
@@ -46,7 +88,7 @@ func main() {
4688

4789
switch action {
4890
case "Description":
49-
printAlgorithmDescription(algorithm)
91+
printAlgorithmDescription(algorithms[i].Uuid)
5092
case "Run":
5193
speedPrompt := promptui.Select{
5294
Label: "Select Visualization Speed",
@@ -75,77 +117,28 @@ func main() {
75117

76118
clearConsole()
77119

78-
runAlgorithm(algorithm, arr, delay)
120+
runAlgorithm(algorithms[i].Uuid, arr, delay)
79121
default:
80122
fmt.Println("Invalid choice")
81123
return
82124
}
83125
}
84126

85-
func printAlgorithmDescription(algorithm string) {
86-
switch algorithm {
87-
case "Bubble Sort":
88-
color.Bold.Println(color.Yellow.Sprint("Bubble Sort"))
89-
fmt.Println()
90-
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,"))
91-
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"))
92-
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":
94-
color.Bold.Println(color.Yellow.Sprint("Selection Sort"))
95-
fmt.Println()
96-
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:"))
97-
fmt.Println(color.Cyan.Sprint("the sorted part and the unsorted part. The algorithm repeatedly selects the minimum element from the unsorted part"))
98-
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":
100-
color.Bold.Println(color.Yellow.Sprint("Insertion Sort"))
101-
fmt.Println()
102-
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:"))
103-
fmt.Println(color.Cyan.Sprint("the sorted part and the unsorted part. The algorithm repeatedly picks an element from the unsorted part and inserts"))
104-
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":
106-
color.Bold.Println(color.Yellow.Sprint("Gnome Sort"))
107-
fmt.Println()
108-
fmt.Println(color.Cyan.Sprint("Gnome Sort is an in-place comparison-based sorting algorithm. It works by repeatedly comparing adjacent elements."))
109-
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"))
110-
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":
112-
color.Bold.Println(color.Yellow.Sprint("Cocktail Shaker Sort"))
113-
fmt.Println()
114-
fmt.Println(color.Cyan.Sprint("Cocktail Shaker Sort, also known as Bidirectional Bubble Sort, is a variation of Bubble Sort. It works by repeatedly"))
115-
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"))
116-
fmt.Println(color.Cyan.Sprint("to the beginning. The algorithm stops when the list becomes sorted."))
117-
case "Comb Sort":
118-
color.Bold.Println(color.Yellow.Sprint("Comb Sort"))
119-
fmt.Println()
120-
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"))
121-
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."))
122-
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":
124-
color.Bold.Println(color.Yellow.Sprint("Odd-Even Sort"))
125-
fmt.Println()
126-
fmt.Println(color.Cyan.Sprint("Odd-Even Sort is an in-place comparison-based sorting algorithm. It works by repeatedly comparing and swapping"))
127-
fmt.Println(color.Cyan.Sprint("adjacent elements at even and odd indices. The process continues until the list is sorted. Odd-Even Sort is known"))
128-
fmt.Println(color.Cyan.Sprint("for its simplicity but is not very efficient for large lists."))
129-
default:
130-
fmt.Println("Invalid selection")
131-
}
132-
}
133-
134-
func runAlgorithm(algorithm string, arr []int, delay time.Duration) {
127+
func runAlgorithm(algorithm int, arr []int, delay time.Duration) {
135128
switch algorithm {
136-
case "Bubble Sort":
129+
case BubbleSort:
137130
bubbleSortVisualizer(arr, delay)
138-
case "Selection Sort":
131+
case SelectionSort:
139132
selectionSortVisualizer(arr, delay)
140-
case "Insertion Sort":
133+
case InsertionSort:
141134
insertionSortVisualizer(arr, delay)
142-
case "Gnome Sort":
135+
case GnomeSort:
143136
gnomeSortVisualizer(arr, delay)
144-
case "Cocktail Shaker Sort":
137+
case CocktailShakerSort:
145138
cocktailShakerSortVisualizer(arr, delay)
146-
case "Comb Sort":
139+
case CombSort:
147140
combSortVisualizer(arr, delay)
148-
case "Odd-Even Sort":
141+
case OddEvenSort:
149142
oddEvenSortVisualizer(arr, delay)
150143
default:
151144
fmt.Println("Invalid selection")

utils.go

+49
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,52 @@ func visualizeIteration(c Color, array []int, idx1, idx2 int, delay time.Duratio
101101
time.Sleep(delay)
102102
clearConsole()
103103
}
104+
105+
func printAlgorithmDescription(algorithm int) {
106+
switch algorithm {
107+
case BubbleSort:
108+
color.Bold.Println(color.Yellow.Sprint("Bubble Sort"))
109+
fmt.Println()
110+
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,"))
111+
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"))
112+
fmt.Println(color.Cyan.Sprint("is sorted. The algorithm gets its name from the way smaller elements 'bubble' to the top of the list."))
113+
case SelectionSort:
114+
color.Bold.Println(color.Yellow.Sprint("Selection Sort"))
115+
fmt.Println()
116+
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:"))
117+
fmt.Println(color.Cyan.Sprint("the sorted part and the unsorted part. The algorithm repeatedly selects the minimum element from the unsorted part"))
118+
fmt.Println(color.Cyan.Sprint("and moves it to the end of the sorted part. This process continues until the entire list is sorted."))
119+
case InsertionSort:
120+
color.Bold.Println(color.Yellow.Sprint("Insertion Sort"))
121+
fmt.Println()
122+
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:"))
123+
fmt.Println(color.Cyan.Sprint("the sorted part and the unsorted part. The algorithm repeatedly picks an element from the unsorted part and inserts"))
124+
fmt.Println(color.Cyan.Sprint("it into the correct position in the sorted part. This process continues until the entire list is sorted."))
125+
case GnomeSort:
126+
color.Bold.Println(color.Yellow.Sprint("Gnome Sort"))
127+
fmt.Println()
128+
fmt.Println(color.Cyan.Sprint("Gnome Sort is an in-place comparison-based sorting algorithm. It works by repeatedly comparing adjacent elements."))
129+
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"))
130+
fmt.Println(color.Cyan.Sprint("are in the correct order, it moves one step forward. This process continues until the entire list is sorted."))
131+
case CocktailShakerSort:
132+
color.Bold.Println(color.Yellow.Sprint("Cocktail Shaker Sort"))
133+
fmt.Println()
134+
fmt.Println(color.Cyan.Sprint("Cocktail Shaker Sort, also known as Bidirectional Bubble Sort, is a variation of Bubble Sort. It works by repeatedly"))
135+
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"))
136+
fmt.Println(color.Cyan.Sprint("to the beginning. The algorithm stops when the list becomes sorted."))
137+
case CombSort:
138+
color.Bold.Println(color.Yellow.Sprint("Comb Sort"))
139+
fmt.Println()
140+
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"))
141+
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."))
142+
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."))
143+
case OddEvenSort:
144+
color.Bold.Println(color.Yellow.Sprint("Odd-Even Sort"))
145+
fmt.Println()
146+
fmt.Println(color.Cyan.Sprint("Odd-Even Sort is an in-place comparison-based sorting algorithm. It works by repeatedly comparing and swapping"))
147+
fmt.Println(color.Cyan.Sprint("adjacent elements at even and odd indices. The process continues until the list is sorted. Odd-Even Sort is known"))
148+
fmt.Println(color.Cyan.Sprint("for its simplicity but is not very efficient for large lists."))
149+
default:
150+
fmt.Println("Invalid selection")
151+
}
152+
}

0 commit comments

Comments
 (0)