-
Notifications
You must be signed in to change notification settings - Fork 282
feat: copy share link for the playing track #458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
repparw
wants to merge
4
commits into
bjarneo:main
Choose a base branch
from
repparw:feat/share-track-link
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b1c2e1a
feat: copy share link for the playing track
repparw a901819
fix(share): address review findings on link fallback, clipboard timeo…
repparw 9b81926
fix(share): run clipboard copy off the TUI update path
repparw cf818e5
test(share): table-drive shareCopiedMsg outcomes
repparw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,105 @@ | ||
| // Package clipboard copies text to the system clipboard using whatever | ||
| // backend the platform provides. It is deliberately dependency-free: | ||
| // backends are resolved with exec.LookPath at call time so headless and | ||
| // minimal installs simply report an error instead of failing to build. | ||
| package clipboard | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "runtime" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| // copyTimeout bounds a single backend invocation. Backends normally exit | ||
| // at once; the deadline only fires when one stalls (locked Wayland socket, | ||
| // wedged X server), which must not freeze the TUI update path. | ||
| const copyTimeout = 5 * time.Second | ||
|
|
||
| // Copy writes text to the system clipboard. It returns an error when no | ||
| // supported backend is installed; callers should fall back to showing the | ||
| // text (footer message, stdout) in that case. | ||
| func Copy(text string) error { | ||
| candidates, err := backends() | ||
| if err != nil { | ||
| return fmt.Errorf("resolve clipboard backends: %w", err) | ||
| } | ||
| var lastErr error | ||
| for _, c := range candidates { | ||
| if _, err := exec.LookPath(c.name); err != nil { | ||
| continue | ||
| } | ||
| ctx, cancel := context.WithTimeout(context.Background(), copyTimeout) | ||
| cmd := exec.CommandContext(ctx, c.name, c.args...) | ||
| cmd.Stdin = strings.NewReader(text) | ||
| // Clipboard backends daemonize (wl-copy and xclip fork to | ||
| // keep serving the selection). Capturing their output would | ||
| // hold the pipes open until the daemon exits, so Run with | ||
| // discarded output instead of CombinedOutput. | ||
| cmd.Stdout = nil | ||
| cmd.Stderr = nil | ||
| runErr := cmd.Run() | ||
| cancel() | ||
| if ctx.Err() == context.DeadlineExceeded { | ||
| lastErr = fmt.Errorf("%s: timed out after %s", c.name, copyTimeout) | ||
| continue | ||
| } | ||
| if runErr != nil { | ||
| lastErr = fmt.Errorf("%s: %w", c.name, runErr) | ||
| continue | ||
| } | ||
| return nil | ||
| } | ||
| if lastErr != nil { | ||
| return lastErr | ||
| } | ||
| return fmt.Errorf("no clipboard backend found (%s)", backendNames(candidates)) | ||
| } | ||
|
|
||
| type backend struct { | ||
| name string | ||
| args []string | ||
| } | ||
|
|
||
| func backends() ([]backend, error) { | ||
| switch runtime.GOOS { | ||
| case "darwin": | ||
| return []backend{{name: "pbcopy"}}, nil | ||
| case "windows": | ||
| return []backend{{name: "clip"}}, nil | ||
| default: | ||
| // Wayland first when a Wayland session is present, then X11. | ||
| // WSLg sets WAYLAND_DISPLAY; plain WSL falls through to the error, | ||
| // where the caller prints the text instead. | ||
| if isWayland() { | ||
| return []backend{ | ||
| {name: "wl-copy"}, | ||
| {name: "xclip", args: []string{"-selection", "clipboard"}}, | ||
| {name: "xsel", args: []string{"--clipboard", "--input"}}, | ||
| }, nil | ||
| } | ||
| return []backend{ | ||
| {name: "xclip", args: []string{"-selection", "clipboard"}}, | ||
| {name: "xsel", args: []string{"--clipboard", "--input"}}, | ||
| {name: "wl-copy"}, | ||
| }, nil | ||
| } | ||
| } | ||
|
|
||
| func isWayland() bool { | ||
| if os.Getenv("WAYLAND_DISPLAY") != "" { | ||
| return true | ||
| } | ||
| return strings.EqualFold(os.Getenv("XDG_SESSION_TYPE"), "wayland") | ||
| } | ||
|
|
||
| func backendNames(candidates []backend) string { | ||
| names := make([]string, 0, len(candidates)) | ||
| for _, c := range candidates { | ||
| names = append(names, c.name) | ||
| } | ||
| return strings.Join(names, ", ") | ||
| } |
This file contains hidden or 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,15 @@ | ||
| package clipboard | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestCopyNoBackend(t *testing.T) { | ||
| oldPath := os.Getenv("PATH") | ||
| os.Setenv("PATH", "/nonexistent-dir-for-test") | ||
| defer os.Setenv("PATH", oldPath) | ||
| if err := Copy("x"); err == nil { | ||
| t.Fatal("expected error with no backends on PATH") | ||
| } | ||
| } |
This file contains hidden or 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,42 @@ | ||
| package playlist | ||
|
|
||
| import ( | ||
| "net/url" | ||
| "strings" | ||
| ) | ||
|
|
||
| // ShareLink derives a shareable https URL for a track path. | ||
| // | ||
| // - Spotify URIs (spotify:<type>:<id>) map to the corresponding | ||
| // open.spotify.com page, e.g. spotify:track:<id> becomes | ||
| // https://open.spotify.com/track/<id>. | ||
| // - Plain http(s) URLs are returned unchanged, since they are already | ||
| // links (radio streams, direct files, video pages). | ||
| // | ||
| // Anything else (local files, yt-dlp search expressions and other | ||
| // pseudo-protocols, provider URIs without a public page) has no shareable | ||
| // form and reports false. | ||
| func ShareLink(path string) (string, bool) { | ||
| if path == "" { | ||
| return "", false | ||
| } | ||
| if rest, ok := strings.CutPrefix(path, "spotify:"); ok { | ||
| typ, id, ok := strings.Cut(rest, ":") | ||
| if !ok || typ == "" || id == "" || strings.ContainsAny(id, ":/?#") { | ||
| return "", false | ||
| } | ||
| switch typ { | ||
| case "track", "episode", "album", "playlist", "artist", "show": | ||
| return "https://open.spotify.com/" + typ + "/" + url.PathEscape(id), true | ||
| default: | ||
| return "", false | ||
| } | ||
| } | ||
| if IsYTSearch(path) { | ||
| return "", false | ||
| } | ||
| if IsURL(path) { | ||
| return path, true | ||
| } | ||
| return "", false | ||
| } |
This file contains hidden or 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,87 @@ | ||
| package playlist | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestShareLink(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| path string | ||
| want string | ||
| share bool | ||
| }{ | ||
| { | ||
| name: "spotify track", | ||
| path: "spotify:track:5FFTCVlkmd78TIw9mTfDUP", | ||
| want: "https://open.spotify.com/track/5FFTCVlkmd78TIw9mTfDUP", | ||
| share: true, | ||
| }, | ||
| { | ||
| name: "spotify episode", | ||
| path: "spotify:episode:4rOoJ6Egrf8K2IrywzwOMk", | ||
| want: "https://open.spotify.com/episode/4rOoJ6Egrf8K2IrywzwOMk", | ||
| share: true, | ||
| }, | ||
| { | ||
| name: "spotify album", | ||
| path: "spotify:album:6akEvsycLGftJxsoqdWqaw", | ||
| want: "https://open.spotify.com/album/6akEvsycLGftJxsoqdWqaw", | ||
| share: true, | ||
| }, | ||
| { | ||
| name: "spotify truncated id", | ||
| path: "spotify:track:", | ||
| want: "", | ||
| share: false, | ||
| }, | ||
| { | ||
| name: "spotify unknown type", | ||
| path: "spotify:user:repparw", | ||
| want: "", | ||
| share: false, | ||
| }, | ||
| { | ||
| name: "spotify id with reserved chars rejected", | ||
| path: "spotify:track:abc/def?x#y", | ||
| want: "", | ||
| share: false, | ||
| }, | ||
| { | ||
| name: "https stream", | ||
| path: "http://radio.cliamp.stream/lofi/stream", | ||
| want: "http://radio.cliamp.stream/lofi/stream", | ||
| share: true, | ||
| }, | ||
| { | ||
| name: "youtube page", | ||
| path: "https://www.youtube.com/watch?v=dQw4w9WgXcQ", | ||
| want: "https://www.youtube.com/watch?v=dQw4w9WgXcQ", | ||
| share: true, | ||
| }, | ||
| { | ||
| name: "yt-dlp search is not a link", | ||
| path: "ytsearch:never gonna give you up", | ||
| want: "", | ||
| share: false, | ||
| }, | ||
| { | ||
| name: "local file", | ||
| path: "/home/me/Music/song.flac", | ||
| want: "", | ||
| share: false, | ||
| }, | ||
| { | ||
| name: "empty", | ||
| path: "", | ||
| want: "", | ||
| share: false, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, ok := ShareLink(tt.path) | ||
| if ok != tt.share || got != tt.want { | ||
| t.Fatalf("ShareLink(%q) = (%q, %v), want (%q, %v)", tt.path, got, ok, tt.want, tt.share) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.