-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
history.go
59 lines (52 loc) · 1.21 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
package history
import (
"bufio"
"crypto/sha256"
"fmt"
"io"
"regexp"
"strings"
"github.com/k1LoW/frgm/snippet"
)
type History struct{}
// New return *History
func New() *History {
return &History{}
}
var trimRe = regexp.MustCompile(`^\s*[0-9*]+\s+`)
func (h *History) Decode(stdin io.Reader, group string) (snippet.Snippets, error) {
snippets := snippet.Snippets{}
in := bufio.NewReader(stdin)
for {
line, err := in.ReadString('\n')
if err == io.EOF {
break
} else if err != nil {
return snippets, err
}
command := strings.TrimRight(trimRe.ReplaceAllString(line, ""), "\n")
if command == "" {
continue
}
name := command
if strings.Count(command, "#") == 1 {
splited := strings.Split(command, "#")
command = strings.TrimSpace(splited[0])
name = strings.TrimSpace(splited[1])
}
uid, err := genUID(line)
if err != nil {
return snippets, err
}
snippets = append(snippets, snippet.New(uid, group, name, command, "", "", []string{}))
}
return snippets, nil
}
func genUID(line string) (string, error) {
h := sha256.New()
if _, err := io.WriteString(h, line); err != nil {
return "", err
}
s := fmt.Sprintf("%x", h.Sum(nil))
return fmt.Sprintf("history-%s", s[:12]), nil
}