-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcontent.go
54 lines (43 loc) · 1.17 KB
/
content.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
package extedit
import (
"bufio"
"io"
"os"
"strings"
)
// Content represents a the in- and output of an extedit session.
type Content struct {
c []string
reader io.Reader
}
func (c Content) Read(b []byte) (int, error) {
return c.reader.Read(b)
}
func (c Content) String() string {
return strings.Join(c.c, "\n") // <- FIXME: join string needs to come from splitfunc somehow
}
func (c Content) Length() int {
return len(c.c)
}
// contentFromReader creates a new Content object by scanning an io.Reader using a bufio.SplitFunc
func contentFromReader(content io.Reader, split bufio.SplitFunc) (Content, error) {
c := Content{}
scanner := bufio.NewScanner(content)
scanner.Split(split)
for scanner.Scan() {
c.c = append(c.c, scanner.Text())
}
c.reader = strings.NewReader(c.String())
return c, scanner.Err()
}
func contentFromFile(filename string, split bufio.SplitFunc) (Content, error) {
file, err := os.Open(filename)
if err != nil {
return Content{}, err
}
defer file.Close()
return contentFromReader(file, split)
}
func contentFromString(content string, split bufio.SplitFunc) (Content, error) {
return contentFromReader(strings.NewReader(content), split)
}