File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package history
22
33import (
4+ "bytes"
45 "encoding/json"
56 "os"
67 "path/filepath"
8+ "slices"
79 "strings"
10+
11+ "github.com/natefinch/atomic"
812)
913
1014type History struct {
@@ -54,15 +58,10 @@ func New(opts ...Opt) (*History, error) {
5458
5559func (h * History ) Add (message string ) error {
5660 // Add the message last but avoid duplicate messages
57- var messages []string
58- for _ , msg := range h .Messages {
59- if msg != message {
60- messages = append (messages , msg )
61- }
62- }
63- messages = append (messages , message )
64-
65- h .Messages = messages
61+ h .Messages = slices .DeleteFunc (h .Messages , func (m string ) bool {
62+ return m == message
63+ })
64+ h .Messages = append (h .Messages , message )
6665 h .current = len (h .Messages )
6766
6867 return h .save ()
@@ -131,7 +130,7 @@ func (h *History) save() error {
131130 return err
132131 }
133132
134- return os .WriteFile (h .path , data , 0o644 )
133+ return atomic .WriteFile (h .path , bytes . NewReader ( data ) )
135134}
136135
137136func (h * History ) load () error {
You can’t perform that action at this time.
0 commit comments