-
Notifications
You must be signed in to change notification settings - Fork 20
/
rescribe.go
109 lines (88 loc) · 2.81 KB
/
rescribe.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
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package script
import (
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/jetsetilly/gopher2600/debugger/terminal"
)
const commentLine = "#"
// check if line is prepended with commentLine (ignoring leading spaces).
func isComment(line string) bool {
return strings.HasPrefix(strings.TrimSpace(line), commentLine)
}
// Rescribe represents an previously scribed script. The type implements the
// terminal.UserRead interface.
type Rescribe struct {
scriptFile string
lines []string
lineCt int
}
// RescribeScript is the preferred method of initialisation for the Rescribe
// type.
func RescribeScript(scriptfile string) (*Rescribe, error) {
// open script and defer closing
f, err := os.Open(scriptfile)
if err != nil {
return nil, fmt.Errorf("rescribe: %w", err)
}
defer f.Close()
buffer, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("rescribe: %w", err)
}
scr := &Rescribe{scriptFile: scriptfile}
// convert buffer to an array of lines
l := strings.Split(string(buffer), "\n")
// allocate enough memory for real line array
scr.lines = make([]string, 0, len(l))
// keep lines that are not empty and not a comment
for i := range l {
l[i] = strings.TrimSpace(l[i])
if len(l[i]) > 0 && !isComment(l[i]) {
scr.lines = append(scr.lines, l[i])
}
}
// reset line counter
scr.lineCt = 0
return scr, nil
}
// IsInteractive implements the terminal.Input interface.
func (scr *Rescribe) IsInteractive() bool {
return false
}
// IsRealTerminal implements the terminal.Input interface.
func (scr *Rescribe) IsRealTerminal() bool {
return false
}
// sentinal error returned when Rescribe.TermRead() reaches the expected end of the script.
var ScriptEnd = errors.New("end of rescribe")
// TermRead implements the terminal.Input interface.
func (scr *Rescribe) TermRead(buffer []byte, _ terminal.Prompt, _ *terminal.ReadEvents) (int, error) {
if scr.lineCt > len(scr.lines)-1 {
return -1, ScriptEnd
}
n := len(scr.lines[scr.lineCt]) + 1
copy(buffer, scr.lines[scr.lineCt])
scr.lineCt++
return n, nil
}
// TermReadCheck implements the terminal.Input interface.
func (scr *Rescribe) TermReadCheck() bool {
return false
}