-
Notifications
You must be signed in to change notification settings - Fork 6
/
writer.go
64 lines (56 loc) · 1.63 KB
/
writer.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
package main
import (
"bufio"
"io"
)
type Translator interface {
FileNameIndex() string
FileNameChannel(channelName string) string
FileNameHistory(channelName string, chunkNumber int) string
ToHeading(level int, text string) []string
ToChannelList(channels []Channel) []string
ToChunkList(chunks []ChunkInfo) []string
ToMessageList(chunk []MessageResolved) []string
ToUserTable(users []User) []string
}
type TranslatingWriter struct {
translator Translator
writer *bufio.Writer
}
func NewTranslatingWriter(translator Translator, writer io.Writer) *TranslatingWriter {
w := new(TranslatingWriter)
w.translator = translator
w.writer = bufio.NewWriter(writer)
return w
}
func (w *TranslatingWriter) writeLines(lines []string) error {
for _, l := range lines {
_, err := w.writer.WriteString(l)
if err != nil {
return err
}
_, err = w.writer.WriteString("\n")
if err != nil {
return err
}
}
return nil
}
func (w *TranslatingWriter) WriteHeading(level int, text string) error {
return w.writeLines(w.translator.ToHeading(level, text))
}
func (w *TranslatingWriter) WriteChannelList(channels []Channel) error {
return w.writeLines(w.translator.ToChannelList(channels))
}
func (w *TranslatingWriter) WriteChunkList(chunks []ChunkInfo) error {
return w.writeLines(w.translator.ToChunkList(chunks))
}
func (w *TranslatingWriter) WriteMessageList(chunk []MessageResolved) error {
return w.writeLines(w.translator.ToMessageList(chunk))
}
func (w *TranslatingWriter) WriteUserTable(users []User) error {
return w.writeLines(w.translator.ToUserTable(users))
}
func (w *TranslatingWriter) Flush() error {
return w.writer.Flush()
}