Skip to content

Commit

Permalink
feat: support OSC 7 notify working directory (#25)
Browse files Browse the repository at this point in the history
* feat: support OSC 7 notify working directory

* upgrade x/ansi, fix, add tests

* test for invalid url

---------

Co-authored-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
rzhw and caarlos0 authored Nov 22, 2024
1 parent b24b806 commit ab50b2b
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 3 deletions.
27 changes: 27 additions & 0 deletions cwd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"bytes"
"fmt"
"net/url"

"github.com/charmbracelet/x/ansi"
)

//nolint:mnd
func handleWorkingDirectoryURL(p *ansi.Parser) (string, error) {
parts := bytes.Split(p.Data(), []byte{';'})
if len(parts) != 2 {
// Invalid, ignore
return "", errInvalid
}

u, err := url.ParseRequestURI(string(parts[1]))

if err != nil || u.Scheme != "file" {
// Should be a file URL.
return "", errInvalid
}

return fmt.Sprintf("Set working directory to %s (on %s)", u.Path, u.Host), nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.8
require (
github.com/charmbracelet/colorprofile v0.1.8
github.com/charmbracelet/lipgloss/v2 v2.0.0-alpha.2.0.20241121164047-8448a9be4804
github.com/charmbracelet/x/ansi v0.5.1
github.com/charmbracelet/x/ansi v0.5.2
github.com/charmbracelet/x/exp/golden v0.0.0-20241029204245-3ef5e7b1ea37
github.com/charmbracelet/x/term v0.2.1
github.com/charmbracelet/x/xpty v0.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/charmbracelet/colorprofile v0.1.8 h1:PywDeXsiAzlPtkiiKgMEVLvb6nlEuKrM
github.com/charmbracelet/colorprofile v0.1.8/go.mod h1:+jpmObxZl1Dab3H3IMVIPSZTsKcFpjJUv97G0dLqM60=
github.com/charmbracelet/lipgloss/v2 v2.0.0-alpha.2.0.20241121164047-8448a9be4804 h1:7CYjb9YMZA4kMhLgGdtlXvq+nu1oyENpMyMQlTvqSFw=
github.com/charmbracelet/lipgloss/v2 v2.0.0-alpha.2.0.20241121164047-8448a9be4804/go.mod h1:F/6E/LGdH3eHCJf2rG8/O3CjlW8cZFL5YJCknJs1GkI=
github.com/charmbracelet/x/ansi v0.5.1 h1:+mg6abP9skvsu/JQZrIJ9Z/4O1YDnLVkpfutar3dUnc=
github.com/charmbracelet/x/ansi v0.5.1/go.mod h1:KBUFw1la39nl0dLl10l5ORDAqGXaeurTQmwyyVKse/Q=
github.com/charmbracelet/x/ansi v0.5.2 h1:dEa1x2qdOZXD/6439s+wF7xjV+kZLu/iN00GuXXrU9E=
github.com/charmbracelet/x/ansi v0.5.2/go.mod h1:KBUFw1la39nl0dLl10l5ORDAqGXaeurTQmwyyVKse/Q=
github.com/charmbracelet/x/conpty v0.1.0 h1:4zc8KaIcbiL4mghEON8D72agYtSeIgq8FSThSPQIb+U=
github.com/charmbracelet/x/conpty v0.1.0/go.mod h1:rMFsDJoDwVmiYM10aD4bH2XiRgwI7NYJtQgl5yskjEQ=
github.com/charmbracelet/x/errors v0.0.0-20240508181413-e8d8b6e2de86 h1:JSt3B+U9iqk37QUU2Rvb6DSBYRLtWqFqfxf8l5hOZUA=
Expand Down
1 change: 1 addition & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var oscHandlers = map[int]handlerFn{
0: handleTitle,
1: handleTitle,
2: handleTitle,
7: handleWorkingDirectoryURL,
8: handleHyperlink,
9: handleNotify,
10: handleTerminalColor,
Expand Down
8 changes: 8 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ var title = map[string]string{
"invalid cmd": strings.Replace(ansi.SetWindowTitle("hello"), "2", "5", 1),
}

var cwd = map[string]string{
"single part": ansi.NotifyWorkingDirectory("localhost", "foo"),
"multiple parts": ansi.NotifyWorkingDirectory("localhost", "foo", "bar"),
"invalid": strings.Replace(ansi.NotifyWorkingDirectory("localhost", "foo"), ";", "", 1),
"invalid url": strings.Replace(ansi.NotifyWorkingDirectory("localhost", "foo"), "file://localhost/foo", "foooooo:/bar", 1),
}

var hyperlink = map[string]string{
"uri only": ansi.SetHyperlink("https://charm.sh"),
"full": ansi.SetHyperlink("https://charm.sh", "my title"),
Expand Down Expand Up @@ -212,6 +219,7 @@ func TestSequences(t *testing.T) {
"kitty": kitty,
"sgr": sgr,
"title": title,
"cwd": cwd,
"hyperlink": hyperlink,
"notify": notify,
"termcolor": termcolor,
Expand Down
1 change: 1 addition & 0 deletions testdata/TestSequences/cwd/invalid.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OSC 7file://localhost/foo: invalid sequence
1 change: 1 addition & 0 deletions testdata/TestSequences/cwd/invalid_url.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OSC 7;foooooo:/bar: invalid sequence
1 change: 1 addition & 0 deletions testdata/TestSequences/cwd/multiple_parts.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OSC 7;file://localhost/foo/bar: Set working directory to /foo/bar (on localhost)
1 change: 1 addition & 0 deletions testdata/TestSequences/cwd/single_part.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OSC 7;file://localhost/foo: Set working directory to /foo (on localhost)

0 comments on commit ab50b2b

Please sign in to comment.