-
Notifications
You must be signed in to change notification settings - Fork 7
/
runtime.go
111 lines (96 loc) · 2.58 KB
/
runtime.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
//go:build !tinygo
// Package cli provides an Evy runtime to for Evy CLI execution in terminal.
package cli
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"time"
"evylang.dev/evy/pkg/cli/svg"
"evylang.dev/evy/pkg/evaluator"
)
// Runtime implements evaluator.Runtime.
type Runtime struct {
evaluator.GraphicsRuntime
reader *bufio.Reader
writer io.Writer
SkipSleep bool
}
// Option is used on Runtime creation to set optional parameters.
type Option func(*Runtime)
// WithSkipSleep sets the SkipSleep field Runtime and is intended to be used
// with NewRuntime.
func WithSkipSleep(skipSleep bool) Option {
return func(rt *Runtime) {
rt.SkipSleep = skipSleep
}
}
// WithSVG sets up an SVG graphics runtime and writes its output to the
// given writer.
func WithSVG(svgStyle string) Option {
return func(rt *Runtime) {
svgRT := svg.NewGraphicsRuntime()
svgRT.SVG.Style = svgStyle
rt.GraphicsRuntime = svgRT
}
}
// WithOutputWriter sets the text output writer, which defaults to os.Stdout.
func WithOutputWriter(w io.Writer) Option {
return func(rt *Runtime) {
rt.writer = w
}
}
// NewRuntime returns an initialized cli runtime.
func NewRuntime(options ...Option) *Runtime {
rt := &Runtime{
reader: bufio.NewReader(os.Stdin),
writer: os.Stdout,
GraphicsRuntime: &evaluator.UnimplementedRuntime{},
}
for _, opt := range options {
opt(rt)
}
return rt
}
// Print prints s to stdout.
func (rt *Runtime) Print(s string) {
fmt.Fprint(rt.writer, s)
}
// Cls clears the screen.
func (*Runtime) Cls() {
cmd := exec.Command("clear")
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", "/c", "cls")
}
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
fmt.Println("cannot clear screen", err)
}
}
// Read reads a line of input from stdin and strips trailing newline.
func (rt *Runtime) Read() string {
s, err := rt.reader.ReadString('\n')
if err != nil {
panic(err)
}
return s[:len(s)-1] // strip trailing newline
}
// Sleep sleeps for dur. If the --skip-sleep flag is used, it does nothing.
func (rt *Runtime) Sleep(dur time.Duration) {
if !rt.SkipSleep {
time.Sleep(dur)
}
}
// Yielder returns a no-op yielder for CLI evy as it is not needed. By
// contrast, browser Evy needs to explicitly hand over control to JS
// host with Yielder.
func (*Runtime) Yielder() evaluator.Yielder { return nil }
// WriteSVG writes the graphics output in SVG format to the writer set with
// option WithSVGWriter.
func (rt *Runtime) WriteSVG(w io.Writer) error {
graphicsRT := rt.GraphicsRuntime.(*svg.GraphicsRuntime)
return graphicsRT.WriteSVG(w)
}