Skip to content

Commit 8afb585

Browse files
committed
Switch from the xyproto/textoutput to the xyproto/vt package
1 parent e1221d0 commit 8afb585

File tree

3 files changed

+27
-51
lines changed

3 files changed

+27
-51
lines changed

engine/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import (
2727
"github.com/xyproto/mime"
2828
"github.com/xyproto/pinterface"
2929
"github.com/xyproto/recwatch"
30-
"github.com/xyproto/textoutput"
3130
"github.com/xyproto/unzip"
31+
"github.com/xyproto/vt"
3232
)
3333

3434
const (
@@ -498,7 +498,7 @@ func (ac *Config) MustServe(mux *http.ServeMux) error {
498498
ac.serveJustHTTP = true
499499
}
500500

501-
to := textoutput.NewTextOutput(runtime.GOOS != "windows", !ac.quietMode)
501+
to := vt.NewTextOutput(runtime.GOOS != "windows", !ac.quietMode)
502502

503503
// Console output
504504
if !ac.quietMode && !ac.singleFileMode && !ac.simpleMode && !ac.noBanner {

engine/plugin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/natefinch/pie"
1212
"github.com/xyproto/algernon/platformdep"
1313
lua "github.com/xyproto/gopher-lua"
14-
"github.com/xyproto/textoutput"
14+
"github.com/xyproto/vt"
1515
)
1616

1717
type luaPlugin struct {
@@ -30,7 +30,7 @@ func (lp *luaPlugin) LuaHelp() (luahelp string, err error) {
3030

3131
// LoadPluginFunctions takes a Lua state and a TextOutput
3232
// (the TextOutput struct should be nil if not in a REPL)
33-
func (ac *Config) LoadPluginFunctions(L *lua.LState, o *textoutput.TextOutput) {
33+
func (ac *Config) LoadPluginFunctions(L *lua.LState, o *vt.TextOutput) {
3434
// Expose the functionality of a given plugin (executable file).
3535
// If on Windows, ".exe" (platformdep.ExeExt) is added to the path.
3636
// Returns true of successful.

engine/repl.go

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"github.com/xyproto/algernon/platformdep"
2020
"github.com/xyproto/ask"
2121
lua "github.com/xyproto/gopher-lua"
22-
"github.com/xyproto/textoutput"
22+
"github.com/xyproto/vt"
2323
)
2424

2525
const exitMessage = "bye"
@@ -101,83 +101,59 @@ func exportREPLSpecific(L *lua.LState) {
101101
}))
102102
}
103103

104-
// Split the given line in three parts, and color the parts
105-
func colorSplit(line, sep string, colorFunc1, colorFuncSep, colorFunc2 func(string) string, reverse bool) (string, string) {
106-
if strings.Contains(line, sep) {
107-
fields := strings.SplitN(line, sep, 2)
108-
s1 := ""
109-
if colorFunc1 != nil {
110-
s1 += colorFunc1(fields[0])
111-
} else {
112-
s1 += fields[0]
113-
}
114-
s2 := ""
115-
if colorFunc2 != nil {
116-
s2 += colorFuncSep(sep) + colorFunc2(fields[1])
117-
} else {
118-
s2 += sep + fields[1]
119-
}
120-
return s1, s2
121-
}
122-
if reverse {
123-
return "", line
124-
}
125-
return line, ""
126-
}
127-
128104
// Syntax highlight the given line
129-
func highlight(o *textoutput.TextOutput, line string) string {
105+
func highlight(o *vt.TextOutput, line string) string {
130106
unprocessed := line
131-
unprocessed, comment := colorSplit(unprocessed, "//", nil, o.DarkGray, o.DarkGray, false)
132-
module, unprocessed := colorSplit(unprocessed, ":", o.LightGreen, o.DarkRed, nil, true)
107+
unprocessed, comment := vt.ColorSplit(unprocessed, "//", 0, vt.DarkGray, vt.DarkGray, false)
108+
module, unprocessed := vt.ColorSplit(unprocessed, ":", vt.LightGreen, vt.Red, 0, true)
133109
function := ""
134110
if unprocessed != "" {
135111
// Green function names
136112
if strings.Contains(unprocessed, "(") {
137113
fields := strings.SplitN(unprocessed, "(", 2)
138-
function = o.LightGreen(fields[0])
114+
function = vt.LightGreen.Get(fields[0])
139115
unprocessed = "(" + fields[1]
140116
} else if strings.Contains(unprocessed, "|") {
141117
unprocessed = "<magenta>" + strings.ReplaceAll(unprocessed, "|", "<white>|</white><magenta>") + "</magenta>"
142118
}
143119
}
144-
unprocessed, typed := colorSplit(unprocessed, "->", nil, o.LightBlue, o.DarkRed, false)
145-
unprocessed = strings.ReplaceAll(unprocessed, "string", o.LightBlue("string"))
146-
unprocessed = strings.ReplaceAll(unprocessed, "number", o.LightYellow("number"))
147-
unprocessed = strings.ReplaceAll(unprocessed, "function", o.LightCyan("function"))
120+
unprocessed, typed := vt.ColorSplit(unprocessed, "->", 0, vt.LightBlue, vt.Red, false)
121+
unprocessed = strings.ReplaceAll(unprocessed, "string", vt.LightBlue.Get("string"))
122+
unprocessed = strings.ReplaceAll(unprocessed, "number", vt.LightYellow.Get("number"))
123+
unprocessed = strings.ReplaceAll(unprocessed, "function", vt.LightCyan.Get("function"))
148124
return module + function + unprocessed + typed + comment
149125
}
150126

151127
// Output syntax highlighted help text, with an additional usage message
152-
func outputHelp(o *textoutput.TextOutput, helpText string) {
128+
func outputHelp(o *vt.TextOutput, helpText string) {
153129
for _, line := range strings.Split(helpText, "\n") {
154130
o.Println(highlight(o, line))
155131
}
156132
o.Println(usageMessage)
157133
}
158134

159135
// Output syntax highlighted help about a specific topic or function
160-
func outputHelpAbout(o *textoutput.TextOutput, helpText, topic string) {
136+
func outputHelpAbout(o *vt.TextOutput, helpText, topic string) {
161137
switch topic {
162138
case "help":
163-
o.Println(o.DarkGray("Output general help or help about a specific topic."))
139+
o.Println(vt.DarkGray.Get("Output general help or help about a specific topic."))
164140
return
165141
case "webhelp":
166-
o.Println(o.DarkGray("Output help about web-related functions."))
142+
o.Println(vt.DarkGray.Get("Output help about web-related functions."))
167143
return
168144
case "confighelp":
169-
o.Println(o.DarkGray("Output help about configuration-related functions."))
145+
o.Println(vt.DarkGray.Get("Output help about configuration-related functions."))
170146
return
171147
case "quit", "exit", "shutdown", "halt":
172-
o.Println(o.DarkGray("Quit Algernon."))
148+
o.Println(vt.DarkGray.Get("Quit Algernon."))
173149
return
174150
}
175151
comment := ""
176152
for _, line := range strings.Split(helpText, "\n") {
177153
if strings.HasPrefix(line, topic) {
178154
// Output help text, with some surrounding blank lines
179155
o.Println("\n" + highlight(o, line))
180-
o.Println("\n" + o.DarkGray(strings.TrimSpace(comment)) + "\n")
156+
o.Println("\n" + vt.DarkGray.Get(strings.TrimSpace(comment)) + "\n")
181157
return
182158
}
183159
// Gather comments until a non-comment is encountered
@@ -187,7 +163,7 @@ func outputHelpAbout(o *textoutput.TextOutput, helpText, topic string) {
187163
comment = ""
188164
}
189165
}
190-
o.Println(o.DarkGray("Found no help for: ") + o.White(topic))
166+
o.Println(vt.DarkGray.Get("Found no help for: ") + vt.White.Get(topic))
191167
}
192168

193169
// Take all functions mentioned in the given help text string and add them to the readline completer
@@ -205,7 +181,7 @@ func addFunctionsFromHelptextToCompleter(helpText string, completer *readline.Pr
205181
}
206182

207183
// LoadLuaFunctionsForREPL exports the various Lua functions that might be needed in the REPL
208-
func (ac *Config) LoadLuaFunctionsForREPL(L *lua.LState, o *textoutput.TextOutput) {
184+
func (ac *Config) LoadLuaFunctionsForREPL(L *lua.LState, o *vt.TextOutput) {
209185
// Server configuration functions
210186
ac.LoadServerConfigFunctions(L, "")
211187

@@ -266,7 +242,7 @@ func (ac *Config) REPL(ready, done chan bool) error {
266242
defer L.Close()
267243

268244
// Colors and input
269-
o := textoutput.NewTextOutput(platformdep.EnableColors, true)
245+
o := vt.NewTextOutput(platformdep.EnableColors, true)
270246

271247
// Command history file
272248
historyFilename = filepath.Join(historydir, platformdep.HistoryFilename)
@@ -277,12 +253,12 @@ func (ac *Config) REPL(ready, done chan bool) error {
277253
<-ready // Wait for the server to be ready
278254

279255
// Tell the user that the server is ready
280-
o.Println(o.LightGreen("Ready"))
256+
o.Println(vt.LightGreen.Get("Ready"))
281257

282258
// Start the read, eval, print loop
283259
var (
284260
line string
285-
prompt = o.LightCyan("lua> ")
261+
prompt = vt.LightCyan.Get("lua> ")
286262
EOF bool
287263
EOFcount int
288264
)
@@ -312,7 +288,7 @@ func (ac *Config) REPL(ready, done chan bool) error {
312288
AtShutdown(func() {
313289
// Verbose mode has different log output at shutdown
314290
if !ac.verboseMode {
315-
o.Println(o.LightBlue(exitMessage))
291+
o.Println(vt.LightBlue.Get(exitMessage))
316292
}
317293
})
318294
for {
@@ -326,7 +302,7 @@ func (ac *Config) REPL(ready, done chan bool) error {
326302
switch {
327303
case err == io.EOF:
328304
if ac.debugMode {
329-
o.Println(o.LightPurple(err.Error()))
305+
o.Println(vt.LightMagenta.Get(err.Error()))
330306
}
331307
EOF = true
332308
case err == readline.ErrInterrupt:

0 commit comments

Comments
 (0)