finalterm.go

 1package ansi
 2
 3import "strings"
 4
 5// FinalTerm returns an escape sequence that is used for shell integrations.
 6// Originally, FinalTerm designed the protocol hence the name.
 7//
 8//	OSC 133 ; Ps ; Pm ST
 9//	OSC 133 ; Ps ; Pm BEL
10//
11// See: https://iterm2.com/documentation-shell-integration.html
12func FinalTerm(pm ...string) string {
13	return "\x1b]133;" + strings.Join(pm, ";") + "\x07"
14}
15
16// FinalTermPrompt returns an escape sequence that is used for shell
17// integrations prompt marks. This is sent just before the start of the shell
18// prompt.
19//
20// This is an alias for FinalTerm("A").
21func FinalTermPrompt(pm ...string) string {
22	if len(pm) == 0 {
23		return FinalTerm("A")
24	}
25	return FinalTerm(append([]string{"A"}, pm...)...)
26}
27
28// FinalTermCmdStart returns an escape sequence that is used for shell
29// integrations command start marks. This is sent just after the end of the
30// shell prompt, before the user enters a command.
31//
32// This is an alias for FinalTerm("B").
33func FinalTermCmdStart(pm ...string) string {
34	if len(pm) == 0 {
35		return FinalTerm("B")
36	}
37	return FinalTerm(append([]string{"B"}, pm...)...)
38}
39
40// FinalTermCmdExecuted returns an escape sequence that is used for shell
41// integrations command executed marks. This is sent just before the start of
42// the command output.
43//
44// This is an alias for FinalTerm("C").
45func FinalTermCmdExecuted(pm ...string) string {
46	if len(pm) == 0 {
47		return FinalTerm("C")
48	}
49	return FinalTerm(append([]string{"C"}, pm...)...)
50}
51
52// FinalTermCmdFinished returns an escape sequence that is used for shell
53// integrations command finished marks.
54//
55// If the command was sent after
56// [FinalTermCmdStart], it indicates that the command was aborted. If the
57// command was sent after [FinalTermCmdExecuted], it indicates the end of the
58// command output. If neither was sent, [FinalTermCmdFinished] should be
59// ignored.
60//
61// This is an alias for FinalTerm("D").
62func FinalTermCmdFinished(pm ...string) string {
63	if len(pm) == 0 {
64		return FinalTerm("D")
65	}
66	return FinalTerm(append([]string{"D"}, pm...)...)
67}