-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathalfred_main.go
99 lines (87 loc) · 1.79 KB
/
alfred_main.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
package main
import (
"bytes"
"fmt"
"os"
"strconv"
"strings"
"github.com/crispgm/alfred-markdown-table/internal/namesgenerator"
aw "github.com/deanishe/awgo"
"github.com/olekukonko/tablewriter"
)
var (
wf *aw.Workflow
)
func run() {
var (
colNum int = 2
rowNum int = 2
testData bool
)
argc := len(os.Args)
if argc == 1 {
return
}
if argc > 1 {
params := os.Args[1]
realArgs := strings.Split(params, " ")
count := 0
for _, arg := range realArgs {
if arg == "" || arg == " " {
continue
}
val, err := strconv.Atoi(arg)
if err == nil {
if count == 0 {
colNum = val
} else if count == 1 {
rowNum = val
}
} else if count == 2 && (arg == "data" || arg == "test") {
testData = true
} else {
break
}
count++
}
}
wf.NewItem(getSubtitle(colNum, rowNum, testData)).
Arg(buildTable(colNum, rowNum, testData)).
Valid(true)
wf.SendFeedback()
return
}
func getSubtitle(colNum, rowNum int, testData bool) string {
subtitle := fmt.Sprintf("Generate a %dx%d table", colNum, rowNum)
if testData {
subtitle += " with test data"
}
return subtitle
}
func buildTable(colNum, rowNum int, testData bool) string {
buf := new(bytes.Buffer)
table := tablewriter.NewWriter(buf)
table.SetHeader(generateRow(colNum, testData))
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
for idx := 0; idx < rowNum; idx++ {
table.Append(generateRow(colNum, testData))
}
table.Render()
return buf.String()
}
func generateRow(colNum int, testData bool) []string {
row := make([]string, colNum)
for index := range row {
if testData {
row[index] = namesgenerator.GetRandomName(0)
} else {
row[index] = " "
}
}
return row
}
func main() {
wf = aw.New()
wf.Run(run)
}