-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
cliphist.go
449 lines (393 loc) · 10.2 KB
/
cliphist.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package main
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"flag"
"fmt"
"image"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
_ "embed"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"go.senan.xyz/flagconf"
_ "golang.org/x/image/bmp"
bolt "go.etcd.io/bbolt"
)
//go:embed version.txt
var version string
//nolint:errcheck
func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "usage:\n")
fmt.Fprintf(flag.CommandLine.Output(), " $ %s <store|list|decode|delete|delete-query|wipe|version>\n", flag.CommandLine.Name())
fmt.Fprintf(flag.CommandLine.Output(), "options:\n")
flag.VisitAll(func(f *flag.Flag) {
fmt.Fprintf(flag.CommandLine.Output(), " -%s (default %s)\n", f.Name, f.DefValue)
fmt.Fprintf(flag.CommandLine.Output(), " %s\n", f.Usage)
})
}
cacheHome, err := os.UserCacheDir()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
configHome, err := os.UserConfigDir()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
maxItems := flag.Uint64("max-items", 750, "maximum number of items to store")
maxDedupeSearch := flag.Uint64("max-dedupe-search", 100, "maximum number of last items to look through when finding duplicates")
previewWidth := flag.Uint("preview-width", 100, "maximum number of characters to preview")
dbPath := flag.String("db-path", filepath.Join(cacheHome, "cliphist", "db"), "path to db")
configPath := flag.String("config-path", filepath.Join(configHome, "cliphist", "config"), "overwrite config path to use instead of cli flags")
flag.Parse()
flagconf.ParseEnv()
flagconf.ParseConfig(*configPath)
switch flag.Arg(0) {
case "store":
switch os.Getenv("CLIPBOARD_STATE") { // from man wl-clipboard
case "sensitive":
case "clear":
err = deleteLast(*dbPath)
default:
err = store(*dbPath, os.Stdin, *maxDedupeSearch, *maxItems)
}
case "list":
err = list(*dbPath, os.Stdout, *previewWidth)
case "decode":
err = decode(*dbPath, os.Stdin, os.Stdout, flag.Arg(1))
case "delete-query":
err = deleteQuery(*dbPath, flag.Arg(1))
case "delete":
err = delete(*dbPath, os.Stdin)
case "wipe":
err = wipe(*dbPath)
case "version":
fmt.Fprintf(flag.CommandLine.Output(), "%s\t%s\n", "version", strings.TrimSpace(version))
flag.VisitAll(func(f *flag.Flag) {
fmt.Fprintf(flag.CommandLine.Output(), "%s\t%s\n", f.Name, f.Value)
})
default:
flag.Usage()
os.Exit(1)
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func store(dbPath string, in io.Reader, maxDedupeSearch, maxItems uint64) error {
input, err := io.ReadAll(in)
if err != nil {
return fmt.Errorf("read stdin: %w", err)
}
if len(input) > 5*1e6 { // don't store >5MB
return nil
}
db, err := initDB(dbPath)
if err != nil {
return fmt.Errorf("opening db: %w", err)
}
defer db.Close()
if len(bytes.TrimSpace(input)) == 0 {
return nil
}
tx, err := db.Begin(true)
if err != nil {
return fmt.Errorf("begin tx: %w", err)
}
defer tx.Rollback() //nolint:errcheck
b := tx.Bucket([]byte(bucketKey))
if err := deduplicate(b, input, maxDedupeSearch); err != nil {
return fmt.Errorf("deduplicating: %w", err)
}
id, err := b.NextSequence()
if err != nil {
return fmt.Errorf("getting next sequence: %w", err)
}
if err := b.Put(itob(id), input); err != nil {
return fmt.Errorf("insert stdin: %w", err)
}
if err := trimLength(b, maxItems); err != nil {
return fmt.Errorf("trimming length: %w", err)
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("commit tx: %w", err)
}
return nil
}
// trim the store's size to a number of max items. manually counting
// seen items because we can't rely on sequence numbers when items can
// be deleted when deduplicating
func trimLength(b *bolt.Bucket, maxItems uint64) error {
c := b.Cursor()
var seen uint64
for k, _ := c.Last(); k != nil; k, _ = c.Prev() {
if seen < maxItems {
seen++
continue
}
if err := b.Delete(k); err != nil {
return fmt.Errorf("delete :%w", err)
}
seen++
}
return nil
}
func deduplicate(b *bolt.Bucket, input []byte, maxDedupeSearch uint64) error {
c := b.Cursor()
var seen uint64
for k, v := c.Last(); k != nil; k, v = c.Prev() {
if seen > maxDedupeSearch {
break
}
if !bytes.Equal(v, input) {
seen++
continue
}
if err := b.Delete(k); err != nil {
return fmt.Errorf("delete :%w", err)
}
seen++
}
return nil
}
func list(dbPath string, out io.Writer, previewWidth uint) error {
db, err := initDBReadOnly(dbPath)
if err != nil {
return fmt.Errorf("opening db: %w", err)
}
defer db.Close()
tx, err := db.Begin(false)
if err != nil {
return fmt.Errorf("begin tx: %w", err)
}
defer tx.Rollback() //nolint:errcheck
b := tx.Bucket([]byte(bucketKey))
c := b.Cursor()
for k, v := c.Last(); k != nil; k, v = c.Prev() {
fmt.Fprintln(out, preview(btoi(k), v, previewWidth))
}
return nil
}
const fieldSep = "\t"
func extractID(input string) (uint64, error) {
idStr, _, _ := strings.Cut(input, fieldSep)
if idStr == "" {
return 0, fmt.Errorf("input not prefixed with id")
}
id, err := strconv.Atoi(idStr)
if err != nil {
return 0, fmt.Errorf("converting id: %w", err)
}
return uint64(id), nil
}
func decode(dbPath string, in io.Reader, out io.Writer, input string) error {
if input == "" {
inp, err := io.ReadAll(in)
if err != nil {
return fmt.Errorf("read stdin: %w", err)
}
input = string(inp)
}
id, err := extractID(input)
if err != nil {
return fmt.Errorf("extracting id: %w", err)
}
db, err := initDBReadOnly(dbPath)
if err != nil {
return fmt.Errorf("opening db: %w", err)
}
defer db.Close()
tx, err := db.Begin(false)
if err != nil {
return fmt.Errorf("begin tx: %w", err)
}
defer tx.Rollback() //nolint:errcheck
b := tx.Bucket([]byte(bucketKey))
v := b.Get(itob(id))
if _, err := out.Write(v); err != nil {
return fmt.Errorf("writing out: %w", err)
}
return nil
}
func deleteQuery(dbPath string, query string) error {
if query == "" {
return fmt.Errorf("please provide a query")
}
db, err := initDB(dbPath)
if err != nil {
return fmt.Errorf("opening db: %w", err)
}
defer db.Close()
tx, err := db.Begin(true)
if err != nil {
return fmt.Errorf("begin tx: %w", err)
}
defer tx.Rollback() //nolint:errcheck
b := tx.Bucket([]byte(bucketKey))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
if bytes.Contains(v, []byte(query)) {
_ = b.Delete(k)
}
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("commit tx: %w", err)
}
return nil
}
func deleteLast(dbPath string) error {
db, err := initDB(dbPath)
if err != nil {
return fmt.Errorf("opening db: %w", err)
}
defer db.Close()
tx, err := db.Begin(true)
if err != nil {
return fmt.Errorf("begin tx: %w", err)
}
defer tx.Rollback() //nolint:errcheck
b := tx.Bucket([]byte(bucketKey))
c := b.Cursor()
k, _ := c.Last()
_ = b.Delete(k)
if err := tx.Commit(); err != nil {
return fmt.Errorf("commit tx: %w", err)
}
return nil
}
func delete(dbPath string, in io.Reader) error {
input, err := io.ReadAll(in) // drain stdin before opening and locking db
if err != nil {
return fmt.Errorf("read stdin: %w", err)
}
db, err := initDB(dbPath)
if err != nil {
return fmt.Errorf("opening db: %w", err)
}
defer db.Close()
tx, err := db.Begin(true)
if err != nil {
return fmt.Errorf("begin tx: %w", err)
}
defer tx.Rollback() //nolint:errcheck
for sc := bufio.NewScanner(bytes.NewReader(input)); sc.Scan(); {
id, err := extractID(sc.Text())
if err != nil {
return fmt.Errorf("extract id: %w", err)
}
b := tx.Bucket([]byte(bucketKey))
if err := b.Delete(itob(id)); err != nil {
return fmt.Errorf("delete key: %w", err)
}
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("commit tx: %w", err)
}
return nil
}
func wipe(dbPath string) error {
db, err := initDB(dbPath)
if err != nil {
return fmt.Errorf("opening db: %w", err)
}
defer db.Close()
tx, err := db.Begin(true)
if err != nil {
return fmt.Errorf("begin tx: %w", err)
}
defer tx.Rollback() //nolint:errcheck
b := tx.Bucket([]byte(bucketKey))
c := b.Cursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() {
_ = b.Delete(k)
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("commit tx: %w", err)
}
return nil
}
const bucketKey = "b"
func initDB(path string) (*bolt.DB, error) { return initDBOption(path, false) }
func initDBReadOnly(path string) (*bolt.DB, error) { return initDBOption(path, true) }
func initDBOption(path string, ro bool) (*bolt.DB, error) {
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
return nil, fmt.Errorf("create cache dir: %w", err)
}
// https://github.com/etcd-io/bbolt/issues/98
if ro {
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
return nil, errors.New("please store something first")
}
}
db, err := bolt.Open(path, 0644, &bolt.Options{
ReadOnly: ro,
Timeout: 1 * time.Second,
})
if err != nil {
return nil, fmt.Errorf("open db: %w", err)
}
if db.IsReadOnly() {
return db, nil
}
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(bucketKey))
return err
})
if err != nil {
return nil, fmt.Errorf("init bucket: %w", err)
}
return db, nil
}
func preview(index uint64, data []byte, width uint) string {
if config, format, err := image.DecodeConfig(bytes.NewReader(data)); err == nil {
return fmt.Sprintf("%d%s[[ binary data %s %s %dx%d ]]",
index, fieldSep, sizeStr(len(data)), format, config.Width, config.Height)
}
prev := string(data)
prev = strings.TrimSpace(prev)
prev = strings.Join(strings.Fields(prev), " ")
prev = trunc(prev, int(width), "…")
return fmt.Sprintf("%d%s%s", index, fieldSep, prev)
}
func trunc(in string, max int, ellip string) string {
runes := []rune(in)
if len(runes) > max {
return string(runes[:max]) + ellip
}
return in
}
func min(a, b int) int { //nolint:unused // we still support go1.19
if a < b {
return a
}
return b
}
func itob(v uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, v)
return b
}
func btoi(v []byte) uint64 {
return binary.BigEndian.Uint64(v)
}
func sizeStr(size int) string {
units := []string{"B", "KiB", "MiB"}
var i int
fsize := float64(size)
for fsize >= 1024 && i < len(units)-1 {
fsize /= 1024
i++
}
return fmt.Sprintf("%.0f %s", fsize, units[i])
}