Skip to content

Commit

Permalink
Merge pull request #7 from sanriodev/5-favorites-selection-menu-and-c…
Browse files Browse the repository at this point in the history
…lipboard

feat: allow for copy from favorites
  • Loading branch information
sanriodev authored Nov 20, 2024
2 parents e9e278b + 3258678 commit fa343cb
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 10 deletions.
6 changes: 3 additions & 3 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
brews:
- repository:
owner: sanriodev
name: goji-brewtap
name: homebrew-goji
branch: brew-releases/{{ .Version }}
# token: '{{ .Env.TOKEN }}'
commit_author:
name: goreleaserbot
email: goreleaserbot@example.com
name: Matteo Juen
email: matteo.juen@ematric.com
folder: Formula
homepage: 'https://github.com/sanriodev/goji'
description: 'goji binaries for brew.'
Expand Down
57 changes: 54 additions & 3 deletions cmd/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func ShowMainMenu() {
return
}
defer keyboard.Close()
menuOptions := []string{"Create New Emoji", "Create Random Emoji", "List Favorites", "Exit"}
menuOptions := []string{"Create New Emoji", "Create Random Emoji", "Copy from Favorites", "Exit"}
selectedIndex := 0
startIndex := 0

Expand Down Expand Up @@ -67,7 +67,7 @@ func ShowMainMenu() {
case 1:
emoji.CreateRandomEmoji()
case 2:
listFavorites()
PickFavorite()
os.Exit(0)
case 3:
util.PrintRed("Exiting...")
Expand All @@ -81,7 +81,7 @@ func ShowMainMenu() {
}
}

func listFavorites() {
/* func listFavorites() {
favorites := util.LoadFavorites()
if len(favorites.Emojis) == 0 {
Expand All @@ -94,4 +94,55 @@ func listFavorites() {
emojiStr := fmt.Sprintf("%d: %s", i+1, emoji.Content)
util.PrintBlue(emojiStr)
}
} */

func PickFavorite() {
favorites := util.LoadFavorites()

if len(favorites.Emojis) == 0 {
fmt.Println("No favorite emojis saved yet!")
return
}

selectedIndex := 0
startIndex := 0

for {
fmt.Println("Pick a favorite emoji to copy:")
emoji.DisplayOptions(util.GetFavoriteContents(favorites), selectedIndex, startIndex)

// Capture user input
char, key, err := keyboard.GetKey()
if err != nil {
fmt.Println("Error reading key:", err, char)
break
}

switch key {
case keyboard.KeyArrowUp:
if selectedIndex > 0 {
selectedIndex--
if selectedIndex < startIndex {
startIndex--
}
}
case keyboard.KeyArrowDown:
if selectedIndex < len(favorites.Emojis)-1 {
selectedIndex++
if selectedIndex >= startIndex+maxVisibleOptions {
startIndex++
}
}
case keyboard.KeyEnter:
emoji := favorites.Emojis[selectedIndex].Content
util.CopyToClipboard(emoji, true)
return
case keyboard.KeyEsc:
fmt.Println("Exiting favorites menu.")
return
}

// Clear terminal after each key press
fmt.Print("\033[H\033[2J")
}
}
4 changes: 2 additions & 2 deletions emoji/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func CreateCustomEmoji() {
emoji := fmt.Sprintf("%s %s %s %s %s %s %s", leftArm, "(", leftEye, mouth, rightEye, ")", rightArm)
fmt.Println("Your custom emoji:", emoji)

util.CopyToClipboard(emoji)
util.CopyToClipboard(emoji, false)
}

func CreateRandomEmoji() {
Expand All @@ -45,7 +45,7 @@ func CreateRandomEmoji() {
fmt.Print("\033[H\033[2J")

fmt.Println("Random emoji generated:", emoji)
util.CopyToClipboard(emoji)
util.CopyToClipboard(emoji, false)
}

func DisplayOptions(options []string, selectedIndex, startIndex int) {
Expand Down
4 changes: 2 additions & 2 deletions util/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/atotto/clipboard"
)

func CopyToClipboard(emoji string) {
func CopyToClipboard(emoji string, cannotReSave bool) {
fmt.Println("Do you want to copy this emoji to the clipboard? (y/n/save)")

var input string
Expand All @@ -19,7 +19,7 @@ func CopyToClipboard(emoji string) {
} else {
fmt.Println("Emoji copied to clipboard!")
}
} else if input == "save" {
} else if input == "save" && !cannotReSave {
addFavorite(emoji)
fmt.Println("Emoji saved to favorites!")
} else {
Expand Down
8 changes: 8 additions & 0 deletions util/favorites.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ func LoadFavorites() definitions.Favorites {

return favorites
}

func GetFavoriteContents(favorites definitions.Favorites) []string {
contents := make([]string, len(favorites.Emojis))
for i, emoji := range favorites.Emojis {
contents[i] = emoji.Content
}
return contents
}

0 comments on commit fa343cb

Please sign in to comment.