-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenvironment.go
More file actions
186 lines (158 loc) · 7.29 KB
/
environment.go
File metadata and controls
186 lines (158 loc) · 7.29 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
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
package webfriend
import (
"fmt"
"os"
"time"
"github.com/fatih/color"
"github.com/ghetzel/friendscript"
"github.com/ghetzel/friendscript/commands/file"
"github.com/ghetzel/friendscript/scripting"
"github.com/ghetzel/go-stockutil/typeutil"
"github.com/ghetzel/go-webfriend/browser"
"github.com/ghetzel/go-webfriend/commands/cookies"
"github.com/ghetzel/go-webfriend/commands/core"
"github.com/ghetzel/go-webfriend/commands/page"
)
var MaxReaderWait = time.Duration(5) * time.Second
type Environment struct {
*friendscript.Environment
Cookies *cookies.Commands
Core *core.Commands
Page *page.Commands
File *file.Commands
browser *browser.Browser
script *scripting.Friendscript
stack []*scripting.Scope
}
func NewEnvironment(b *browser.Browser) *Environment {
environment := &Environment{
Environment: friendscript.NewEnvironment(),
browser: b,
stack: make([]*scripting.Scope, 0),
}
if b := environment.browser; b != nil {
b.SetScope(environment)
b.Tab().RegisterEventHandler(`Inspector.detached`, func(event *browser.Event) {
b.Stop()
os.Exit(0)
})
}
// add in our custom modules and module overrides
environment.Core = core.New(b)
environment.Cookies = cookies.New(b)
environment.Page = page.New(b)
environment.RegisterModule(``, environment.Core)
environment.RegisterModule(`cookies`, environment.Cookies)
environment.RegisterModule(`page`, environment.Page)
// add our custom REPL commands
environment.RegisterCommandHandler(`help`, environment.handleReplHelp)
// add command context handlers
environment.RegisterContextHandler(func(ctx *scripting.Context, isCompleted bool) {
if browser := environment.Browser(); browser != nil {
params := map[string]interface{}{
`command`: ctx.Label,
`offset`: ctx.AbsoluteStartOffset,
`advance`: ctx.Length,
}
if id := environment.Scope().Get(`invocation`); !typeutil.IsZero(id) {
params[`id`] = id
}
if isCompleted {
params[`action`] = `finished`
params[`took`] = (ctx.Took.Round(time.Microsecond) / time.Millisecond)
} else {
params[`action`] = `running`
}
if err := ctx.Error; err != nil {
params[`error`] = err.Error()
}
browser.Tab().Emit(`Webfriend.scriptContextEvent`, params)
if isCompleted && ctx.Error == nil {
if delay := browser.Tab().AfterCommandDelay; delay > 0 {
time.Sleep(delay)
}
}
}
})
return environment
}
func (self *Environment) MustModule(name string) friendscript.Module {
if module, ok := self.Module(name); ok {
return module
} else {
panic(fmt.Sprintf("Invalid module %q", name))
}
}
func (self *Environment) Browser() *browser.Browser {
return self.browser
}
func (self *Environment) handleReplHelp(ctx *friendscript.InteractiveContext, environment *friendscript.Environment) ([]string, error) {
// This is a big colorized Unicode WebFriend logo for terminal output.
// [ .. ] hi blue w/ blue bg
// { .. } hi green w/ blue bg
// ( .. ) hi black w/ blue bg
// < .. > hi green w/ default bg
lines := []string{}
lines = append(lines, "")
lines = append(lines, fmt.Sprintf("[Web]<friend> v%s - %q", Version, Slogan))
lines = append(lines, "")
lines = append(lines, " [░░▓▓▓▓▓▓▓▓▓▓▓░░]")
lines = append(lines, " [░▓▓█████████████████▓▓░]")
lines = append(lines, " {░████▙}[██████████████████████▓▓░░]")
lines = append(lines, " {░███████▛}[██]{▟██▙}[███████████████]{▟█████░}")
lines = append(lines, " {░██████████}[█]{▟██▛}[███████████████]{▟█▛██████░}")
lines = append(lines, " {░████████████████}[██████████████████]{▟████████░}")
lines = append(lines, " {░███████████████▛}[████████████████]{▟████████████░}")
lines = append(lines, " {░█████████████▛}[█████████████████]{▟██████▛}[█]{▜█▛}[█]{███░}")
lines = append(lines, " {░█████████████▛}[█](▟██▙)[█████████](▟██▙){▜██▛}[█████]{▜▛}[██]{████░}")
lines = append(lines, " {░█}[█]{▜████▛}[██]{▜█▙}[██](████)[█████████](████)[█████████████]{████░}")
lines = append(lines, " [░███]{▜████}[███████](▜██▛)[█████████](▜██▛){▟█████████████████░}")
lines = append(lines, " [░█████]{▜█▙}[████████████████████]{▟████████████████████░}")
lines = append(lines, " [░██████]{▜█▙}[██████████████████]{█████████████████████▓░}")
lines = append(lines, " [░▓▓█]{▟██████▙}[████████████████]{▜███████████████▛}[██▓░]")
lines = append(lines, " {░███████████▙}[███████████████████]{▟██████████▛}[█▓░]")
lines = append(lines, " {░███████████}[█████](▜████████▛)[███]{██████████▛}[█▓▓░]")
lines = append(lines, " {░███████▛}[███████](▜██████▛)[████]{█████████▛}[█░]")
lines = append(lines, " {░████▛}[███████████████████]{▟███████▛}[██░]")
lines = append(lines, " [░▓▓███████████████████]{███████}[█░]")
lines = append(lines, " [░▓▓███████████████]{▜█▓}[▓░]")
lines = append(lines, " [░░▓▓▓▓▓▓▓▓▓▓▓▓░░]")
output := ``
for i, line := range lines {
outline := ``
state := 0
for _, c := range line {
switch c {
case '[':
state = 1
case '{':
state = 2
case '(':
state = 3
case '<':
state = 4
case ']', '}', ')', '>':
switch state {
case 1:
output += color.New(color.FgHiBlue).Sprint(outline)
case 2:
output += color.New(color.FgHiGreen, color.BgHiBlue).Sprint(outline)
case 3:
output += color.New(color.FgHiBlack, color.BgHiBlue).Sprint(outline)
case 4:
output += color.New(color.FgHiGreen).Sprint(outline)
}
state = 0
outline = ``
default:
if state > 0 {
outline += string(c)
} else {
output += string(c)
}
}
}
lines[i] = output
}
return lines, nil
}