Skip to content

Commit 4714de1

Browse files
authored
feat: add -preview-width option to limit char width of preview (sentriz#88)
1 parent c9f9e4b commit 4714de1

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

cliphist.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func main() {
4141

4242
maxItems := flag.Uint64("max-items", 750, "maximum number of items to store")
4343
maxDedupeSearch := flag.Uint64("max-dedupe-search", 100, "maximum number of last items to look through when finding duplicates")
44+
previewWidth := flag.Uint("preview-width", 100, "maximum number of characters to preview")
4445
flag.Parse()
4546

4647
var err error
@@ -54,7 +55,7 @@ func main() {
5455
err = store(os.Stdin, *maxDedupeSearch, *maxItems)
5556
}
5657
case "list":
57-
err = list(os.Stdout)
58+
err = list(os.Stdout, *previewWidth)
5859
case "decode":
5960
err = decode(os.Stdin, os.Stdout, flag.Arg(1))
6061
case "delete-query":
@@ -159,7 +160,7 @@ func deduplicate(b *bolt.Bucket, input []byte, maxDedupeSearch uint64) error {
159160
return nil
160161
}
161162

162-
func list(out io.Writer) error {
163+
func list(out io.Writer, previewWidth uint) error {
163164
db, err := initDBReadOnly()
164165
if err != nil {
165166
return fmt.Errorf("opening db: %w", err)
@@ -175,7 +176,7 @@ func list(out io.Writer) error {
175176
b := tx.Bucket([]byte(bucketKey))
176177
c := b.Cursor()
177178
for k, v := c.Last(); k != nil; k, v = c.Prev() {
178-
fmt.Fprintln(out, preview(btoi(k), v))
179+
fmt.Fprintln(out, preview(btoi(k), v, previewWidth))
179180
}
180181
return nil
181182
}
@@ -384,15 +385,15 @@ func initDBOption(ro bool) (*bolt.DB, error) {
384385
return db, nil
385386
}
386387

387-
func preview(index uint64, data []byte) string {
388+
func preview(index uint64, data []byte, width uint) string {
388389
if config, format, err := image.DecodeConfig(bytes.NewReader(data)); err == nil {
389390
return fmt.Sprintf("%d%s[[ binary data %s %s %dx%d ]]",
390391
index, fieldSep, sizeStr(len(data)), format, config.Width, config.Height)
391392
}
392393
prev := string(data)
393394
prev = strings.TrimSpace(prev)
394395
prev = strings.Join(strings.Fields(prev), " ")
395-
prev = trunc(prev, 100, "…")
396+
prev = trunc(prev, int(width), "…")
396397
return fmt.Sprintf("%d%s%s", index, fieldSep, prev)
397398
}
398399

cliphist_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"image/jpeg"
99
"image/png"
1010
"io"
11+
mathrand "math/rand"
1112
"os"
1213
"strconv"
1314
"testing"
@@ -27,6 +28,16 @@ func TestMain(m *testing.M) {
2728
_, _ = io.CopyN(os.Stdout, rand.Reader, int64(size))
2829
return 0
2930
},
31+
"randstr": func() int {
32+
size, _ := strconv.Atoi(os.Args[1])
33+
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
34+
b := make([]byte, size)
35+
for i := range b {
36+
b[i] = letterBytes[mathrand.Intn(len(letterBytes))]
37+
}
38+
os.Stdout.Write(b)
39+
return 0
40+
},
3041

3142
"gif": func() int { _ = gif.Encode(os.Stdout, testImage, nil); return 0 },
3243
"jpg": func() int { _ = jpeg.Encode(os.Stdout, testImage, nil); return 0 },

testdata/preview-width.txtar

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# default width is 100
2+
exec randstr 100
3+
stdin stdout
4+
exec cliphist store
5+
exec cliphist list
6+
! stdout …
7+
8+
exec randstr 101
9+
stdin stdout
10+
exec cliphist store
11+
exec cliphist list
12+
stdout …
13+
14+
exec randstr 1000
15+
stdin stdout
16+
exec cliphist store
17+
exec cliphist -preview-width 1000 list
18+
! stdout …
19+
20+
exec randstr 1001
21+
stdin stdout
22+
exec cliphist store
23+
exec cliphist -preview-width 1000 list
24+
stdout -count=1 …
25+
26+
! exec cliphist -preview-width -10 list
27+
28+
exec cliphist -preview-width 1 list
29+
stdout -count=4 '.\t.…'
30+
31+
exec cliphist -preview-width 0 list
32+
stdout -count=4 '.\t…'

0 commit comments

Comments
 (0)