Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
170 lines (161 loc) · 5.25 KB
/
Copy pathmain.go
File metadata and controls
170 lines (161 loc) · 5.25 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"os"
"os/signal"
"strings"
"syscall"
"golang.org/x/term"
"nimbleterminal.dev/ntrecord/internal/doctor"
"nimbleterminal.dev/ntrecord/internal/encode"
"nimbleterminal.dev/ntrecord/internal/record"
"nimbleterminal.dev/ntrecord/internal/tape"
)
// version is stamped by GoReleaser and task build VERSION=... .
var version = "dev"
func main() {
// A closed stdout pipe must return EPIPE so Run can save captured frames.
// Notify also leaves the normal SIGPIPE disposition for exec'd children.
sigpipe := make(chan os.Signal, 1)
signal.Notify(sigpipe, syscall.SIGPIPE)
defer signal.Stop(sigpipe)
if err := run(os.Args[1:], os.Stdout, os.Stderr); err != nil {
fmt.Fprintln(os.Stderr, "ntrecord:", err)
os.Exit(1)
}
}
const usage = `Usage: ntrecord [-mode realtime|deterministic] <file.tape>
ntrecord record [-o output] [-shell executable] [-cols n] [-rows n] [-font-size pixels] [-- command args...]
ntrecord play <file.cast> [-paused] [-speed n]
ntrecord render <file.cast> -o <output> [-font-size pixels] [-fps n] [-speed n]
ntrecord validate <file.tape>...
ntrecord doctor
ntrecord skill show
ntrecord skill install [FOLDER]
ntrecord --version
-mode picks the clock for a tape. realtime (the default) samples the screen as time
passes and captures animations. deterministic runs a virtual clock: each step is
held as long as the tape says, once the program settles, so the same tape gives
the same recording and Sleep costs no real time. Use Wait for anything slow.
validate checks tapes (syntax, fonts, themes, Require, outputs) without recording.
doctor checks that this machine can record.
skill show prints the skill that teaches an agent to use ntrecord; skill install
installs it for the agents on this machine.`
func run(args []string, stdout, stderr io.Writer) error {
if len(args) == 1 && (args[0] == "--version" || args[0] == "-version" || args[0] == "version") {
fmt.Fprintln(stdout, "ntrecord", version)
return nil
}
if len(args) == 0 || args[0] == "-h" || args[0] == "--help" {
fmt.Fprintln(stdout, usage)
return nil
}
switch args[0] {
case "validate":
return validate(args[1:], stdout, stderr)
case "skill":
return skillCommand(args[1:], stdout, stderr)
case "doctor":
if !doctor.Run(stdout, doctor.Options{Version: version}) {
return errors.New("doctor found problems")
}
return nil
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if args[0] == "play" {
return playCast(ctx, args[1:], stderr)
}
if args[0] == "render" {
return renderCast(ctx, args[1:], stderr)
}
opts := record.Options{Config: tape.DefaultConfig(), Warn: stderr}
opts.Config.Version = version
if args[0] == "record" {
fs := flag.NewFlagSet("record", flag.ContinueOnError)
fs.StringVar(&opts.Config.Output, "o", opts.Config.Output, encode.OutputHelp())
fs.StringVar(&opts.Config.Shell, "shell", opts.Config.Shell, "interactive shell executable")
fs.IntVar(&opts.Config.Cols, "cols", opts.Config.Cols, "PTY columns")
fs.IntVar(&opts.Config.Rows, "rows", opts.Config.Rows, "PTY rows")
fs.Float64Var(&opts.Config.FontSize, "font-size", opts.Config.FontSize, "font size in pixels")
if err := fs.Parse(args[1:]); err != nil {
if errors.Is(err, flag.ErrHelp) {
return nil
}
return err
}
opts.Command, opts.Input, opts.Output = fs.Args(), os.Stdin, stdout
if term.IsTerminal(int(os.Stdin.Fd())) {
state, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
return err
}
defer term.Restore(int(os.Stdin.Fd()), state)
}
} else {
fs := flag.NewFlagSet("ntrecord", flag.ContinueOnError)
fs.SetOutput(stderr)
modeFlag := fs.String("mode", string(tape.Realtime), "clock: realtime or deterministic")
if err := fs.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
return nil
}
return err
}
if fs.NArg() != 1 {
return fmt.Errorf("expected one tape path")
}
mode, err := tape.ParseMode(*modeFlag)
if err != nil {
return err
}
t, err := tape.ParseFile(fs.Arg(0))
if err != nil {
return err
}
opts.Config, opts.Tape = t.Config, t
opts.Config.Version = version
opts.Config.Mode = mode
}
err := record.Run(ctx, opts)
if err != nil {
return err
}
for _, path := range opts.Config.Destinations() {
fmt.Fprintln(stderr, "Wrote", path)
}
return nil
}
// validate checks each tape without recording and reports every problem, so one
// run shows all that needs fixing.
func validate(paths []string, stdout, stderr io.Writer) error {
if len(paths) == 0 {
return errors.New("validate: expected one or more tape paths")
}
failed := 0
for _, path := range paths {
t, err := tape.ParseFile(path)
if err != nil { // already names the file, line and column
fmt.Fprintln(stderr, err)
failed++
continue
}
t.Config.Version = version
s, err := record.Validate(t)
if err != nil {
fmt.Fprintf(stderr, "%s: %v\n", path, err)
failed++
continue
}
fmt.Fprintf(stdout, "%s: ok — %d commands · %dx%d cells, %dx%d px · outputs: %s\n",
path, s.Commands, s.Cols, s.Rows, s.Width, s.Height, strings.Join(s.Outputs, ", "))
}
if failed > 0 {
return fmt.Errorf("%d of %d tapes invalid", failed, len(paths))
}
return nil
}