-
Notifications
You must be signed in to change notification settings - Fork 3
/
highlighter.go
66 lines (52 loc) · 1.33 KB
/
highlighter.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
package helios
import (
"github.com/amenzhinsky/go-memexec"
"reflect"
"strconv"
)
type HighlightRequest struct {
ScreenWidth int `arg:"--screen-width"`
ScreenHeight int `arg:"--screen-height"`
X float64 `arg:"--x"`
Y float64 `arg:"--y"`
Width float64 `arg:"--w"`
Height float64 `arg:"--h"`
Duration float64 `arg:"--d"`
}
func (r HighlightRequest) AsArgsArray() []string {
var a []string
requestReflect := reflect.TypeOf(r)
reflectVal := reflect.ValueOf(r)
numFields := requestReflect.NumField()
i := 0
for i < numFields {
fieldName := requestReflect.Field(i).Tag.Get("arg")
fieldValue := reflectVal.Field(i).Interface()
a = append(a, fieldName)
if val, isInt := fieldValue.(int); isInt {
a = append(a, strconv.Itoa(val))
}
if val, isFloat := fieldValue.(float64); isFloat {
a = append(a, strconv.FormatFloat(val, 'f', -1, 64))
}
i++
}
return a
}
type Highlighter struct {
highlighterBinary []byte
}
func NewHighlighter(highlighterBinary []byte) *Highlighter {
return &Highlighter{
highlighterBinary: highlighterBinary,
}
}
func (h *Highlighter) Highlight(r *HighlightRequest) {
args := r.AsArgsArray()
exe, err := memexec.New(h.highlighterBinary)
if err != nil {
return
}
defer func() { _ = exe.Close() }()
_, _ = exe.Command(args...).Output()
}