-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.go
More file actions
140 lines (115 loc) · 3.28 KB
/
main.go
File metadata and controls
140 lines (115 loc) · 3.28 KB
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
package main
import (
"os"
"flag"
"io/ioutil"
"encoding/json"
"github.com/nsf/termbox-go"
"github.com/sebashwa/vixl44/drawing"
"github.com/sebashwa/vixl44/keybindings"
"github.com/sebashwa/vixl44/state"
"github.com/sebashwa/vixl44/modes"
"github.com/sebashwa/vixl44/types"
"github.com/sebashwa/vixl44/factory"
)
func draw() {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
if state.CurrentMode == modes.PaletteMode {
drawing.DrawPalette()
} else {
drawing.DrawCanvas()
}
if state.CurrentMode == modes.VisualBlockMode {
drawing.DrawVisualBlockCursor()
} else {
drawing.DrawCursor()
}
if state.CurrentMode == modes.CommandMode {
drawing.DrawCommand()
} else {
drawing.DrawStatusBar()
}
termbox.Flush()
}
func pollEvents() {
loop:
for {
switch event := termbox.PollEvent(); event.Type {
case termbox.EventKey:
if state.CurrentMode == modes.CommandMode {
shouldQuit := keybindings.CommandMode(event.Ch, event.Key)
if shouldQuit { break loop }
} else {
switch state.CurrentMode {
case modes.VisualBlockMode:
keybindings.VisualBlockMode(event.Ch, event.Key)
case modes.PaletteMode:
keybindings.PaletteMode(event.Ch, event.Key)
case modes.NormalMode:
keybindings.NormalMode(event.Ch, event.Key)
}
keybindings.CursorMovement(event.Ch, event.Key)
keybindings.Common(event.Ch)
keybindings.ModeSelection(event.Ch, event.Key)
}
draw()
case termbox.EventResize:
draw()
}
}
}
func parseArguments() (string, int, int) {
var rows, columns int
for _, value := range([]string{"rows", "r"}) {
flag.IntVar(&rows, value, 20, "number of rows, default is 20, 0 means full height, ignored if name of existing file given")
}
for _, value := range([]string{"cols", "c"}) {
flag.IntVar(&columns, value, 20, "number of columns, default is 20, 0 means full width, ignored if name of existing file given")
}
flag.Parse()
return flag.Arg(0), rows, columns
}
func openOrCreateCanvas(filename string, columns, rows int) types.Canvas {
if _, err := os.Stat(filename); err == nil {
if data, err := ioutil.ReadFile(filename); err == nil {
var file types.File
if err := json.Unmarshal(data, &file); err != nil {
panic(err)
}
return factory.CreateCanvasFromFileCanvas(file.Canvas)
} else {
panic(err)
}
} else {
return factory.CreateCanvas(columns, rows)
}
}
func initializeApp() {
filename, canvasRows, canvasColumns := parseArguments()
state.Canvas = openOrCreateCanvas(filename, canvasColumns, canvasRows)
state.Palette = factory.CreatePalette(state.Canvas.Columns, state.Canvas.Rows)
state.StatusBar = types.StatusBar{
Position: state.Canvas.Rows,
Hint: "",
Error: "",
Command: "",
}
state.Cursor = types.Cursor{}
state.SelectedColor = termbox.Attribute(4)
state.CurrentMode = modes.NormalMode
state.Filename = filename
state.History = types.History{}
state.History.AddCanvasState(state.Canvas.GetValuesCopy())
}
func main() {
err := termbox.Init()
if err != nil {
panic(err)
}
termbox.SetOutputMode(termbox.Output256)
initializeApp()
defer termbox.Close()
termbox.HideCursor()
draw()
pollEvents()
}