-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (130 loc) · 3.22 KB
/
main.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
// Copyright 2022 Frederik Zipp. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Ivy-prompt is a line editor interface wrapper for Rob Pike's [Ivy],
// an APL-like big number calculator. It provides a Readline-style input
// prompt with an input history and tab-completion.
//
// [Ivy]: https://robpike.io/ivy
package main
import (
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strings"
"github.com/peterh/liner"
)
var (
opMultiline = regexp.MustCompile(`^\s*op\s*.*=\s*$`)
specialDemo = regexp.MustCompile(`^\s*\)\s*demo\s*$`)
specialPrompt = regexp.MustCompile(`^\s*\)\s*prompt.*$`)
)
func main() {
origMode, err := liner.TerminalMode()
check(err)
ivy, err := NewIvy()
check(err)
err = ivy.Start()
check(err)
defer func() { printErr(ivy.Quit()) }()
fmt.Println(`Ivy big number calculator. https://robpike.io/ivy
Type ")help" for help, Ctrl-D to quit.`)
l := liner.NewLiner()
defer func() { printErr(l.Close()) }()
l.SetWordCompleter(makeCompleter(ivy))
l.SetTabCompletionStyle(liner.TabPrints)
linerMode, err := liner.TerminalMode()
if err != nil {
printErr(fmt.Errorf("could not determine terminal mode: %w", err))
return
}
printErr(loadHistory(l))
for err == nil {
err = readEvalPrint(ivy, l, origMode, linerMode)
}
if err != io.EOF {
printErr(err)
// continue in order to save history
}
err = saveHistory(l)
if err != nil {
printErr(fmt.Errorf("could not save history: %w", err))
}
}
// An io.EOF error is returned if the user signals end-of-file
// by pressing Ctrl-D.
func readEvalPrint(ivy *Ivy, l *liner.State, origMode, linerMode liner.ModeApplier) error {
input, err := l.Prompt("ivy> ")
if err != nil {
return err
}
if specialDemo.MatchString(input) {
err = origMode.ApplyMode()
if err != nil {
return fmt.Errorf("could not apply original terminal mode: %w", err)
}
runDemo()
err = linerMode.ApplyMode()
if err != nil {
return fmt.Errorf("could not apply terminal mode for line editor: %w", err)
}
} else if specialPrompt.MatchString(input) {
fmt.Println("prompt special command not yet supported in ivy-prompt")
} else {
if opMultiline.MatchString(input) {
input, err = readMultiline(l, input)
if err != nil {
return err
}
}
var resp string
resp, err = ivy.Exec(input)
if err != nil {
return fmt.Errorf("could not execute input: %w", err)
}
fmt.Print(resp)
}
l.AppendHistory(input)
return nil
}
func runDemo() {
cmd := exec.Command("ivy", "-e", ")demo")
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
printErr(fmt.Errorf("could not run demo: %w", err))
}
}
// An io.EOF error is returned if the user signals end-of-file
// by pressing Ctrl-D.
func readMultiline(l *liner.State, firstLine string) (string, error) {
var b strings.Builder
b.WriteString(firstLine)
for {
line, err := l.Prompt(" ")
if err != nil {
return "", err
}
b.WriteByte('\n')
b.WriteString(line)
if strings.TrimSpace(line) == "" {
break
}
}
return b.String(), nil
}
func check(err error) {
if err != nil {
printErr(err)
os.Exit(1)
}
}
func printErr(err error) {
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
}