Skip to content

Commit

Permalink
feat: clear last entry if we get clear from CLIPBOARD_STATE
Browse files Browse the repository at this point in the history
fixes #58
  • Loading branch information
sentriz committed Sep 29, 2023
1 parent 702f082 commit 5764b03
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
34 changes: 29 additions & 5 deletions cliphist.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ func main_() int {
var err error
switch flags.Arg(0) {
case "store":
// from man wl-clipboard
switch os.Getenv("CLIPBOARD_STATE") {
switch x := os.Getenv("CLIPBOARD_STATE"); x { // from man wl-clipboard
case "sensitive":
return 0
case "clear":
err = deleteLast()
default:
err = store(os.Stdin, *maxDedupeSearch, *maxItems)
}

err = store(os.Stdin, *maxDedupeSearch, *maxItems)
case "list":
err = list(os.Stdout)
case "decode":
Expand Down Expand Up @@ -261,6 +261,30 @@ func deleteQuery(query string) error {
return nil
}

func deleteLast() error {
db, err := initDB()
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(in io.Reader) error {
input, err := io.ReadAll(in)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions testdata/clipboard-state-env-clear.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
stdin in-1
exec cliphist store
stdin in-2
exec cliphist store
stdin in-3
exec cliphist store

exec cliphist list
cmp stdout out-before

env CLIPBOARD_STATE=clear
exec cliphist store

exec cliphist list
cmp stdout out-after

-- in-1 --
test input 1

-- in-2 --
test input 2

-- in-3 --
test input 3

-- out-before --
3 test input 3
2 test input 2
1 test input 1
-- out-after --
2 test input 2
1 test input 1
File renamed without changes.

0 comments on commit 5764b03

Please sign in to comment.