Description:"Read your task's terminal: the shell in this task's worktree that the human uses from the Terminal tab, and that terminal_type types into. "+
"Returns its recent output as plain text, escape sequences removed, at most the last 64 KiB. "+
"Full-screen programs (vim, top, less) come out garbled, since this is the output stream, not a picture of the screen.",
Description:"Type into your task's terminal: the shell in this task's worktree that the human uses from the Terminal tab, starting it when none is running and waiting for its prompt before typing. "+
"keys are named keys sent first (ctrl-c cancels, ctrl-u clears the input line, enter runs it), then text goes to the shell exactly as keystrokes, so end a command with a real line break to run it (an escape you write in text, such as \"\\u0003\", can arrive as those literal characters, so send control keys through keys), or press enter in a later call. "+
"When the input line still holds unsent text after keys, text is refused, so that it isn't joined onto someone else's command: clear it with ctrl-u first, or set append to add to it on purpose. "+
"Replies from this tool and terminal_read end with the shell's current input line when zsh is at its prompt. "+
"To hand the human a command to run (one needing their password, say), type it without the newline and tell them to press Enter in the Terminal tab. In a zsh terminal, the reply then says byre will message you when the shell next returns to its prompt, with the exit status and output, starting a turn if you are idle, so end your turn rather than asking whether they ran it. "+
"Returns the output that followed, as terminal_read does, once the terminal has been quiet for half a second or after 10 seconds; read the rest of a longer command's output with terminal_read.",
out+=fmt.Sprintf("\nThe terminal's input line already has unsent text: %q. Your text was not typed. Send keys [\"ctrl-u\"] to clear it (keys [\"ctrl-c\"] when it continues an unfinished command from earlier lines), or set append to add to it.",line)
text:=fmt.Sprintf("The line you left unsent in the terminal was run, and the shell is back at its prompt with exit status %s (130 means it was cancelled with Ctrl-C). The terminal's output since you typed it:\n\n%s",exit,terminal.Text(out))
// One byte per write: a program that stops reading blocks a write, and the PTY can't be given a deadline, so this keeps what arrives after we give up to one byte.
fori:=0;i<len(text)&&!stop.Load();i++{
err:=term.Write([]byte{text[i]})
iferr!=nil{
written<-err
return
}
typed.Add(1)
}
written<-nil
}()
result:=func()string{
res:=terminal.Text(got)
exit,exited:=term.ExitStatus()
ifexited{
res+="\n[shell ended: "+exit+"]"
}
ifn:=typed.Load();n<int64(len(text)){
res+=fmt.Sprintf("\n[only %d of %d bytes were typed: the terminal stopped reading input]",n,len(text))