-
Notifications
You must be signed in to change notification settings - Fork 2
/
gokilo.go
98 lines (77 loc) · 1.86 KB
/
gokilo.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
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/srinathh/gokilo/rawmode"
"github.com/srinathh/gokilo/terminal"
)
func ctrlKey(b byte) rune {
return rune(b & 0x1f)
}
type editorState int
const (
stateEditing editorState = iota
stateSavePrompt
stateQuitPrompt
stateFindPrompt
stateFindNav
)
const kiloVersion = "0.0.2"
// SafeExit restores terminal using the original terminal config stored
// in the global session variable
func SafeExit(err error) {
fmt.Fprint(os.Stdout, "\x1b[2J\x1b[H")
if err1 := rawmode.Restore(s.OrigTermCfg); err1 != nil {
fmt.Fprintf(os.Stderr, "Error: disabling raw mode: %s\r\n", err)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\r\n", err)
os.Exit(1)
}
os.Exit(0)
}
var s = Session{}
func main() {
// parse config flags & parameters
flag.Parse()
filename := flag.Arg(0)
// enable raw mode
origCfg, err := rawmode.Enable()
if err != nil {
fmt.Fprintf(os.Stderr, "Error enabling raw mode: %v", err)
os.Exit(1)
}
s.OrigTermCfg = origCfg
// get the screen dimensions and create a view
rows, cols, err := rawmode.GetWindowSize()
if err != nil {
SafeExit(fmt.Errorf("couldn't get window size: %v", err))
}
s.View = NewView(rows, cols)
// create the editor
if flag.Arg(0) == "" {
s.Editor = NewEditor()
} else {
s.Editor, err = NewEditorFromFile(filename)
if err != nil {
SafeExit(fmt.Errorf("couldn't open file %s: %v", filename, err))
}
}
s.setStatusMessage(startMsg)
s.State = stateEditing
for {
s.View.RefreshScreen(s.Editor, s.StatusMessage, s.Prompt)
// read key
k, err := terminal.ReadKey()
if err != nil {
SafeExit(fmt.Errorf("Error reading from terminal: %s", err))
}
s.Dispatch(k)
// if it's been 5 secs since the last status message, reset
if time.Now().Sub(s.StatusMessageTime) > time.Second*5 && s.State == stateEditing {
s.setStatusMessage("")
}
}
}