-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ansi): support OSC 7 notify working directory (#274)
* feat(ansi): support OSC 7 notify working directory * Fix extra ; * Remove unused param * Add links * Update ansi/cwd.go Co-authored-by: Ayman Bagabas <ayman.bagabas@gmail.com> * Use list of strings Co-authored-by: Ayman Bagabas <ayman.bagabas@gmail.com> * Apply suggestions from code review * fix import --------- Co-authored-by: Ayman Bagabas <ayman.bagabas@gmail.com>
- Loading branch information
1 parent
08d3e3b
commit a372054
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package ansi | ||
|
||
import ( | ||
"net/url" | ||
"path" | ||
) | ||
|
||
// NotifyWorkingDirectory returns a sequence that notifies the terminal | ||
// of the current working directory. | ||
// | ||
// OSC 7 ; Pt BEL | ||
// | ||
// Where Pt is a URL in the format "file://[host]/[path]". | ||
// Set host to "localhost" if this is a path on the local computer. | ||
// | ||
// See: https://wezfurlong.org/wezterm/shell-integration.html#osc-7-escape-sequence-to-set-the-working-directory | ||
// See: https://iterm2.com/documentation-escape-codes.html#:~:text=RemoteHost%20and%20CurrentDir%3A-,OSC%207,-%3B%20%5BPs%5D%20ST | ||
func NotifyWorkingDirectory(host string, paths ...string) string { | ||
path := path.Join(paths...) | ||
u := &url.URL{ | ||
Scheme: "file", | ||
Host: host, | ||
Path: path, | ||
} | ||
return "\x1b]7;" + u.String() + "\x07" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ansi_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/charmbracelet/x/ansi" | ||
) | ||
|
||
func TestNotifyWorkingDirectory_LocalFile(t *testing.T) { | ||
h := ansi.NotifyWorkingDirectory("localhost", "path", "to", "file") | ||
if h != "\x1b]7;file://localhost/path/to/file\x07" { | ||
t.Errorf("Unexpected url: %s", h) | ||
} | ||
} | ||
|
||
func TestNotifyWorkingDirectory_RemoteFile(t *testing.T) { | ||
h := ansi.NotifyWorkingDirectory("example.com", "path", "to", "file") | ||
if h != "\x1b]7;file://example.com/path/to/file\x07" { | ||
t.Errorf("Unexpected url: %s", h) | ||
} | ||
} |