-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.go
197 lines (179 loc) · 4.78 KB
/
buffer.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
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2015 Lukas Weber. All rights reserved.
// Use of this source code is governed by the MIT-styled
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"strings"
termbox "github.com/nsf/termbox-go"
)
// A Buffer is a screen of the ui. Buffers are opened on a stack.
type Buffer interface {
// Draw content on the screen
Draw()
// Title displayed in the bottom bar. It has to identify buffer uniquely.
Title() string
// Name of the buffer
Name() string
// Close the Buffer
Close()
// HandleCommand handles string commands. If it returns bool, the
// command was accepted. If not, it was invalid.
HandleCommand(cmd string, args []string, stack *BufferStack) bool
}
// BufferStack is the Stack structure managing drawing of the screen and
// buffers.
type BufferStack struct {
buffers []Buffer
prompt Prompt
}
func invalidCommand(cmd string) {
StatusLine = "invalid command: " + cmd
}
// Init initializes the BufferStack and executes the initial command set in Config.
func (b *BufferStack) Init() {
fields := strings.Fields(config.General.Initial_Command)
if len(fields) > 0 {
accept := b.handleCommand(fields[0], fields[1:])
if !accept {
invalidCommand(fields[0])
}
if len(b.buffers) == 0 {
b.Push(NewSearchBuffer("", STMessages))
}
}
}
// Push pushes a new buffer on the stack. Focus is changed to that buffer.
func (b *BufferStack) Push(n Buffer) {
for i, buf := range b.buffers {
// If the buffer already exists, change to it instead.
if buf.Name() == n.Name() && buf.Title() == n.Title() {
n.Close()
copy(b.buffers[i:], b.buffers[i+1:])
b.buffers[len(b.buffers)-1] = buf
b.refresh()
return
}
}
b.buffers = append(b.buffers, n)
b.refresh()
}
// StatusLine is displayed at the bottom of the screen. useful for error messages.
var StatusLine string
// Pop pops the last buffer from the stack.
func (b *BufferStack) Pop() {
if len(b.buffers) == 0 {
return
}
b.buffers[len(b.buffers)-1].Close()
b.buffers = b.buffers[:len(b.buffers)-1]
b.refresh()
}
// refresh clears the terminal and redraws everything.
func (b *BufferStack) refresh() {
termbox.Clear(0, 0)
if len(b.buffers) == 0 {
return
}
b.buffers[len(b.buffers)-1].HandleCommand("_refresh", nil, b)
b.buffers[len(b.buffers)-1].Draw()
w, h := termbox.Size()
cbuf := termbox.CellBuffer()
for i := 0; i < w; i++ {
cbuf[(h-2)*w+i].Bg = termbox.Attribute(config.Theme.BottomBar)
cbuf[(h-2)*w+i].Fg = termbox.AttrBold
}
title := b.buffers[len(b.buffers)-1].Title()
name := b.buffers[len(b.buffers)-1].Name()
printLine(0, h-2, fmt.Sprintf("[%d: %s] %s", len(b.buffers)-1, name, title), -1, -1)
if b.prompt.Active() {
b.prompt.Draw()
} else if StatusLine != "" {
_, h := termbox.Size()
printLine(0, h-1, StatusLine, -1, -1)
}
termbox.Flush()
}
// handleCommand executes global commands.
func (b *BufferStack) handleCommand(cmd string, args []string) bool {
switch cmd {
case "close":
b.Pop()
case "quit":
for _, buf := range b.buffers {
buf.Close()
}
b.buffers = nil
case "search":
b.Push(NewSearchBuffer(strings.Join(args, " "), STThreads))
case "msearch":
b.Push(NewSearchBuffer(strings.Join(args, " "), STMessages))
case "compose":
b.Push(NewComposeBuffer(composeMail()))
case "help":
b.Push(&HelpBuffer{b.buffers[len(b.buffers)-1].Name()})
case "prompt":
StatusLine = ""
b.prompt.Activate(strings.Join(args, " "))
case "refresh":
StatusLine = "view refreshed."
for _, buf := range b.buffers {
buf.HandleCommand("_refresh", nil, b)
}
b.refresh()
default:
return false
}
return true
}
// HandleEvent handles termbox events and invokes commands if their keybinding was pressed.
func (b *BufferStack) HandleEvent(event *termbox.Event) {
if len(b.buffers) == 0 {
return
}
if event.Type == termbox.EventResize {
termbox.Flush()
for _, buf := range b.buffers {
buf.HandleCommand("resize", nil, b)
}
b.refresh()
return
}
if event.Type == termbox.EventKey {
StatusLine = ""
if b.prompt.Active() {
cmd, args := b.prompt.HandleEvent(event)
if len(cmd) != 0 {
accept := b.buffers[len(b.buffers)-1].HandleCommand(cmd, args, b)
if !accept {
accept = b.handleCommand(cmd, args)
}
if !accept {
invalidCommand(cmd)
}
_, h := termbox.Size()
printLine(0, h-1, StatusLine, -1, -1)
}
return
}
cmd := getBinding("", event.Ch, event.Key)
accept := false
if cmd == nil {
cmd = getBinding(b.buffers[len(b.buffers)-1].Name(), event.Ch, event.Key)
if cmd == nil {
return
}
}
accept = b.buffers[len(b.buffers)-1].HandleCommand(cmd.Command, cmd.Args, b)
if !accept {
accept = b.handleCommand(cmd.Command, cmd.Args)
}
if !accept {
invalidCommand(cmd.Command)
}
if StatusLine != "" {
_, h := termbox.Size()
printLine(0, h-1, StatusLine, -1, -1)
}
}
}