-
Notifications
You must be signed in to change notification settings - Fork 4
/
file.go
184 lines (164 loc) · 3.92 KB
/
file.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"encoding/binary"
"fmt"
"io"
"os"
"github.com/stretto-editor/gocui"
)
// create the file in the directory of the
func createFile(filename string) {
if _, err := os.Stat(filename); os.IsNotExist(err) {
var file *os.File
file, _ = os.Create(filename)
file.Close()
}
}
var newFileHandler = func() func(g *gocui.Gui, v *gocui.View) error {
i := 0
return func(g *gocui.Gui, v *gocui.View) error {
i++
name := fmt.Sprintf("file%d", i)
newView, _ := newFileView(g, name)
g.SetViewOnTop(newView.Name())
g.SetCurrentView(newView.Name())
g.SetWorkingView(newView.Name())
return nil
}
}()
func saveHandler(g *gocui.Gui, v *gocui.View) error {
// vMain, _ := g.View("main")
vMain := g.Workingview()
if vMain.Title == "" {
currentDemonInput = func(g *gocui.Gui, input string) (demonInput, error) {
createFile(input)
vMain.Title = input
if err := saveMain(vMain, vMain.Title); err != nil {
return nil, err
}
return nil, nil
}
interactive(g, "Save")
return nil
}
if err := saveMain(vMain, vMain.Title); err != nil {
return err
}
return nil
}
func saveMain(v *gocui.View, filename string) error {
if filename == "" {
return nil
}
f, err := os.OpenFile(filename, os.O_WRONLY, 0666)
if err != nil {
return err
}
defer f.Close()
p := make([]byte, 5)
v.Rewind()
var size int64 = -1
for {
n, err := v.Read(p)
size += int64(n)
if n > 0 {
if _, err = f.Write(p[:n]); err != nil {
return err
}
}
if err == io.EOF {
f.Truncate(size * int64(binary.Size(p[0])))
break
}
if err != nil {
return err
}
}
return nil
}
func quitHandler(g *gocui.Gui, v *gocui.View) error {
currentDemonInput = func(g *gocui.Gui, input string) (demonInput, error) {
if input != "n" {
// vMain, _ := g.View("main")
vMain := g.Workingview()
if vMain.Title == "" {
interactive(g, "File name")
return func(g *gocui.Gui, input string) (demonInput, error) {
createFile(input)
vMain.Title = input
if err := saveMain(vMain, vMain.Title); err != nil {
return nil, err
}
return nil, gocui.ErrQuit
}, nil
}
if err := saveMain(vMain, vMain.Title); err != nil {
return nil, err
}
}
return nil, gocui.ErrQuit
}
interactive(g, "Save Modifications (y/n)")
return nil
}
func closeFileHandler(g *gocui.Gui, v *gocui.View) error {
currentDemonInput = func(g *gocui.Gui, input string) (demonInput, error) {
// vMain, _ := g.View("main")
vMain := g.Workingview()
if input != "n" {
if vMain.Title == "" {
interactive(g, "File name")
return func(g *gocui.Gui, input string) (demonInput, error) {
createFile(input)
vMain.Title = input
if err := saveMain(vMain, vMain.Title); err != nil {
return nil, err
}
closeView(g, vMain)
return nil, nil
}, nil
}
if err := saveMain(vMain, vMain.Title); err != nil {
return nil, err
}
}
closeView(g, vMain)
return nil, nil
}
interactive(g, "Save Modifications (y/n)")
return nil
}
func openFileHandler(g *gocui.Gui, v *gocui.View) error {
currentDemonInput = func(g *gocui.Gui, input string) (demonInput, error) {
return nil, openAndDisplayFile(g, input)
}
interactive(g, "Open File")
return nil
}
func openAndDisplayFile(g *gocui.Gui, filename string) error {
v, _ := newFileView(g, filename)
g.SetWorkingView(v.Name())
if g.CurrentMode().Name() != cmdMode {
g.SetCurrentView(v.Name())
}
// g.SetViewOnTop(v.Name())
err := openFile(v, filename)
if err == nil {
v.Title = filename
return nil
}
return fmt.Errorf("Could not open file : %s", filename)
}
func saveAsHandler(g *gocui.Gui, v *gocui.View) error {
currentDemonInput = func(g *gocui.Gui, filename string) (demonInput, error) {
return nil, saveAs(g, filename)
}
interactive(g, "Save as")
return nil
}
func saveAs(g *gocui.Gui, filename string) error {
//v, _ := g.View("main")
v := g.Workingview()
createFile(filename)
return saveMain(v, filename)
}