-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_sets.go
160 lines (139 loc) · 3.32 KB
/
buffer_sets.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
package buffer
import (
"errors"
"io/fs"
"os"
"slices"
"github.com/ge-editor/utils"
"github.com/ge-editor/te/file"
"github.com/ge-editor/te/pkg_error"
)
// Create te.Editor.Buffers from
// Files (command line arguments)
// Always one will be created
//
// # Create bufferSet and append to Buffers from path string array
//
// From paths (command line argument)
// create te.Editor.Buffers
// Create one bufferSet, even if there is none.
func NewBufferSets(paths []string) (bss *BufferSets, errs error) {
bss = &BufferSets{}
for _, path := range paths {
var err error
var mode fs.FileMode
buff := newBufferSet(path)
fileInfo, err := os.Stat(path)
if os.IsNotExist(err) {
err = buff.New()
goto last
}
if err != nil {
goto last
}
mode = fileInfo.Mode()
if mode.IsRegular() {
err = buff.Load()
buff.SetReadonly(mode.Perm()&0200 == 0)
} else {
continue // such directory or ...
}
last:
if err != nil {
errs = errors.Join(errs, err)
} else {
bss.Append(buff)
}
} // for files
// create one buffer if nothing
if len(*bss) == 0 {
buff := newBufferSet("")
buff.New()
bss.Append(buff)
}
return bss, errs
}
// Buffers
type BufferSets []*bufferSet
// Find a buffer from the bufferSetArray
// If found, return the meta from metas
// If metas does not exist, return a new meta
// If there exists an Editor, you could call "func (e *Editor) GetMeta() *meta {}"
func (bss *BufferSets) GetMeta(ff *file.File) *Meta {
for _, buffSet := range *bss {
if buffSet.File == ff {
return buffSet.PopMeta()
}
}
return newMeta()
}
// Find BufferSet in BufferSetArray
func (bss *BufferSets) BufferSet(ff *file.File) *bufferSet {
for _, buffSet := range *bss {
if buffSet.File == ff {
return buffSet
}
}
return nil
}
// Find the buffer and meta for filePath from Buffers
// Create a new buffer otherwise
// Load into buffer if the file exists
// Register into Buffers
// Return buffer and meta
func (bss *BufferSets) GetFileAndMeta(filePath string) (*file.File, *Meta, error) {
for _, buffSet := range *bss {
if utils.SameFile(filePath, buffSet.GetPath()) {
return buffSet.File, buffSet.PopMeta(), nil
}
}
var err error
buffSet := newBufferSet(filePath)
if err = buffSet.Load(); err != nil {
if err = buffSet.New(); err != nil {
return buffSet.File, buffSet.PopMeta(), err
}
err = pkg_error.ErrorNewFile
} else {
err = pkg_error.ErrorLoadedFile
}
bss.Append(buffSet)
return buffSet.File, buffSet.PopMeta(), err
}
func (bss *BufferSets) Append(buffSet *bufferSet) {
*bss = append(*bss, buffSet)
}
/*
func (bss *BufferSets) Remove(filePath string) {
for i, buffSet := range *bss {
if utils.SameFile(filePath, buffSet.GetPath()) {
*bss = append((*bss)[:i], (*bss)[i+1:]...)
return
}
}
}
*/
func (bss *BufferSets) RemoveByBufferFile(ff *file.File) int {
i := bss.GetIndexByBufferFile(ff)
if i == -1 {
return -1
}
*bss = slices.Delete(*bss, i, i+1)
/*
copy((*bss)[i:], (*bss)[i+1:])
(*bss)[len(*bss)-1] = nil //
*bss = (*bss)[:len(*bss)-1]
*/
return i
}
// Check if the specified *file.File exists in BufferSets
// If found, return its index
// If not found, return -1
func (bss *BufferSets) GetIndexByBufferFile(ff *file.File) int {
for i, buffSet := range *bss {
if buffSet.File == ff {
return i
}
}
return -1 // Return -1 if the element is not found
}