Skip to content

Commit

Permalink
feat(ansi): support OSC 7 notify working directory (#274)
Browse files Browse the repository at this point in the history
* 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
rzhw and aymanbagabas authored Nov 21, 2024
1 parent 08d3e3b commit a372054
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
26 changes: 26 additions & 0 deletions ansi/cwd.go
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"
}
21 changes: 21 additions & 0 deletions ansi/cwd_test.go
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)
}
}

0 comments on commit a372054

Please sign in to comment.