forked from knqyf263/pet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippet.go
105 lines (87 loc) · 3.04 KB
/
snippet.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
103
104
105
package snippet
import (
"bytes"
"fmt"
"os"
"sort"
"github.com/BurntSushi/toml"
"github.com/knqyf263/pet/config"
)
type Snippets struct {
Snippets []SnippetInfo `toml:"snippets"`
}
type SnippetInfo struct {
Description string `toml:"description"`
Command string `toml:"command"`
Tag []string `toml:"tag"`
Output string `toml:"output"`
}
// Load reads toml file.
func (snippets *Snippets) Load() error {
snippetFile := config.Conf.General.SnippetFile
if _, err := os.Stat(snippetFile); os.IsNotExist(err) {
return nil
}
if _, err := toml.DecodeFile(snippetFile, snippets); err != nil {
return fmt.Errorf("Failed to load snippet file. %v", err)
}
snippets.Order()
return nil
}
// Save saves the snippets to toml file.
func (snippets *Snippets) Save() error {
snippetFile := config.Conf.General.SnippetFile
f, err := os.Create(snippetFile)
defer f.Close()
if err != nil {
return fmt.Errorf("Failed to save snippet file. err: %s", err)
}
return toml.NewEncoder(f).Encode(snippets)
}
// ToString returns the contents of toml file.
func (snippets *Snippets) ToString() (string, error) {
var buffer bytes.Buffer
err := toml.NewEncoder(&buffer).Encode(snippets)
if err != nil {
return "", fmt.Errorf("Failed to convert struct to TOML string: %v", err)
}
return buffer.String(), nil
}
// Order snippets regarding SortBy option defined in config toml
// Prefix "-" reverses the order, default is "recency", "+<expressions>" is the same as "<expression>"
func (snippets *Snippets) Order() {
sortBy := config.Conf.General.SortBy
switch {
case sortBy == "command" || sortBy == "+command":
sort.Sort(ByCommand(snippets.Snippets))
case sortBy == "-command":
sort.Sort(sort.Reverse(ByCommand(snippets.Snippets)))
case sortBy == "description" || sortBy == "+description":
sort.Sort(ByDescription(snippets.Snippets))
case sortBy == "-description":
sort.Sort(sort.Reverse(ByDescription(snippets.Snippets)))
case sortBy == "output" || sortBy == "+output":
sort.Sort(ByOutput(snippets.Snippets))
case sortBy == "-output":
sort.Sort(sort.Reverse(ByOutput(snippets.Snippets)))
case sortBy == "-recency":
snippets.reverse()
}
}
func (snippets *Snippets) reverse() {
for i, j := 0, len(snippets.Snippets)-1; i < j; i, j = i+1, j-1 {
snippets.Snippets[i], snippets.Snippets[j] = snippets.Snippets[j], snippets.Snippets[i]
}
}
type ByCommand []SnippetInfo
func (a ByCommand) Len() int { return len(a) }
func (a ByCommand) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByCommand) Less(i, j int) bool { return a[i].Command > a[j].Command }
type ByDescription []SnippetInfo
func (a ByDescription) Len() int { return len(a) }
func (a ByDescription) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByDescription) Less(i, j int) bool { return a[i].Description > a[j].Description }
type ByOutput []SnippetInfo
func (a ByOutput) Len() int { return len(a) }
func (a ByOutput) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByOutput) Less(i, j int) bool { return a[i].Output > a[j].Output }