Documentation
¶
Overview ¶
Package progresume reads and writes the Program Resume Protocol (OSC 7577), revision 0.5.
A program tells its terminal how to bring it back after the terminal restarts: an argument list, an owning pid, the time, and a hash that proves the program knows a token the terminal put in the environment of the pane's first process (OSC7577_TOKEN). The terminal checks the hash and the time before anything else, so text that only passes through a pane (a file shown with cat, the output of a remote host) cannot make a record.
Parse is the terminal side. Report and Clear are the program side: they read the token once, take it out of the environment, sign the report and write it to the controlling terminal. Sequence builds a signed report for a caller that writes it itself. Parse keeps no state: the record store, the owner check and the order of reports for one pid are the terminal's.
Example ¶
A program reports how to start it again when it opens a session file, and clears the report when the person quits it on purpose.
package main
import (
"errors"
"log"
"tuios.dev/progresume"
)
func main() {
err := progresume.Report([]string{"nvim", "-S", "/home/user/.local/state/nvim/sessions/8f2c1d.vim"}, "")
if err != nil && !errors.Is(err, progresume.ErrNoToken) {
log.Printf("cannot report: %v", err)
}
// ... the program runs ...
_ = progresume.Clear()
}
Output:
Index ¶
Examples ¶
Constants ¶
const Command = 7577
Command is the OSC number of the protocol.
const MaxArgv = 64
MaxArgv is the most argv elements accepted, the minimum the specification asks for.
const MaxSequence = 2048
MaxSequence is the longest report accepted, in bytes, counted over the whole sequence from the OSC opener to the terminator. It is the minimum the specification asks a terminal to accept.
const TokenEnv = "OSC7577_TOKEN"
TokenEnv is the variable that holds a pane's token. A program that finds it may report, and a program that does not has no token to sign with.
const Window = 60 * time.Second
Window is how far a report's time may be from the terminal's clock.
Variables ¶
var ErrInvalid = errors.New("progresume: the report is not valid")
ErrInvalid is what Report returns for a report that breaks a rule of the protocol. Nothing was sent.
var ErrNoToken = errors.New("progresume: the terminal gave no " + TokenEnv)
ErrNoToken is what Report and Clear return when the terminal gave this process no token. The terminal does not support the protocol, or another process already took the token. A program can ignore it.
var Implemented = []string{"pid", "time", "mac", "argv", "cwd", "clear", "need"}
Implemented is every key this package reads, which is what a report's need key may name.
Functions ¶
func Clear ¶
func Clear() error
Clear tells the terminal that this program has nothing to resume. See Reporter.Clear.
func NewToken ¶
NewToken returns a fresh token of 32 characters of [A-Za-z0-9_-] from the system's secure random source.
func Report ¶
Report tells the terminal how to start this program again. See Reporter.Report. The first call of Report, Clear or TakeToken takes the token out of the environment. ErrNoToken means the terminal gave none.
func Sequence ¶
Sequence is the whole OSC 7577 sequence for r, signed with token, ended with ST. A report with Clear set carries no argv or cwd.
func TakeToken ¶
func TakeToken()
TakeToken removes OSC7577_TOKEN from the environment of this process without sending anything, so the programs it starts do not get the token. Report and Clear still work after it.
func ValidToken ¶
ValidToken reports whether tok has the form the specification allows: 22 to 64 characters of [A-Za-z0-9_-].
Types ¶
type Message ¶
type Message struct {
// PID is the owning process. 0 when the report named no usable pid.
PID int
// Time is when the report was made, in seconds since the Unix epoch.
Time int64
// Argv is the resume command. Empty for a clear.
Argv []string
// Cwd is the folder to resume in, "" when the report named none.
Cwd string
// Clear asks to remove the owner's record.
Clear bool
}
Message is one report, decoded.
type Outcome ¶
type Outcome int
Outcome is what a terminal does with a report.
const ( // Ignored has no effect at all: the mac or the time is wrong, or the // report is cut short. Ignored Outcome = iota // Rejected passed the mac and the time but breaks a rule. The owner's // records are removed, when the report names a usable pid. Rejected // Accepted passed every check of the protocol. Accepted )
type Reporter ¶
type Reporter struct {
// Token is the terminal's token.
Token string
// PID is the owner the reports name: the process the terminal should
// watch. Report and Clear use the pid of this process.
PID int
// Out is where the reports go: the controlling terminal for Report and
// Clear.
Out io.Writer
// Now is the clock. nil uses time.Now.
Now func() time.Time
}
Reporter sends reports for one program, with the token, pid and output its fields name. The package functions Report and Clear share one Reporter made from the environment.
func (*Reporter) Clear ¶
Clear tells the terminal that the program has nothing to resume, for example when the person quit it on purpose.
func (*Reporter) Report ¶
Report tells the terminal how to start the program again: argv is the command, with argv[0] a bare name that the terminal looks up on PATH or an absolute path. cwd is the folder to start it in, "" for none. A report that breaks a rule of the protocol is not sent, and the error says which.
type Result ¶
type Result struct {
Outcome Outcome
Message Message
// Reason says why a report was ignored or rejected, for a debug log. It
// never holds a value of the report.
Reason string
}
Result is a parsed report and what to do with it. Message.PID and Message.Time are set for a rejected report when it named them.
func Parse ¶
Parse reads the payload of one OSC 7577 sequence, "7577;" and the body, as the emulator hands it over. token is the pane's token and now the terminal's clock. The order is the specification's: the mac, then the time, then the grammar. A report whose time is earlier than the last accepted for its pid must be ignored as well; the caller holds those times.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
resume-wrap
command
Command resume-wrap runs a command so that the terminal can bring it back after a restart (OSC 7577, the Program Resume Protocol).
|
Command resume-wrap runs a command so that the terminal can bring it back after a restart (OSC 7577, the Program Resume Protocol). |