Skip to content

Commit

Permalink
fix: avoid error when duplicating wsl tab
Browse files Browse the repository at this point in the history
use same osc escape code everywhere

detect wsl using env variable
  • Loading branch information
lnu authored and JanDeDobbeleer committed Feb 15, 2021
1 parent 860eeb4 commit 6add3bf
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/ansi_formats.go
Expand Up @@ -37,14 +37,14 @@ func (a *ansiFormats) init(shell string) {
a.clearEOL = "%{\x1b[K%}"
a.saveCursorPosition = "%{\x1b7%}"
a.restoreCursorPosition = "%{\x1b8%}"
a.title = "%%{\033]0;%s\007%%}"
a.title = "%%{\x1b]0;%s\007%%}"
a.colorSingle = "%%{\x1b[%sm%%}%s%%{\x1b[0m%%}"
a.colorFull = "%%{\x1b[%sm\x1b[%sm%%}%s%%{\x1b[0m%%}"
a.colorTransparent = "%%{\x1b[%s;49m\x1b[7m%%}%s%%{\x1b[m\x1b[0m%%}"
a.escapeLeft = "%{"
a.escapeRight = "%}"
a.hyperlink = "%%{\x1b]8;;%s\x1b\\%%}%s%%{\x1b]8;;\x1b\\%%}"
a.osc99 = "%%{\x1b]9;9;%s\x1b7%%}"
a.osc99 = "%%{\x1b]9;9;%s\x1b\\%%}"
case bash:
a.linechange = "\\[\x1b[%d%s\\]"
a.left = "\\[\x1b[%dC\\]"
Expand All @@ -53,14 +53,14 @@ func (a *ansiFormats) init(shell string) {
a.clearEOL = "\\[\x1b[K\\]"
a.saveCursorPosition = "\\[\x1b7\\]"
a.restoreCursorPosition = "\\[\x1b8\\]"
a.title = "\\[\033]0;%s\007\\]"
a.title = "\\[\x1b]0;%s\007\\]"
a.colorSingle = "\\[\x1b[%sm\\]%s\\[\x1b[0m\\]"
a.colorFull = "\\[\x1b[%sm\x1b[%sm\\]%s\\[\x1b[0m\\]"
a.colorTransparent = "\\[\x1b[%s;49m\x1b[7m\\]%s\\[\x1b[m\x1b[0m\\]"
a.escapeLeft = "\\["
a.escapeRight = "\\]"
a.hyperlink = "\\[\x1b]8;;%s\x1b\\\\\\]%s\\[\x1b]8;;\x1b\\\\\\]"
a.osc99 = "\\[\x1b]9;9;%s\x1b7\\]"
a.osc99 = "\\[\x1b]9;9;%s\x1b\\\\\\]"
default:
a.linechange = "\x1b[%d%s"
a.left = "\x1b[%dC"
Expand All @@ -69,14 +69,14 @@ func (a *ansiFormats) init(shell string) {
a.clearEOL = "\x1b[K"
a.saveCursorPosition = "\x1b7"
a.restoreCursorPosition = "\x1b8"
a.title = "\033]0;%s\007"
a.title = "\x1b]0;%s\007"
a.colorSingle = "\x1b[%sm%s\x1b[0m"
a.colorFull = "\x1b[%sm\x1b[%sm%s\x1b[0m"
a.colorTransparent = "\x1b[%s;49m\x1b[7m%s\x1b[m\x1b[0m"
a.escapeLeft = ""
a.escapeRight = ""
a.hyperlink = "\x1b]8;;%s\x1b\\%s\x1b]8;;\x1b\\"
a.osc99 = "\x1b]9;9;%s\x1b7"
a.osc99 = "\x1b]9;9;%s\x1b\\"
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/engine.go
Expand Up @@ -163,7 +163,15 @@ func (e *engine) render() {
if e.settings.FinalSpace {
e.renderer.write(" ")
}
e.renderer.osc99(e.env.getcwd())

cwd := e.env.getcwd()

// temp to fix 3.89 issue and add wsl support
if e.env.isWsl() {
// transform path
cwd, _ = e.env.runCommand("wslpath", "-m", cwd)
}
e.renderer.osc99(cwd)
e.print()
}

Expand Down
1 change: 1 addition & 0 deletions src/environment.go
Expand Up @@ -63,6 +63,7 @@ type environmentInfo interface {
getWindowTitle(imageName, windowTitleRegex string) (string, error)
doGet(url string) ([]byte, error)
hasParentFilePath(path string) (fileInfo *fileInfo, err error)
isWsl() bool
}

type commandCache struct {
Expand Down
8 changes: 8 additions & 0 deletions src/environment_unix.go
Expand Up @@ -18,3 +18,11 @@ func (env *environment) homeDir() string {
func (env *environment) getWindowTitle(imageName, windowTitleRegex string) (string, error) {
return "", errors.New("not implemented")
}

func (env *environment) isWsl() bool {
// one way to check
// version := env.getFileContent("/proc/version")
// return strings.Contains(version, "microsoft")
// using env variable
return env.getenv("WSL_DISTRO_NAME") != ""
}
4 changes: 4 additions & 0 deletions src/environment_windows.go
Expand Up @@ -53,3 +53,7 @@ func (env *environment) homeDir() string {
func (env *environment) getWindowTitle(imageName, windowTitleRegex string) (string, error) {
return getWindowTitle(imageName, windowTitleRegex)
}

func (env *environment) isWsl() bool {
return false
}
4 changes: 4 additions & 0 deletions src/segment_path_test.go
Expand Up @@ -132,6 +132,10 @@ func (env *MockedEnvironment) hasParentFilePath(path string) (*fileInfo, error)
return args.Get(0).(*fileInfo), args.Error(1)
}

func (env *MockedEnvironment) isWsl() bool {
return false
}

const (
homeBill = "/home/bill"
homeJan = "/usr/home/jan"
Expand Down

0 comments on commit 6add3bf

Please sign in to comment.