-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.go
134 lines (116 loc) · 2.93 KB
/
match.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
package espanso
import (
"strings"
)
// Match represents a single match for espanso.
type Match struct {
imagePath string
propagateCase bool
replace string
trigger string
word bool
}
// NewMatch is generating a new match.
func NewMatch() Match {
return Match{}
}
// SetImagePath is setting the image_path value for a match.
func (m *Match) SetImagePath(p string) *Match {
m.imagePath = p
return m
}
// SetPropagateCase is setting the propagate_case value for a match.
func (m *Match) SetPropagateCase(p bool) *Match {
m.propagateCase = p
return m
}
// SetTrigger is setting the trigger value for a match.
func (m *Match) SetTrigger(t string) *Match {
m.trigger = t
return m
}
// SetReplace is setting the replace value for a match.
func (m *Match) SetReplace(r string) *Match {
m.replace = r
return m
}
// SetWord is setting the word value for a match.
func (m *Match) SetWord(w bool) *Match {
m.word = w
return m
}
// ImagePath returns the imagePath value for a match.
func (m *Match) ImagePath() string {
return m.imagePath
}
// PropagateCase returns the propagateCase value for a match.
func (m *Match) PropagateCase() bool {
return m.propagateCase
}
// Trigger returns the trigger value for a match.
func (m *Match) Trigger() string {
return toRaw(m.trigger)
}
// Replace returns the replace value for a match.
func (m *Match) Replace() string {
return toRaw(m.replace)
}
// Word returns the word value for a match.
func (m *Match) Word() bool {
return m.word
}
// Matches represents multiple matches for espanso.
type Matches []Match
// SetPropagateCase sets the propagateCase value for multiple matches.
func (m Matches) SetPropagateCase(p bool) Matches {
var matches Matches
for _, match := range m {
nm := NewMatch()
nm.SetImagePath(match.ImagePath())
nm.SetPropagateCase(p)
nm.SetReplace(match.Replace())
nm.SetTrigger(match.Trigger())
nm.SetWord(match.Word())
matches = append(matches, nm)
}
return matches
}
// SetWord sets the word value for multiple matches.
func (m Matches) SetWord(w bool) Matches {
var matches Matches
for _, match := range m {
nm := NewMatch()
nm.SetImagePath(match.ImagePath())
nm.SetPropagateCase(match.PropagateCase())
nm.SetTrigger(match.Trigger())
nm.SetReplace(match.Replace())
nm.SetWord(w)
matches = append(matches, nm)
}
return matches
}
// DictToMatches is converting a dict with the format of
// var dict = []string{
// "trigger", "replace",
// }
// to Matches.
func DictToMatches(dict []string) Matches {
var matches Matches
for i := 0; i < len(dict); i += 2 {
match := NewMatch()
match.SetTrigger(dict[i])
match.SetReplace(dict[i+1])
matches = append(matches, match)
}
return matches
}
// toRaw is generating pseudo "raw string literals".
func toRaw(s string) string {
if strings.Contains(s, "\n") {
s = strings.Replace(s, "\n", "\\n", -1)
}
if strings.Contains(s, "\"") {
s = strings.Replace(s, "\"", "\\\"", -1)
}
return s
}