Program resume
Use OSC 7577 to tell the terminal how to bring your program back after the terminal restarts. The program reports a resume command. After a restart, a supporting terminal can run that command again. Resumption is a hint: the terminal decides whether to store the command and whether to run it.
Detect support
A supporting terminal sets OSC7577_TOKEN in the environment of the first
process in each pane. Program::supports_program_resume returns true when
the token is present. The protocol has no query and no reply.
Without the token, set_program_resume and clear_program_resume write
nothing and return Ok. An application can call them in every terminal.
Report a resume command
Program::set_program_resume sends and flushes a report. Each report names
the current process ID and the current time. It also carries a SHA-256 hash
of the token, which lets the terminal reject reports from displayed text or
remote hosts. uncurses never writes the token itself.
use uncurses::event::Event;
use uncurses::program::{Program, ProgramResume};
fn main() -> std::io::Result<()> {
let mut program = Program::stdio()?;
let result = (|| {
program.init()?;
let mut report = ProgramResume::new(&["my-editor", "--session", "8f2c1d"]);
report.cwd = Some("/home/user/src/project");
program.set_program_resume(&report)?;
while !matches!(program.read_event()?, Event::KeyPress(_)) {}
program.clear_program_resume()
})();
let finish = program.finish();
result.and(finish)
}Supply plain UTF-8 text; uncurses performs base64 encoding. The first
element of argv is a bare command name or an absolute path for the host
platform. The terminal
looks up a bare name on its own PATH when it resumes the program. Use an
absolute path when the program depends on which executable the name finds.
The command runs in a new environment. Put each value that the program needs,
such as a session ID or a state file, in argv. The terminal can run the
command more than one time, or not at all, so make it safe to repeat.
cwd is optional. Set it to an absolute path when the working directory
matters.
Keep the record current
Each report replaces the earlier one. Report again in these cases:
- The resume command changes, for example when the user opens a different session.
- The working directory in
cwdchanges. - The program gets the terminal back after
resume. While the program was stopped, another program could replace the record.
The terminal owns the record. pause, resume, screen switches, and
finish() leave it unchanged.
Call clear_program_resume when the user ends the program. When the
program exits because of SIGHUP, SIGTERM, or the loss of its terminal,
keep the record: those events also occur when the terminal itself ends,
which is when the terminal needs the record.
Commands that do not fit
The writer validates the command before it writes any output. These
commands return io::ErrorKind::InvalidInput:
- An empty
argv, or a first element that is empty, relative, or starts with-. - Text with a control character, such as a newline.
- A
cwdthat is not an absolute path for the host platform. - More than 64 elements, or a complete sequence longer than 2048 bytes.
When the command is invalid, set_program_resume first sends a clear report,
then returns the error. Every supporting terminal accepts sequences within
those limits. Without the clear report, a terminal that ignored the new
command could resume the earlier one.
Protect the token
Any program with the token can register a command that the terminal runs later with the user’s rights. Remove the token from the programs that your program starts:
use std::process::Command;
use uncurses::ansi::program_resume::TOKEN_ENV;
fn main() -> std::io::Result<()> {
Command::new("git")
.arg("status")
.env_remove(TOKEN_ENV)
.status()?;
Ok(())
}Remove it for each child, as shown. Program::stdio reads the live process
environment, so the program keeps its own access to the token. Do not
forward the token over ssh or into a container.
Output without a session
uncurses::ansi::program_resume::write_program_resume and
write_program_resume_clear write to any std::io::Write. They take the
process ID, the time, and the token from the caller and leave flushing to
the caller. A wrapper can use them to report for the process that it starts.
Run cargo run -p examples --example program_resume for a counter that
reports its count in the resume command. Add -- --preview to print an
escaped report built with a placeholder token, without a terminal session.
See the OSC 7577 specification for protocol details.
