-
Notifications
You must be signed in to change notification settings - Fork 19
/
shortcuts.go
73 lines (62 loc) · 1.44 KB
/
shortcuts.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
package gui
import (
"bytes"
"github.com/atotto/clipboard"
"io/ioutil"
"log"
)
func ShowPalette(v *View) bool {
b := v.getCurrentBuff()
v.UnfocusBuffers()
v.focusPalette(b)
return true
}
func Paste(v *View) bool {
b := v.getCurrentBuff()
if b == nil {
return false
}
str, err := clipboard.ReadAll()
if err == nil {
b.insertString(b.curs.x, str)
b.moveToEndOfLine()
return true
}
log.Println("Failed to paste from clipboard: ", err.Error())
return false
}
// NOTE: all shortcuts return a bool
// this is whether or not they have
// modified the buffer
// if the buffer is modified it will be
// re-rendered.
func Save(v *View) bool {
b := v.getCurrentBuff()
if b == nil {
return false
}
var buffer bytes.Buffer
for idx, line := range b.contents {
if idx > 0 {
// TODO: this avoids a trailing newline
// if we handle it like this? but if we have
// say enforce_newline_at_eof or something we
// might want to do this all the time
buffer.WriteRune('\n')
}
buffer.WriteString(line.String())
}
// TODO:
// - files probably dont have to be entirely
// re-saved all the time!
// - we can probably stream this somehow?
// - multi threaded?
// - lots of checks to do here: does the file exist/not exist
// handle the errors... etc.
err := ioutil.WriteFile(b.filePath, buffer.Bytes(), 0775)
if err != nil {
log.Println(err.Error())
}
log.Println("Wrote file '" + b.filePath + "' to disk")
return false
}