Skip to content

Commit

Permalink
feat: add -preview-width option to limit char width of preview (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtvrsh authored Feb 23, 2024
1 parent c9f9e4b commit 4714de1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
11 changes: 6 additions & 5 deletions cliphist.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func main() {

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")
flag.Parse()

var err error
Expand All @@ -54,7 +55,7 @@ func main() {
err = store(os.Stdin, *maxDedupeSearch, *maxItems)
}
case "list":
err = list(os.Stdout)
err = list(os.Stdout, *previewWidth)
case "decode":
err = decode(os.Stdin, os.Stdout, flag.Arg(1))
case "delete-query":
Expand Down Expand Up @@ -159,7 +160,7 @@ func deduplicate(b *bolt.Bucket, input []byte, maxDedupeSearch uint64) error {
return nil
}

func list(out io.Writer) error {
func list(out io.Writer, previewWidth uint) error {
db, err := initDBReadOnly()
if err != nil {
return fmt.Errorf("opening db: %w", err)
Expand All @@ -175,7 +176,7 @@ func list(out io.Writer) error {
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))
fmt.Fprintln(out, preview(btoi(k), v, previewWidth))
}
return nil
}
Expand Down Expand Up @@ -384,15 +385,15 @@ func initDBOption(ro bool) (*bolt.DB, error) {
return db, nil
}

func preview(index uint64, data []byte) string {
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, 100, "…")
prev = trunc(prev, int(width), "…")
return fmt.Sprintf("%d%s%s", index, fieldSep, prev)
}

Expand Down
11 changes: 11 additions & 0 deletions cliphist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"image/jpeg"
"image/png"
"io"
mathrand "math/rand"
"os"
"strconv"
"testing"
Expand All @@ -27,6 +28,16 @@ func TestMain(m *testing.M) {
_, _ = io.CopyN(os.Stdout, rand.Reader, int64(size))
return 0
},
"randstr": func() int {
size, _ := strconv.Atoi(os.Args[1])
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, size)
for i := range b {
b[i] = letterBytes[mathrand.Intn(len(letterBytes))]
}
os.Stdout.Write(b)
return 0
},

"gif": func() int { _ = gif.Encode(os.Stdout, testImage, nil); return 0 },
"jpg": func() int { _ = jpeg.Encode(os.Stdout, testImage, nil); return 0 },
Expand Down
32 changes: 32 additions & 0 deletions testdata/preview-width.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# default width is 100
exec randstr 100
stdin stdout
exec cliphist store
exec cliphist list
! stdout …

exec randstr 101
stdin stdout
exec cliphist store
exec cliphist list
stdout …

exec randstr 1000
stdin stdout
exec cliphist store
exec cliphist -preview-width 1000 list
! stdout …

exec randstr 1001
stdin stdout
exec cliphist store
exec cliphist -preview-width 1000 list
stdout -count=1 …

! exec cliphist -preview-width -10 list

exec cliphist -preview-width 1 list
stdout -count=4 '.\t.…'

exec cliphist -preview-width 0 list
stdout -count=4 '.\t…'

0 comments on commit 4714de1

Please sign in to comment.