-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave.go
81 lines (70 loc) · 1.67 KB
/
save.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
package main
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/jroimartin/gocui"
)
type Save struct {
Editor *Editor
Force bool
}
func (s *Save) Layout(gui *gocui.Gui) error {
maxX, maxY := gui.Size()
var input, label *gocui.View
_, err := gui.SetView("save-box", maxX/2-33, maxY/2-3, maxX/2+33, maxY/2+3)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
}
label, err = gui.SetView("save-label", maxX/2-30, maxY/2-2, maxX/2+30, maxY/2)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
fmt.Fprint(label, "Enter Filename:")
label.Frame = false
}
input, err = gui.SetView("save-input", maxX/2-30, maxY/2, maxX/2+30, maxY/2+2)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
fmt.Fprint(input, s.Editor.Layout.FileName)
input.SetCursor(len(s.Editor.Layout.FileName), 0)
}
input.Editor = s
input.Editable = true
gui.SetCurrentView("save-input")
return nil
}
func (s *Save) Edit(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
switch {
case key == gocui.KeyEnter:
s.Editor.Layout.FileName = strings.TrimSpace(v.Buffer())
err := s.Save()
if err != nil {
label, _ := s.Editor.Gui.View("save-label")
label.Clear()
fmt.Fprint(label, err)
return
}
s.Editor.Gui.DeleteView("save-box")
s.Editor.Gui.DeleteView("save-input")
s.Editor.Gui.DeleteView("save-label")
s.Editor.Layout.Save = nil
default:
gocui.DefaultEditor.Edit(v, key, ch, mod)
}
}
func (s *Save) Save() error {
err := os.WriteFile(s.Editor.Layout.FileName, bytes.Join(s.Editor.Text, []byte{'\n'}), 0644)
if err != nil {
s.Editor.Nodify(err.Error())
} else {
s.Editor.Nodify("Saved")
}
return err
}