Skip to content

Commit 0728507

Browse files
committed
script: Try to get&set terminal size
Instead of defaulting to 80x24, try to get the terminal size and set it. Lovingly tested by hand. Signed-off-by: Jussi Maki <[email protected]>
1 parent b13b664 commit 0728507

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

Diff for: script.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,13 @@ func RunRepl(h *Hive, in *os.File, out *os.File, prompt string) {
124124
io.Reader
125125
io.Writer
126126
}{in, out}
127-
term := term.NewTerminal(inout, prompt)
128-
log := slog.New(slog.NewTextHandler(term, nil))
127+
terminal := term.NewTerminal(inout, prompt)
128+
log := slog.New(slog.NewTextHandler(terminal, nil))
129+
if width, height, err := term.GetSize(int(in.Fd())); err == nil {
130+
if err := terminal.SetSize(width, height); err != nil {
131+
log.Error("Failed to set terminal size", "error", err)
132+
}
133+
}
129134

130135
cmds, err := h.ScriptCommands(log)
131136
if err != nil {
@@ -168,7 +173,7 @@ func RunRepl(h *Hive, in *os.File, out *os.File, prompt string) {
168173
s := newState()
169174

170175
for {
171-
line, err := term.ReadLine()
176+
line, err := terminal.ReadLine()
172177
if err != nil {
173178
if errors.Is(err, io.EOF) {
174179
return
@@ -177,16 +182,16 @@ func RunRepl(h *Hive, in *os.File, out *os.File, prompt string) {
177182
}
178183
}
179184

180-
err = e.ExecuteLine(s, line, term)
185+
err = e.ExecuteLine(s, line, terminal)
181186
if err != nil {
182-
fmt.Fprintln(term, err.Error())
187+
fmt.Fprintln(terminal, err.Error())
183188
}
184189

185190
if s.Context().Err() != nil {
186191
// Context was cancelled due to interrupt. Re-create the state
187192
// to run more commands.
188193
s = newState()
189-
fmt.Fprintln(term, "^C (interrupted)")
194+
fmt.Fprintln(terminal, "^C (interrupted)")
190195
}
191196
}
192197
}

Diff for: script/cmds.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -1205,25 +1205,28 @@ func Break() Cmd {
12051205
defer term.Restore(int(tty.Fd()), prev)
12061206

12071207
// Switch the log output to the terminal until we continue
1208-
term := term.NewTerminal(tty, "debug> ")
1208+
terminal := term.NewTerminal(tty, "debug> ")
1209+
if width, height, err := term.GetSize(int(tty.Fd())); err == nil {
1210+
terminal.SetSize(width, height)
1211+
}
12091212
origLogOut := s.logOut
12101213
defer func() {
12111214
s.logOut = origLogOut
12121215

12131216
}()
1214-
s.logOut = term
1217+
s.logOut = terminal
12151218

1216-
fmt.Fprintf(term, "\nBreak! Control-d to continue.\n")
1219+
fmt.Fprintf(terminal, "\nBreak! Control-d to continue.\n")
12171220

12181221
engine := s.engine
12191222
for {
1220-
line, err := term.ReadLine()
1223+
line, err := terminal.ReadLine()
12211224
if err != nil {
12221225
return nil, nil
12231226
}
1224-
err = engine.ExecuteLine(s, line, term)
1227+
err = engine.ExecuteLine(s, line, terminal)
12251228
if err != nil {
1226-
fmt.Fprintln(term, err.Error())
1229+
fmt.Fprintln(terminal, err.Error())
12271230
}
12281231
}
12291232
},

0 commit comments

Comments
 (0)