-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgmoji.go
70 lines (56 loc) · 1.49 KB
/
gmoji.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
package gmoji
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/fatih/color"
)
const (
// JSONPath is the path to the original gitmojis JSON file.
JSONPath string = "https://raw.githubusercontent.com/carloscuesta/gitmoji/master/src/data/gitmojis.json"
)
// Gmoji represents a gmoji.
type Gmoji struct {
Code string `json:"code"`
Description string `json:"description"`
Emoji string `json:"emoji"`
Entity string `json:"entity"`
Name string `json:"name"`
}
// Gmojis represents a list of Gmojis.
type Gmojis []Gmoji
// Gitmojis represents an original gitmojis.
type Gitmojis struct {
Gitmojis Gmojis `json:"gitmojis"`
}
// NewGmojis returns a new Gmojis.
func NewGmojis(path string) (Gmojis, error) {
g := &Gitmojis{}
if err := g.readJSON(path); err != nil {
return nil, err
}
return g.Gitmojis, nil
}
func (g *Gitmojis) readJSON(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return fmt.Errorf("%s not found; download with 'gmoji init'", path)
}
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
if err := json.Unmarshal(b, &g); err != nil {
return err
}
return nil
}
// Selection returns a formatted list of gmojis.
// FIXME: The width between the emoji and the hyphen is not consistent.
func (g Gmojis) Selection() []string {
var gmojis []string
for _, gmoji := range g {
gmojis = append(gmojis, fmt.Sprintf("%s - %s - %s", gmoji.Emoji, color.BlueString(gmoji.Code), gmoji.Description))
}
return gmojis
}