Skip to content

Commit

Permalink
feat: scroll effect and more choices
Browse files Browse the repository at this point in the history
  • Loading branch information
sanriodev committed Sep 25, 2024
1 parent 162c325 commit 90c8f94
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
)

// Define available characters for each part of the emoji
var leftArms = []string{"٩", "∿", "☝", "<"}
var rightArms = []string{"۶", "∿", "☝", ">"}
var eyes = []string{"o", "O", "^", "◕", "•", "°", "ʘ"}
var mouths = []string{"O", "‿", ".̫ ", "⊖", "ω", "ʖ"}
var leftArms = []string{"٩", "∿", "☝", "<", "ヽ", "(", "へ", "ᕕ", "ノ"}
var rightArms = []string{"۶", "∿", "☝", ">", "ノ", ")", "へ", "ᕗ", "ノ"}
var eyes = []string{"o", "O", "^", "◕", "•", "°", "ʘ", "ʕ", "ಠ", "눈"}
var mouths = []string{"O", "‿", ".̫ ", "⊖", "ω", "ʖ", "﹏", "▽", "益"}

const maxVisibleOptions = 6

func main() {
var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -46,6 +48,8 @@ func showMainMenu() {
menuOptions := []string{"Create New Emoji", "Create Random Emoji", "Exit"}

selectedIndex := 0
startIndex := 0

for {
printBue(`
___ ___
Expand All @@ -61,7 +65,7 @@ func showMainMenu() {
\/__/ \/__/ \/__/ \/__/
`)
fmt.Println("Select an option:")
displayOptions(menuOptions, selectedIndex)
displayOptions(menuOptions, selectedIndex, startIndex)

// Read the keyboard input
char, key, err := keyboard.GetKey()
Expand All @@ -75,10 +79,16 @@ func showMainMenu() {
case keyboard.KeyArrowUp:
if selectedIndex > 0 {
selectedIndex--
if selectedIndex < startIndex {
startIndex--
}
}
case keyboard.KeyArrowDown:
if selectedIndex < len(menuOptions)-1 {
selectedIndex++
if selectedIndex >= startIndex+maxVisibleOptions {
startIndex++
}
}
case keyboard.KeyEnter:
fmt.Print("\033[H\033[2J")
Expand Down Expand Up @@ -139,11 +149,12 @@ func createRandomEmoji() {

func pickPart(options []string, message string) string {
selectedIndex := 0
startIndex := 0

for {
fmt.Println(message)
displayOptions(options, selectedIndex)

displayOptions(options, selectedIndex, startIndex)
printBue("move up or down to display more choices")
char, key, err := keyboard.GetKey()
if err != nil {
fmt.Println("Error reading key and char:", err, char)
Expand All @@ -154,10 +165,16 @@ func pickPart(options []string, message string) string {
case keyboard.KeyArrowUp:
if selectedIndex > 0 {
selectedIndex--
if selectedIndex < startIndex {
startIndex--
}
}
case keyboard.KeyArrowDown:
if selectedIndex < len(options)-1 {
selectedIndex++
if selectedIndex >= startIndex+maxVisibleOptions {
startIndex++
}
}
case keyboard.KeyEnter:
fmt.Print("\033[H\033[2J")
Expand All @@ -173,12 +190,18 @@ func pickPart(options []string, message string) string {
return options[selectedIndex]
}

func displayOptions(options []string, selectedIndex int) {
for i, option := range options {
// displayOptions shows a window of maxVisibleOptions at a time
func displayOptions(options []string, selectedIndex, startIndex int) {
endIndex := startIndex + maxVisibleOptions
if endIndex > len(options) {
endIndex = len(options)
}

for i := startIndex; i < endIndex; i++ {
if i == selectedIndex {
printSelected("> %s\n", option)
printSelected("> %s\n", options[i])
} else {
fmt.Printf(" %s\n", option)
fmt.Printf(" %s\n", options[i])
}
}
}
Expand Down

0 comments on commit 90c8f94

Please sign in to comment.