fix(osc7): correct format

relates to #2515
This commit is contained in:
Jan De Dobbeleer 2022-07-26 08:28:39 +02:00 committed by Jan De Dobbeleer
parent b5b35d1456
commit fe2e5fd1d3
3 changed files with 17 additions and 11 deletions

View file

@ -32,7 +32,8 @@ type Ansi struct {
escapeRight string escapeRight string
hyperlink string hyperlink string
hyperlinkRegex string hyperlinkRegex string
pwd string osc99 string
osc7 string
bold string bold string
italic string italic string
underline string underline string
@ -65,7 +66,8 @@ func (a *Ansi) Init(shellName string) {
a.escapeRight = "%}" a.escapeRight = "%}"
a.hyperlink = "%%{\x1b]8;;%s\x1b\\%%}%s%%{\x1b]8;;\x1b\\%%}" a.hyperlink = "%%{\x1b]8;;%s\x1b\\%%}%s%%{\x1b]8;;\x1b\\%%}"
a.hyperlinkRegex = `(?P<STR>%{\x1b]8;;(.+)\x1b\\%}(?P<TEXT>.+)%{\x1b]8;;\x1b\\%})` a.hyperlinkRegex = `(?P<STR>%{\x1b]8;;(.+)\x1b\\%}(?P<TEXT>.+)%{\x1b]8;;\x1b\\%})`
a.pwd = "%%{\x1b]%s;\"%s\"\x1b\\%%}" a.osc99 = "%%{\x1b]9;9;\"%s\"\x1b\\%%}"
a.osc7 = "%%{\x1b]7;file://%s/%s\x1b\\%%}"
a.bold = "%%{\x1b[1m%%}%s%%{\x1b[22m%%}" a.bold = "%%{\x1b[1m%%}%s%%{\x1b[22m%%}"
a.italic = "%%{\x1b[3m%%}%s%%{\x1b[23m%%}" a.italic = "%%{\x1b[3m%%}%s%%{\x1b[23m%%}"
a.underline = "%%{\x1b[4m%%}%s%%{\x1b[24m%%}" a.underline = "%%{\x1b[4m%%}%s%%{\x1b[24m%%}"
@ -92,7 +94,8 @@ func (a *Ansi) Init(shellName string) {
a.escapeRight = "\\]" a.escapeRight = "\\]"
a.hyperlink = "\\[\x1b]8;;%s\x1b\\\\\\]%s\\[\x1b]8;;\x1b\\\\\\]" a.hyperlink = "\\[\x1b]8;;%s\x1b\\\\\\]%s\\[\x1b]8;;\x1b\\\\\\]"
a.hyperlinkRegex = `(?P<STR>\\\[\x1b\]8;;(.+)\x1b\\\\\\\](?P<TEXT>.+)\\\[\x1b\]8;;\x1b\\\\\\\])` a.hyperlinkRegex = `(?P<STR>\\\[\x1b\]8;;(.+)\x1b\\\\\\\](?P<TEXT>.+)\\\[\x1b\]8;;\x1b\\\\\\\])`
a.pwd = "\\[\x1b]%s;\"%s\"\x1b\\\\\\]" a.osc99 = "\\[\x1b]9;9;\"%s\"\x1b\\\\\\]"
a.osc7 = "\\[\x1b]7;file://%s/%s\x1b\\\\]"
a.bold = "\\[\x1b[1m\\]%s\\[\x1b[22m\\]" a.bold = "\\[\x1b[1m\\]%s\\[\x1b[22m\\]"
a.italic = "\\[\x1b[3m\\]%s\\[\x1b[23m\\]" a.italic = "\\[\x1b[3m\\]%s\\[\x1b[23m\\]"
a.underline = "\\[\x1b[4m\\]%s\\[\x1b[24m\\]" a.underline = "\\[\x1b[4m\\]%s\\[\x1b[24m\\]"
@ -119,7 +122,8 @@ func (a *Ansi) Init(shellName string) {
a.escapeRight = "" a.escapeRight = ""
a.hyperlink = "\x1b]8;;%s\x1b\\%s\x1b]8;;\x1b\\" a.hyperlink = "\x1b]8;;%s\x1b\\%s\x1b]8;;\x1b\\"
a.hyperlinkRegex = "(?P<STR>\x1b]8;;(.+)\x1b\\\\\\\\?(?P<TEXT>.+)\x1b]8;;\x1b\\\\)" a.hyperlinkRegex = "(?P<STR>\x1b]8;;(.+)\x1b\\\\\\\\?(?P<TEXT>.+)\x1b]8;;\x1b\\\\)"
a.pwd = "\x1b]%s;\"%s\"\x1b\\" a.osc99 = "\x1b]9;9;\"%s\"\x1b\\"
a.osc7 = "\x1b]7;file://%s/%s\x1b\\"
a.bold = "\x1b[1m%s\x1b[22m" a.bold = "\x1b[1m%s\x1b[22m"
a.italic = "\x1b[3m%s\x1b[23m" a.italic = "\x1b[3m%s\x1b[23m"
a.underline = "\x1b[4m%s\x1b[24m" a.underline = "\x1b[4m%s\x1b[24m"
@ -243,17 +247,17 @@ func (a *Ansi) ChangeLine(numberOfLines int) string {
return fmt.Sprintf(a.linechange, numberOfLines, position) return fmt.Sprintf(a.linechange, numberOfLines, position)
} }
func (a *Ansi) ConsolePwd(pwdType, pwd string) string { func (a *Ansi) ConsolePwd(pwdType, hostName, pwd string) string {
if strings.HasSuffix(pwd, ":") { if strings.HasSuffix(pwd, ":") {
pwd += "\\" pwd += "\\"
} }
switch pwdType { switch pwdType {
case OSC7: case OSC7:
return fmt.Sprintf(a.pwd, "7", pwd) return fmt.Sprintf(a.osc7, hostName, pwd)
case OSC99: case OSC99:
fallthrough fallthrough
default: default:
return fmt.Sprintf(a.pwd, "9;9", pwd) return fmt.Sprintf(a.osc99, pwd)
} }
} }

View file

@ -85,7 +85,7 @@ func (e *Engine) printPWD() {
cwd := e.Env.Pwd() cwd := e.Env.Pwd()
// Backwards compatibility for deprecated OSC99 // Backwards compatibility for deprecated OSC99
if e.Config.OSC99 { if e.Config.OSC99 {
e.writeANSI(e.Ansi.ConsolePwd(color.OSC99, cwd)) e.writeANSI(e.Ansi.ConsolePwd(color.OSC99, "", cwd))
return return
} }
// Allow template logic to define when to enable the PWD (when supported) // Allow template logic to define when to enable the PWD (when supported)
@ -97,7 +97,8 @@ func (e *Engine) printPWD() {
if err != nil || len(pwdType) == 0 { if err != nil || len(pwdType) == 0 {
return return
} }
e.writeANSI(e.Ansi.ConsolePwd(pwdType, cwd)) host, _ := e.Env.Host()
e.writeANSI(e.Ansi.ConsolePwd(pwdType, host, cwd))
} }
func (e *Engine) newline() { func (e *Engine) newline() {

View file

@ -53,16 +53,17 @@ func TestPrintPWD(t *testing.T) {
}{ }{
{Case: "Empty PWD"}, {Case: "Empty PWD"},
{Case: "OSC99", PWD: color.OSC99, Expected: "\x1b]9;9;\"pwd\"\x1b\\"}, {Case: "OSC99", PWD: color.OSC99, Expected: "\x1b]9;9;\"pwd\"\x1b\\"},
{Case: "OSC7", PWD: color.OSC7, Expected: "\x1b]7;\"pwd\"\x1b\\"}, {Case: "OSC7", PWD: color.OSC7, Expected: "\x1b]7;file://host/pwd\x1b\\"},
{Case: "Deprecated OSC99", OSC99: true, Expected: "\x1b]9;9;\"pwd\"\x1b\\"}, {Case: "Deprecated OSC99", OSC99: true, Expected: "\x1b]9;9;\"pwd\"\x1b\\"},
{Case: "Template (empty)", PWD: "{{ if eq .Shell \"pwsh\" }}osc7{{ end }}"}, {Case: "Template (empty)", PWD: "{{ if eq .Shell \"pwsh\" }}osc7{{ end }}"},
{Case: "Template (non empty)", PWD: "{{ if eq .Shell \"shell\" }}osc7{{ end }}", Expected: "\x1b]7;\"pwd\"\x1b\\"}, {Case: "Template (non empty)", PWD: "{{ if eq .Shell \"shell\" }}osc7{{ end }}", Expected: "\x1b]7;file://host/pwd\x1b\\"},
} }
for _, tc := range cases { for _, tc := range cases {
env := new(mock.MockedEnvironment) env := new(mock.MockedEnvironment)
env.On("Pwd").Return("pwd") env.On("Pwd").Return("pwd")
env.On("Shell").Return("shell") env.On("Shell").Return("shell")
env.On("Host").Return("host", nil)
env.On("TemplateCache").Return(&environment.TemplateCache{ env.On("TemplateCache").Return(&environment.TemplateCache{
Env: make(map[string]string), Env: make(map[string]string),
Shell: "shell", Shell: "shell",