Skip to content

Commit

Permalink
fix: clipboard stored as json leading to problem with double quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
abenz1267 committed Mar 23, 2024
1 parent 986b031 commit 15a896b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
4 changes: 2 additions & 2 deletions history/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type History map[string]Entry
type Entry struct {
LastUsed time.Time `json:"last_used,omitempty"`
Used int `json:"used,omitempty"`
DaysSinceUsed int
DaysSinceUsed int `json:"-"`
}

func (s History) Save(entry string) {
Expand All @@ -34,7 +34,7 @@ func (s History) Save(entry string) {

s[entry] = h

util.ToJson(s, filepath.Join(util.CacheDir(), "history.json"))
util.ToJson(&s, filepath.Join(util.CacheDir(), "history.json"))
}

func Get() History {
Expand Down
2 changes: 1 addition & 1 deletion modules/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func parse() []Entry {
entries = append(entries, v.Generic)
}

util.ToJson(entries, filepath.Join(util.CacheDir(), fmt.Sprintf("%s.json", ApplicationsName)))
util.ToJson(&entries, filepath.Join(util.CacheDir(), fmt.Sprintf("%s.json", ApplicationsName)))

return entries
}
10 changes: 5 additions & 5 deletions modules/clipboard/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c Clipboard) Entries(term string) []modules.Entry {

es := []Entry{}

util.FromJson(c.file, &es)
util.FromGob(c.file, &es)

for _, v := range es {
e := modules.Entry{
Expand Down Expand Up @@ -102,7 +102,7 @@ func (c Clipboard) Setup(cfg *config.Config) modules.Workable {

c.prefix = module.Prefix
c.switcherExclusive = module.SwitcherExclusive
c.file = filepath.Join(util.CacheDir(), "clipboard.json")
c.file = filepath.Join(util.CacheDir(), "clipboard.gob")
c.max = cfg.Clipboard.MaxEntries

c.imgTypes = make(map[string]string)
Expand All @@ -111,7 +111,7 @@ func (c Clipboard) Setup(cfg *config.Config) modules.Workable {
c.imgTypes["image/jpeg"] = "jpeg"

current := []Entry{}
util.FromJson(c.file, &current)
util.FromGob(c.file, &current)

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

Expand All @@ -134,7 +134,7 @@ func clean(entries []Entry, file string) []Entry {
}
}

util.ToJson(cleaned, file)
util.ToGob(&cleaned, file)

return cleaned
}
Expand Down Expand Up @@ -235,6 +235,6 @@ func (c *Clipboard) watch() {
c.entries = slices.Clone(c.entries[:c.max])
}

util.ToJson(c.entries, c.file)
util.ToGob(&c.entries, c.file)
}
}
2 changes: 1 addition & 1 deletion ui/interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func setupCommands() {
os.Remove(filepath.Join(util.CacheDir(), "applications.json"))
}
commands["clearclipboard"] = func() {
os.Remove(filepath.Join(util.CacheDir(), "clipboard.json"))
os.Remove(filepath.Join(util.CacheDir(), "clipboard.bson"))
}
}

Expand Down
52 changes: 46 additions & 6 deletions util/file.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,56 @@
package util

import (
"bytes"
"encoding/gob"
"encoding/json"
"io"
"log"
"os"
"path/filepath"
)

func ToJson(src any, dest string) {
err := os.MkdirAll(filepath.Dir(dest), 0755)
func ToGob[T any](val *T, dest string) {
var b bytes.Buffer
encoder := gob.NewEncoder(&b)
if err := encoder.Encode(val); err != nil {
log.Fatalln(err)
}

writeFile(b.Bytes(), dest)
}

func FromGob[T any](src string, dest *T) bool {
if _, err := os.Stat(src); err != nil {
return false
}

file, err := os.Open(src)
if err != nil {
log.Println(err)
return
log.Fatalln(err)
}

b, err := json.Marshal(src)
b, err := io.ReadAll(file)
if err != nil {
log.Panic(err)
}

decoder := gob.NewDecoder(bytes.NewReader(b))
err = decoder.Decode(dest)
if err != nil {
log.Fatalln(err)
}

err = os.WriteFile(dest, b, 0o600)
return true
}

func ToJson[T any](src *T, dest string) {
b, err := json.Marshal(src)
if err != nil {
log.Fatalln(err)
}

writeFile(b, dest)
}

func FromJson[T any](src string, dest *T) bool {
Expand Down Expand Up @@ -70,3 +97,16 @@ func CacheDir() string {

return filepath.Join(dir, "walker")
}

func writeFile(b []byte, dest string) {
err := os.MkdirAll(filepath.Dir(dest), 0755)
if err != nil {
log.Println(err)
return
}

err = os.WriteFile(dest, b, 0o600)
if err != nil {
log.Fatalln(err)
}
}

0 comments on commit 15a896b

Please sign in to comment.