-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.go
161 lines (132 loc) · 2.8 KB
/
buffer.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"bufio"
"errors"
"log"
"os"
)
// A Buffer is a view into a file on the servers computer
type Buffer struct {
prev *Stack
curr *string
next *Stack
file *string
}
// EmptyBuffer returns a new, empty, buffer
func EmptyBuffer() *Buffer {
return &Buffer{
prev: new(Stack),
curr: nil,
next: new(Stack),
}
}
// BufferFromFile returns a new buffer of the given filepath
func BufferFromFile(filepath string) *Buffer {
buff := EmptyBuffer()
buff.file = &filepath
file, err := os.Open(filepath)
if err != nil {
log.Printf("Opening a new file [%s]", filepath)
return buff
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
buff.Append(scanner.Text())
}
buff.Goto(1)
return buff
}
// Write writes the buffer to the file associated with it
func (buff *Buffer) Write() error {
if buff.file == nil {
return errors.New("Buffer has no file associated with it")
}
file, err := os.Create(*buff.file)
if err != nil {
return err
}
defer file.Close()
orgLine := buff.GetLineNum()
lastLine := 0
buff.Goto(1)
for buff.GetLineNum() != lastLine {
file.WriteString(buff.GetLine() + "\n")
lastLine = buff.GetLineNum()
buff.NextLine()
}
buff.Goto(orgLine)
return nil
}
// GetLine returns the current line from the buffer
func (buff *Buffer) GetLine() string {
if buff.curr != nil {
return *buff.curr
}
return ""
}
// GetLineNum returns the current line number
func (buff Buffer) GetLineNum() int {
return buff.prev.Len() + 1
}
// GetTotalLines returns the total line count
func (buff Buffer) GetTotalLines() int {
var current int
if buff.curr != nil {
current = 1
}
return buff.prev.Len() + buff.next.Len() + current
}
// NextLine shifts the lines forwards
func (buff *Buffer) NextLine() {
el := buff.next.Pop()
if el == nil {
return
}
buff.prev.Push(*buff.curr)
buff.curr = el
}
// PrevLine shifts the lines backwards
func (buff *Buffer) PrevLine() {
el := buff.prev.Pop()
if el == nil {
return
}
buff.next.Push(*buff.curr)
buff.curr = el
}
// Goto shifts the lines to the specified line
func (buff *Buffer) Goto(num int) {
if buff.GetTotalLines() == 0 {
return
}
if num < 1 {
num = 1
} else if num > buff.GetTotalLines() {
num = buff.GetTotalLines()
}
for buff.GetLineNum() != num {
if buff.GetLineNum() > num {
buff.PrevLine()
} else {
buff.NextLine()
}
}
}
// Insert puts text before the current line
// And sets the current line to the inserted text
func (buff *Buffer) Insert(text string) {
if buff.curr != nil {
buff.next.Push(*buff.curr)
}
buff.curr = &text
}
// Append puts text after the current line
// And sets the current line to the appended text
func (buff *Buffer) Append(text string) {
if buff.curr != nil {
buff.prev.Push(*buff.curr)
}
buff.curr = &text
}