-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
129 lines (116 loc) · 2.33 KB
/
cmd.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
package main
import (
"bufio"
"fmt"
"github.com/pkg/errors"
"io"
"os"
"regexp"
"strings"
)
func runCommand() error {
if flags.version {
fmt.Println(gitTag)
return nil
} else if flags.list {
for _, item := range emojiData {
for _, v := range item.Aliases {
fmt.Printf(":%s: %s\n", v, item.Emoji)
}
}
return nil
} else if isInputFromPipe() {
return toEmoji(os.Stdin, os.Stdout)
} else if emojiAliases != "" {
return toEmoji(strings.NewReader(emojiAliases), os.Stdout)
} else {
file, e := getFile()
if e != nil {
return e
}
defer file.Close()
return toEmoji(file, os.Stdout)
}
}
func isInputFromPipe() bool {
fi, _ := os.Stdin.Stat()
return fi.Mode()&os.ModeCharDevice == 0
}
func getFile() (*os.File, error) {
if flags.filepath == "" {
return nil, errors.New("please input a file")
}
if !fileExists(flags.filepath) {
return nil, errors.New("the file provided does not exist")
}
file, e := os.Open(flags.filepath)
if e != nil {
return nil, errors.Wrapf(e,
"unable to read the file %s", flags.filepath)
}
return file, nil
}
func processConvert(s string) string {
re := regexp.MustCompile(`\w+|([+-]\d)`)
parts := strings.Split(s, ":")
Contains := func(s []string, substr string) bool {
for _, v := range s {
if v == substr {
return true
}
}
return false
}
ToEmoji := func(source string) string {
for _, item := range emojiData {
if Contains(item.Aliases, source) {
return item.Emoji
}
}
return source
}
var ret string
prev_state := false
for _, part := range parts {
if len(strings.Fields(part)) == 1 && re.MatchString(part) {
toEmoji := ToEmoji(part)
if toEmoji != part {
ret = ret + toEmoji
prev_state = true
} else {
if prev_state {
ret = ret + part
} else {
ret = ret + ":" + part
}
prev_state = false
}
} else {
if prev_state {
ret = ret + part
} else {
ret = ret + ":" + part
}
prev_state = false
}
}
return ret[1:]
}
func toEmoji(r io.Reader, w io.Writer) error {
scanner := bufio.NewScanner(bufio.NewReader(r))
for scanner.Scan() {
_, e := fmt.Fprintln(
w, processConvert(scanner.Text()))
if e != nil {
return e
}
}
return nil
}
func fileExists(filepath string) bool {
info, e := os.Stat(filepath)
if os.IsNotExist(e) {
return false
}
return !info.IsDir()
}