-
Notifications
You must be signed in to change notification settings - Fork 0
/
acme.go
108 lines (95 loc) · 1.89 KB
/
acme.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
package nyne
import (
"fmt"
"os"
"strings"
"sync"
"9fans.net/go/acme"
)
// Acme implements the Listener interface for acme events
type Acme struct {
EventHooks map[Text][]Handler
WinHooks map[Text][]WinHandler
KeyHooks map[rune]Handler
wins map[int]string
bufs map[int]*Buf
mux sync.Mutex
}
// NewAcme constructs an Acme event listener
func NewAcme() *Acme {
return &Acme{
EventHooks: make(map[Text][]Handler),
WinHooks: make(map[Text][]WinHandler),
KeyHooks: make(map[rune]Handler),
wins: make(map[int]string),
bufs: make(map[int]*Buf),
}
}
// Listen watches the acme event log for events and executes hooks
// based on those events
func (a *Acme) Listen() error {
l, err := acme.Log()
if err != nil {
return err
}
for {
event, err := l.Read()
if err != nil {
return err
}
// create listener on new window events
if event.Op == "new" {
go a.startBuf(event.ID)
}
}
}
func (a *Acme) startBuf(id int) {
err := a.mapWindows()
if err != nil {
fmt.Fprintf(os.Stderr, "%+v", err)
return
}
if a.isDisabled(id) {
return
}
f := &Buf{
id: id,
file: a.wins[id],
EventHooks: a.EventHooks,
WinHooks: a.WinHooks,
KeyHooks: a.KeyHooks,
}
a.bufs[id] = f
err = f.Start()
if err != nil {
fmt.Fprintf(os.Stderr, "%+v", err)
return
}
}
var disabledNames = []string{"/-", "Del", "xplor", "+Errors"}
func (a *Acme) isDisabled(id int) bool {
filename := a.wins[id]
for _, name := range disabledNames {
if strings.Contains(filename, name) {
return true
}
}
return false
}
func (a *Acme) mapWindows() error {
ws, err := acme.Windows()
if err != nil {
return err
}
a.mux.Lock()
defer a.mux.Unlock()
a.wins = make(map[int]string)
for _, w := range ws {
a.wins[w.ID] = w.Name
}
return nil
}
// Buf returns the running Buf by its ID
func (a *Acme) Buf(id int) *Buf {
return a.bufs[id]
}