This repository has been archived by the owner on Apr 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
155 lines (127 loc) · 3.53 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
package buffer
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"unicode"
"github.com/jonpalmisc/atto/internal/config"
"github.com/jonpalmisc/atto/internal/support"
)
// IsInsertable tells whether a character is insertable into the buffer or not.
func IsInsertable(c rune) bool {
switch unicode.ToLower(c) {
case '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l',
'z', 'x', 'c', 'v', 'b', 'n', 'm',
'`', '~', '-', '_', '=', '+', '\t', '[', '{', ']', '}', '\\', '|',
';', ':', '\'', '"', ',', '<', '.', '>', '/', '?', ' ':
return true
default:
return false
}
}
// Buffer represents a text buffer corresponding to a file.
type Buffer struct {
Config *config.Config
// The path to the file and its type.
Path string
FileType support.FileType
// The buffer's lines and condition.
Lines []Line
IsDirty bool
IsReadOnly bool
// The cursor's position. The Y value must always be decremented by one when
// accessing buffer elements since the editor's title bar occupies the first
// row of the screen. CursorDX is the cursor's X position, with compensation
// for extra space introduced by rendering tabs.
CursorX int
CursorDX int
CursorY int
// The viewport's column and row offsets.
OffsetX int
OffsetY int
}
// Create creates a new buffer for a given path.
func Create(config *config.Config, path string) (Buffer, error) {
b := Buffer{
Config: config,
Path: path,
FileType: support.GuessFileType(path),
CursorY: 1,
}
// Attempt to open the file at the given path.
f, err := os.Open(path)
if err != nil && !os.IsNotExist(err) {
return Buffer{}, fmt.Errorf("%v (%v)", path, err)
}
// Read the file line by line and append each line to end of the buffer.
s := bufio.NewScanner(f)
for s.Scan() {
b.InsertLine(b.Length(), s.Text())
}
// If the file is completely empty, add an empty line to the buffer.
if b.Length() == 0 {
b.InsertLine(0, "")
}
f.Close()
return b, nil
}
// FromStrings creates a buffer from an array of strings rather than a file.
func FromStrings(config *config.Config, name string, rawLines []string) Buffer {
b := Buffer{
Config: config,
Path: name,
FileType: support.GuessFileType(name),
CursorY: 1,
}
// Insert each array element as a new line.
for _, l := range rawLines {
b.InsertLine(b.Length(), l)
}
// If the file is completely empty, add an empty line to the buffer.
if b.Length() == 0 {
b.InsertLine(0, "")
}
return b
}
// Length returns the buffer's length (number of lines).
func (b *Buffer) Length() int {
return len(b.Lines)
}
// FileName extracts the name of the file from the buffer's file path.
func (b *Buffer) FileName() string {
_, name := filepath.Split(b.Path)
return name
}
// FocusedLine returns the buffer's focused line.
func (b *Buffer) FocusedLine() *Line {
return &b.Lines[b.CursorY-1]
}
// PreviousLine returns the line above the buffer's focused line.
func (b *Buffer) PreviousLine() *Line {
i := b.CursorY - 2
if i < 0 {
return &b.Lines[0]
}
return &b.Lines[i]
}
func (b *Buffer) Write(path string) error {
var text string
// Append each line of the buffer (plus a newline) to the string.
for i := 0; i < b.Length(); i++ {
text += b.Lines[i].Text + "\n"
}
err := ioutil.WriteFile(path, []byte(text), os.ModePerm)
if err != nil {
return err
} else {
b.Path = path
b.FileType = support.GuessFileType(path)
b.IsDirty = false
return nil
}
}