Skip to content

Commit

Permalink
feat: use tab as the id separator
Browse files Browse the repository at this point in the history
lets us hide the id for some pickers, such as fzf. eg

    fzf --delimiter $'\t' --with-nth 2
  • Loading branch information
sentriz committed Jan 31, 2023
1 parent c2e08d9 commit bf792ca
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 27 deletions.
30 changes: 6 additions & 24 deletions cliphist.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -217,20 +216,11 @@ func list(db *bolt.DB, out io.Writer) error {
return nil
}

var (
decodeID = regexp.MustCompile(`^(?P<id>\d+)\. `)
decodeIDIndex = decodeID.SubexpIndex("id")
)

func extractID(input []byte) (uint64, error) {
if len(input) <= 2 {
return 0, fmt.Errorf("input too short to decode")
}
matches := decodeID.FindSubmatch(input)
if decodeIDIndex >= len(matches) {
idStr, _, found := strings.Cut(string(input), "\t")
if !found {
return 0, fmt.Errorf("input not prefixed with id")
}
idStr := string(matches[decodeIDIndex])
id, err := strconv.Atoi(idStr)
if err != nil {
return 0, fmt.Errorf("converting id: %w", err)
Expand Down Expand Up @@ -280,17 +270,9 @@ func deleteQuery(db *bolt.DB, query string) error {
}

func delete(db *bolt.DB, input []byte) error {
if len(input) <= 2 {
return fmt.Errorf("input too short to decode")
}
matches := decodeID.FindSubmatch(input)
if len(matches) != 2 {
return fmt.Errorf("input not prefixed with id")
}
idStr := string(matches[1])
id, err := strconv.Atoi(idStr)
id, err := extractID(input)
if err != nil {
return fmt.Errorf("converting id: %w", err)
return fmt.Errorf("extract id: %w", err)
}

tx, err := db.Begin(true)
Expand Down Expand Up @@ -375,11 +357,11 @@ func initDBOption(ro bool) (*bolt.DB, error) {
func preview(index uint64, data []byte) string {
data = data[:min(len(data), 100)]
if mime := mime(data); mime != "" {
return fmt.Sprintf("%d. binary data %s", index, mime)
return fmt.Sprintf("%d\tbinary data %s", index, mime)
}
data = bytes.TrimSpace(data)
data = bytes.Join(bytes.Fields(data), []byte(" "))
return fmt.Sprintf("%d. %s", index, data)
return fmt.Sprintf("%d\t%s", index, data)
}

func mime(data []byte) string {
Expand Down
6 changes: 3 additions & 3 deletions cliphist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestDeleteQuery(t *testing.T) {
items := splitn(buff.Bytes())
is.Equal(len(items), 3) // we deleted the bbs
for _, item := range items {
is.True(bytes.Contains(item, []byte(" aa "))) // item is aa
is.True(bytes.Contains(item, []byte("aa"))) // item is aa
}
}

Expand All @@ -123,7 +123,7 @@ func TestDelete(t *testing.T) {
is.NoErr(store(db, []byte("bb hello 4"), maxDedupe, maxStored))
is.NoErr(store(db, []byte("aa hello 5"), maxDedupe, maxStored))

is.NoErr(delete(db, []byte("3. aa hello 3")))
is.NoErr(delete(db, []byte("3\taa hello 3")))

var buff bytes.Buffer
is.NoErr(list(db, &buff))
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestBinary(t *testing.T) {

items := splitn(listBuff.Bytes())
is.Equal(len(items), 1) // we have one image
is.Equal(string(items[0]), "1. binary data image/png") // it looks like a png
is.Equal(string(items[0]), "1\tbinary data image/png") // it looks like a png
}

func initDBt(t *testing.T) *bolt.DB {
Expand Down

0 comments on commit bf792ca

Please sign in to comment.