-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_grl.go
96 lines (72 loc) · 2.28 KB
/
main_grl.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
// +build ignore
// Copyright 2013-2015 Rocky Bernstein
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
// This simple REPL (read-eval-print loop) for Go using GNU Readline
import (
"fmt"
"os"
"reflect"
"github.com/rocky/go-gnureadline"
"github.com/davecgh/go-spew/spew"
"github.com/rocky/go-fish"
"github.com/rocky/go-fish/cmd"
)
func intro_text() {
repl.Section("== A Go eval REPL with GNU Readline support ==")
fmt.Printf(`
Results of expression are stored in variable slice "results".
The environment is stored in global variable "env".
Short form assignment, e.g. a, b := 1, 2, is supported.
Enter expressions to be evaluated at the "gofish>" prompt.
To see all results, type: "results".
To quit, enter: "quit" or Ctrl-D (EOF).
To get help, enter: "help".
`)
}
// history_file is file name where history entries were and are to be saved. If
// the empty string, no history is saved and no history read in initially.
var historyFile string
// term is the current environment TERM value, e.g. "gnome", "xterm", or "vt100"
var term string
// gnuReadLineSetup is boilerplate initialization for GNU Readline.
func gnuReadLineSetup() {
term = os.Getenv("TERM")
historyFile = repl.HistoryFile(".go-fish")
if historyFile != "" {
gnureadline.ReadHistory(historyFile)
}
// Set maximum number of history entries
gnureadline.StifleHistory(100)
}
// gnuReadLineTermination has GNU Readline Termination tasks:
// save history file if ane, and reset the terminal.
func gnuReadLineTermination() {
if historyFile != "" {
gnureadline.WriteHistory(historyFile)
}
if term != "" {
gnureadline.Rl_reset_terminal(term)
}
}
func spewInspect(a ...interface{}) string {
value := a[0].(reflect.Value)
return spew.Sprint(value.Interface())
}
// Set up the Go package, function, constant, variable environment; then REPL
// (Read, Eval, Print, and Loop).
func main() {
// A place to store result values of expressions entered
// interactively
env := repl.MakeEvalEnv()
// Make this truly self-referential
env.Vars["env"] = reflect.ValueOf(env)
intro_text()
gnuReadLineSetup()
defer gnuReadLineTermination()
// Initialize REPL commands
fishcmd.Init()
repl.REPL(env, gnureadline.Readline, spewInspect)
os.Exit(repl.ExitCode)
}