-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
179 lines (152 loc) · 4.03 KB
/
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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright © 2015-2017 Daniele Tricoli <[email protected]>.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main // import "eriol.xyz/piken"
import (
"fmt"
"os"
"path"
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/atotto/clipboard"
"github.com/codegangsta/cli"
"eriol.xyz/piken/format"
"eriol.xyz/piken/sql"
)
const (
unicodeDataUrl = "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt"
pikenHome = ".piken"
defaultDatabaseFile = "piken.sqlite3"
defaultDataFile = "UnicodeData.txt"
version = "0.2"
)
var (
baseDir = path.Join(getHome(), pikenHome)
databaseFile = path.Join(baseDir, defaultDatabaseFile)
dataFile = path.Join(baseDir, defaultDataFile)
store sql.Store
)
func main() {
app := cli.NewApp()
app.Name = "piken"
app.Version = version
app.Author = "Daniele Tricoli"
app.Email = "[email protected]"
app.Usage = "unicode search tool backed by SQLite3"
if _, err := os.Stat(baseDir); os.IsNotExist(err) {
os.Mkdir(baseDir, 0755)
}
if err := store.Open(databaseFile); err != nil {
logrus.Fatal(err)
}
defer store.Close()
app.Commands = []cli.Command{
{
Name: "update",
Usage: "Update unicode data",
Action: func(c *cli.Context) {
modifiedTime, err := checkLastModified(unicodeDataUrl)
if err != nil {
logrus.Fatal(err)
}
lastUpdate, err := store.GetLastUpdate(defaultDataFile)
if err != nil {
logrus.Fatal(err)
}
if lastUpdate.Before(modifiedTime) {
download(unicodeDataUrl, dataFile)
records, err := readCsvFile(dataFile)
if err != nil {
logrus.Fatal(err)
}
if err := store.DeleteUnicodeData(); err != nil {
logrus.Fatal(err)
}
if err := store.LoadFromRecords(records); err != nil {
logrus.Fatal(err)
}
if lastUpdate == time.Unix(0, 0) {
if err := store.CreateLastUpdate(defaultDataFile,
modifiedTime); err != nil {
logrus.Fatal(err)
}
} else {
if err := store.UpdateLastUpdate(defaultDataFile,
modifiedTime); err != nil {
logrus.Fatal(err)
}
}
} else {
logrus.Info("Already up to date.")
}
},
},
{
Name: "search",
Aliases: []string{"s"},
Usage: "Search for unicode",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "copy, c",
Usage: "copy glyph to clipboard",
},
cli.BoolTFlag{
Name: "show-glyph",
Usage: "show glyph (defaults to true, use --show-glyph=false to disable)",
},
cli.StringFlag{
Name: "separator",
Value: " -- ",
Usage: "separator for unicode fields",
},
cli.StringSliceFlag{
Name: "fields",
Value: &cli.StringSlice{"CodePoint", "Name"},
Usage: "unicode fields",
},
},
Action: func(c *cli.Context) {
args := strings.Join(c.Args(), " ")
rows, err := store.SearchUnicode(args)
if err != nil {
logrus.Fatal(err)
}
fields := c.StringSlice("fields")
// StringSliceFlag default value can't be overridden for now:
// https://github.com/codegangsta/cli/issues/160
if c.IsSet("fields") {
fields = fields[2:]
// TODO: validate fields
}
formatter := format.NewTextFormatter(
fields,
c.String("separator"),
c.Bool("show-glyph"))
if c.Bool("copy") && len(rows) > 1 {
logrus.Warn("Copy to clipboard not allowed for multiple rows.")
}
for _, row := range rows {
b, err := formatter.Format(&row)
if err != nil {
logrus.Fatal(err)
}
fmt.Println(b)
// Copy to clipboard only when one row is returned by search.
if c.Bool("copy") && len(rows) == 1 {
glyph, err := format.CodePointToGlyph(row.CodePoint)
if err != nil {
logrus.Fatalf("Impossible to convert %s to glyph.",
row.CodePoint)
}
if err := clipboard.WriteAll(glyph); err != nil {
logrus.Fatalf("Copy to clipboard failed: %v", err)
}
}
}
},
},
}
app.Run(os.Args)
}