-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy patheditor.go
145 lines (116 loc) · 2.53 KB
/
editor.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package github
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/jingweno/gh/cmd"
"github.com/jingweno/gh/git"
)
func NewEditor(topic, message string) (editor *Editor, err error) {
messageFile, err := getMessageFile(topic)
if err != nil {
return
}
program, err := git.Editor()
if err != nil {
return
}
editor = &Editor{
Program: program,
File: messageFile,
Message: message,
doEdit: doTextEditorEdit,
}
return
}
type Editor struct {
Program string
File string
Message string
doEdit func(program, file string) error
}
func (e *Editor) Edit() (content []byte, err error) {
if e.Message != "" {
err = ioutil.WriteFile(e.File, []byte(e.Message), 0644)
if err != nil {
return
}
}
defer os.Remove(e.File)
err = e.doEdit(e.Program, e.File)
if err != nil {
err = fmt.Errorf("error using text editor for editing message")
return
}
content, err = ioutil.ReadFile(e.File)
return
}
func (e *Editor) EditTitleAndBody() (title, body string, err error) {
content, err := e.Edit()
if err != nil {
return
}
content = bytes.TrimSpace(content)
reader := bufio.NewReader(bytes.NewReader(content))
title, body, err = readTitleAndBody(reader)
return
}
func doTextEditorEdit(program, file string) error {
editCmd := cmd.New(program)
r := regexp.MustCompile("[mg]?vi[m]$")
if r.MatchString(program) {
editCmd.WithArg("-c")
editCmd.WithArg("set ft=gitcommit tw=0 wrap lbr")
}
editCmd.WithArg(file)
return editCmd.Exec()
}
func readTitleAndBody(reader *bufio.Reader) (title, body string, err error) {
r := regexp.MustCompile("\\S")
var titleParts, bodyParts []string
line, err := readLine(reader)
for err == nil {
if strings.HasPrefix(line, "#") {
break
}
if len(bodyParts) == 0 && r.MatchString(line) {
titleParts = append(titleParts, line)
} else {
bodyParts = append(bodyParts, line)
}
line, err = readLine(reader)
}
if err == io.EOF {
err = nil
}
title = strings.Join(titleParts, " ")
title = strings.TrimSpace(title)
body = strings.Join(bodyParts, "\n")
body = strings.TrimSpace(body)
return
}
func readLine(r *bufio.Reader) (string, error) {
var (
isPrefix = true
err error
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}
return string(ln), err
}
func getMessageFile(about string) (string, error) {
gitDir, err := git.Dir()
if err != nil {
return "", err
}
return filepath.Join(gitDir, fmt.Sprintf("%s_EDITMSG", about)), nil
}