Skip to content

Commit

Permalink
clipboard: added always_put_new_on_top option, default: true
Browse files Browse the repository at this point in the history
  • Loading branch information
abenz1267 committed Jan 19, 2025
1 parent 00b353d commit 1e95c90
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cmd/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.12.7-git
0.12.7
1 change: 1 addition & 0 deletions internal/config/config.default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ placeholder = "Windows"
show_icon_when_single = true

[builtins.clipboard]
always_put_new_on_top = true
exec = "wl-copy"
weight = 5
name = "clipboard"
Expand Down
11 changes: 6 additions & 5 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,12 @@ type ActivationMode struct {
}

type Clipboard struct {
GeneralModule `koanf:",squash"`
AvoidLineBreaks bool `koanf:"avoid_line_breaks"`
ImageHeight int `koanf:"image_height"`
MaxEntries int `koanf:"max_entries"`
Exec string `koanf:"exec"`
GeneralModule `koanf:",squash"`
AvoidLineBreaks bool `koanf:"avoid_line_breaks"`
ImageHeight int `koanf:"image_height"`
MaxEntries int `koanf:"max_entries"`
Exec string `koanf:"exec"`
AlwaysPutNewOnTop bool `koanf:"always_put_new_on_top"`
}

type Dmenu struct {
Expand Down
76 changes: 58 additions & 18 deletions internal/modules/clipboard/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ func (c *Clipboard) SetupData() {
current := []ClipboardItem{}
_ = util.FromGob(c.file, &current)

go c.watch()

c.items = clean(current, c.file)

for _, v := range c.items {
c.entries = append(c.entries, itemToEntry(v, c.exec, c.avoidLineBreaks))
}

go c.watch()

c.general.IsSetup = true
c.general.HasInitialSetup = true
}
Expand Down Expand Up @@ -227,32 +227,72 @@ func (c *Clipboard) watch() {
hash := md5.Sum([]byte(content))
strgHash := hex.EncodeToString(hash[:])

if c.exists(strgHash) {
exists := c.exists(strgHash)

if exists && !config.Cfg.Builtins.Clipboard.AlwaysPutNewOnTop {
continue
}

if len(content) < 2 {
continue
}

mimetype := getType()
if exists && c.items[0].HashIdent != strgHash {
for k, v := range c.items {
if v.HashIdent == strgHash {
c.items[k].Time = time.Now()
}
}

e := ClipboardItem{
Content: content,
Time: time.Now(),
Hash: strgHash,
IsImg: false,
HashIdent: strgHash,
}
for k, v := range c.entries {
if v.HashIdent == strgHash {
c.entries[k].LastUsed = time.Now()
}
}

if val, ok := c.imgTypes[mimetype]; ok {
file := saveTmpImg(val)
e.Content = file
e.IsImg = true
}
slices.SortFunc(c.items, func(a, b ClipboardItem) int {
if a.Time.After(b.Time) {
return -1
}

c.entries = append([]util.Entry{itemToEntry(e, c.exec, c.avoidLineBreaks)}, c.entries...)
c.items = append([]ClipboardItem{e}, c.items...)
if a.Time.Before(b.Time) {
return 1
}

return 0
})

slices.SortFunc(c.entries, func(a, b util.Entry) int {
if a.LastUsed.After(b.LastUsed) {
return -1
}

if a.LastUsed.Before(b.LastUsed) {
return 1
}

return 0
})
} else if !exists {
mimetype := getType()

e := ClipboardItem{
Content: content,
Time: time.Now(),
Hash: strgHash,
IsImg: false,
HashIdent: strgHash,
}

if val, ok := c.imgTypes[mimetype]; ok {
file := saveTmpImg(val)
e.Content = file
e.IsImg = true
}

c.entries = append([]util.Entry{itemToEntry(e, c.exec, c.avoidLineBreaks)}, c.entries...)
c.items = append([]ClipboardItem{e}, c.items...)
}

hstry := history.Get()

Expand Down

0 comments on commit 1e95c90

Please sign in to comment.