-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitmoji.go
178 lines (144 loc) · 3.25 KB
/
gitmoji.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/log"
)
type Emoji struct {
Emoji string `json:"emoji"`
Code string `json:"code"`
Description string `json:"description"`
Name string `json:"name"`
}
type Response struct {
Gitmojis []Emoji `json:"gitmojis"`
}
func isCached(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
func fetchFromCache(path string) ([]Emoji, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var response Response
err = json.NewDecoder(file).Decode(&response)
if err != nil {
return nil, err
}
return response.Gitmojis, nil
}
func writeToCache(path string, response Response) error {
directory := filepath.Dir(path)
if _, err := os.Stat(directory); os.IsNotExist(err) {
err := os.MkdirAll(directory, os.ModePerm)
if err != nil {
return err
}
}
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
err = json.NewEncoder(file).Encode(&response)
if err != nil {
return err
}
return nil
}
type Gitmoji struct {
notation string
}
func NewGitmoji(notation string) *Gitmoji {
return &Gitmoji{notation}
}
func (g *Gitmoji) fetch() ([]Emoji, error) {
dirname, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
path := filepath.Join(dirname, ".config", "cmt", "gitmojis.json")
if isCached(path) {
return fetchFromCache(path)
}
res, err := http.Get("https://gitmoji.dev/api/gitmojis")
if err != nil {
return nil, err
}
defer res.Body.Close()
var response Response
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
return nil, err
}
if err := writeToCache(path, response); err != nil {
return nil, err
}
return response.Gitmojis, nil
}
// Remove zero-width joiners (U+200D) from emojis since they may not render properly in terminals
func parseEmoji(emoji Emoji) string {
runes := []rune(emoji.Emoji)
result := make([]rune, 0, len(runes))
for _, r := range runes {
if r != '\u200D' {
result = append(result, r)
}
}
return string(result)
}
func (g *Gitmoji) ask() (string, error) {
emojis, err := g.fetch()
if err != nil {
return "", err
}
options := make([]huh.Option[string], 0, len(emojis))
for _, e := range emojis {
optionText := fmt.Sprintf("%s - %s", parseEmoji(e), e.Description)
optionValue := e.Emoji
if g.notation != "emoji" {
optionValue = e.Code
}
options = append(options, huh.NewOption(optionText, optionValue))
}
var emoji string
if err := huh.NewSelect[string]().
Title("Select the type of commit").
Options(options...).
Value(&emoji).
Filtering(true).
Height(10).
Validate(func(val string) error {
if val == "" {
return errors.New("type cannot be empty")
}
return nil
}).Run(); err != nil {
return "", err
}
return emoji, nil
}
func (g *Gitmoji) Construct() (string, error) {
gitmoji, err := g.ask()
if err != nil {
return "", err
}
message, err := promptForMessage()
if err != nil {
return "", err
}
return fmt.Sprintf("%s %s", gitmoji, message), nil
}
func (g *Gitmoji) Type() ConventionType {
return GitmojiConvention
}
var _ Provider = (*Gitmoji)(nil)