Skip to content

Commit ab50b2b

Browse files
rzhwcaarlos0
andauthored
feat: support OSC 7 notify working directory (#25)
* feat: support OSC 7 notify working directory * upgrade x/ansi, fix, add tests * test for invalid url --------- Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
1 parent b24b806 commit ab50b2b

9 files changed

Lines changed: 43 additions & 3 deletions

File tree

cwd.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"net/url"
7+
8+
"github.com/charmbracelet/x/ansi"
9+
)
10+
11+
//nolint:mnd
12+
func handleWorkingDirectoryURL(p *ansi.Parser) (string, error) {
13+
parts := bytes.Split(p.Data(), []byte{';'})
14+
if len(parts) != 2 {
15+
// Invalid, ignore
16+
return "", errInvalid
17+
}
18+
19+
u, err := url.ParseRequestURI(string(parts[1]))
20+
21+
if err != nil || u.Scheme != "file" {
22+
// Should be a file URL.
23+
return "", errInvalid
24+
}
25+
26+
return fmt.Sprintf("Set working directory to %s (on %s)", u.Path, u.Host), nil
27+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22.8
55
require (
66
github.com/charmbracelet/colorprofile v0.1.8
77
github.com/charmbracelet/lipgloss/v2 v2.0.0-alpha.2.0.20241121164047-8448a9be4804
8-
github.com/charmbracelet/x/ansi v0.5.1
8+
github.com/charmbracelet/x/ansi v0.5.2
99
github.com/charmbracelet/x/exp/golden v0.0.0-20241029204245-3ef5e7b1ea37
1010
github.com/charmbracelet/x/term v0.2.1
1111
github.com/charmbracelet/x/xpty v0.1.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/charmbracelet/colorprofile v0.1.8 h1:PywDeXsiAzlPtkiiKgMEVLvb6nlEuKrM
44
github.com/charmbracelet/colorprofile v0.1.8/go.mod h1:+jpmObxZl1Dab3H3IMVIPSZTsKcFpjJUv97G0dLqM60=
55
github.com/charmbracelet/lipgloss/v2 v2.0.0-alpha.2.0.20241121164047-8448a9be4804 h1:7CYjb9YMZA4kMhLgGdtlXvq+nu1oyENpMyMQlTvqSFw=
66
github.com/charmbracelet/lipgloss/v2 v2.0.0-alpha.2.0.20241121164047-8448a9be4804/go.mod h1:F/6E/LGdH3eHCJf2rG8/O3CjlW8cZFL5YJCknJs1GkI=
7-
github.com/charmbracelet/x/ansi v0.5.1 h1:+mg6abP9skvsu/JQZrIJ9Z/4O1YDnLVkpfutar3dUnc=
8-
github.com/charmbracelet/x/ansi v0.5.1/go.mod h1:KBUFw1la39nl0dLl10l5ORDAqGXaeurTQmwyyVKse/Q=
7+
github.com/charmbracelet/x/ansi v0.5.2 h1:dEa1x2qdOZXD/6439s+wF7xjV+kZLu/iN00GuXXrU9E=
8+
github.com/charmbracelet/x/ansi v0.5.2/go.mod h1:KBUFw1la39nl0dLl10l5ORDAqGXaeurTQmwyyVKse/Q=
99
github.com/charmbracelet/x/conpty v0.1.0 h1:4zc8KaIcbiL4mghEON8D72agYtSeIgq8FSThSPQIb+U=
1010
github.com/charmbracelet/x/conpty v0.1.0/go.mod h1:rMFsDJoDwVmiYM10aD4bH2XiRgwI7NYJtQgl5yskjEQ=
1111
github.com/charmbracelet/x/errors v0.0.0-20240508181413-e8d8b6e2de86 h1:JSt3B+U9iqk37QUU2Rvb6DSBYRLtWqFqfxf8l5hOZUA=

handlers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var oscHandlers = map[int]handlerFn{
5555
0: handleTitle,
5656
1: handleTitle,
5757
2: handleTitle,
58+
7: handleWorkingDirectoryURL,
5859
8: handleHyperlink,
5960
9: handleNotify,
6061
10: handleTerminalColor,

main_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ var title = map[string]string{
167167
"invalid cmd": strings.Replace(ansi.SetWindowTitle("hello"), "2", "5", 1),
168168
}
169169

170+
var cwd = map[string]string{
171+
"single part": ansi.NotifyWorkingDirectory("localhost", "foo"),
172+
"multiple parts": ansi.NotifyWorkingDirectory("localhost", "foo", "bar"),
173+
"invalid": strings.Replace(ansi.NotifyWorkingDirectory("localhost", "foo"), ";", "", 1),
174+
"invalid url": strings.Replace(ansi.NotifyWorkingDirectory("localhost", "foo"), "file://localhost/foo", "foooooo:/bar", 1),
175+
}
176+
170177
var hyperlink = map[string]string{
171178
"uri only": ansi.SetHyperlink("https://charm.sh"),
172179
"full": ansi.SetHyperlink("https://charm.sh", "my title"),
@@ -212,6 +219,7 @@ func TestSequences(t *testing.T) {
212219
"kitty": kitty,
213220
"sgr": sgr,
214221
"title": title,
222+
"cwd": cwd,
215223
"hyperlink": hyperlink,
216224
"notify": notify,
217225
"termcolor": termcolor,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OSC 7file://localhost/foo: invalid sequence
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OSC 7;foooooo:/bar: invalid sequence
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OSC 7;file://localhost/foo/bar: Set working directory to /foo/bar (on localhost)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OSC 7;file://localhost/foo: Set working directory to /foo (on localhost)

0 commit comments

Comments
 (0)