-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathhistory.go
102 lines (86 loc) · 1.83 KB
/
history.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Package history manages the list of recently played games
package history
import (
"bufio"
"encoding/csv"
"io"
"log"
"os"
"path/filepath"
"github.com/adrg/xdg"
)
// Game represents a game in the history file
type Game struct {
Path string // Absolute path of the game on the filesystem
Name string // Human readable name of the game, comes from the RDB
System string // Name of the game console
CorePath string // Absolute path to the libretro core
Savestate string // Absolute path of the last savestate on this game
}
// History is a list of games
type History []Game
// List is the list of recently played games
var List History
// Push pushes a game onto the stack
func Push(g Game) {
List = append([]Game{g}, List...)
// Deduplicate
l := History{}
exist := map[string]bool{}
for _, g := range List {
if !exist[g.Path] {
l = append(l, g)
exist[g.Path] = true
}
}
List = l
err := Save()
if err != nil {
log.Println(err)
}
}
// Load loads history.csv in memory
func Load() error {
file, err := os.Open(filepath.Join(xdg.DataHome, "ludo", "history.csv"))
if err != nil {
return err
}
defer file.Close()
wr := csv.NewReader(bufio.NewReader(file))
List = History{}
for {
record, err := wr.Read()
if err == io.EOF {
break
}
if err != nil {
return err
}
List = append(List, Game{
Path: record[0],
Name: record[1],
System: record[2],
CorePath: record[3],
})
}
return nil
}
// Save persists the history as a csv file
func Save() error {
file, err := os.Create(filepath.Join(xdg.DataHome, "ludo", "history.csv"))
if err != nil {
return err
}
defer file.Close()
wr := csv.NewWriter(bufio.NewWriter(file))
defer wr.Flush()
for _, game := range List {
wr.Write([]string{
game.Path,
game.Name,
game.System,
game.CorePath,
})
}
return nil
}