High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix(cli): give nav's stdout one owner, so a piped --snap or --text payload starts at its first line and the tab id moves to stderr (PIN-267)
Luigi Agostiea16e6a10153
diff --git a/cmd/pinchtab/cmd_cli_register.go b/cmd/pinchtab/cmd_cli_register.goindex e61810a3..bdb6f251 100644--- a/cmd/pinchtab/cmd_cli_register.go+++ b/cmd/pinchtab/cmd_cli_register.go@@ -275,7 +275,7 @@ func configureBrowserFlags() {)evalCmd.Flags().Bool("await-promise", false, "Resolve a returned Promise before responding")- navCmd.Flags().Bool("print-tab-id", false, "Print only the tab ID on stdout (also triggered automatically when stdout is a pipe)")+ navCmd.Flags().Bool("print-tab-id", false, "Print only the tab ID on stdout; with --snap or --text the payload owns stdout and the tab ID goes to stderr (also triggered automatically when stdout is a pipe)")for _, cmd := range []*cobra.Command{handoffCmd, tabHandoffCmd} {cmd.Flags().String("reason", "", "Reason for human handoff (default: manual_handoff)")cmd.Flags().Int("timeout-ms", 0, "Optional auto-resume timeout in milliseconds")diff --git a/docs/commands.md b/docs/commands.mdindex 095f1863..d8fe5f1c 100644--- a/docs/commands.md+++ b/docs/commands.md@@ -78,7 +78,7 @@ pinchtab nav <url> --block-images # Block images for this navigationpinchtab nav <url> --block-ads # Block ads for this navigationpinchtab nav <url> --snap # Navigate and output interactive snapshotpinchtab nav <url> --text # Navigate and output page text-pinchtab nav <url> --print-tab-id # Print only the tab ID, whatever stdout is+pinchtab nav <url> --print-tab-id # Print only the tab ID, whatever stdout is (with --snap/--text the tab ID goes to stderr)pinchtab back # Go back in the active tabpinchtab back --tab <id> # Go back in a specific tabpinchtab forward # Go forward in the active tabdiff --git a/internal/cli/actions/actions_navigate.go b/internal/cli/actions/actions_navigate.goindex f6add939..fca42d97 100644--- a/internal/cli/actions/actions_navigate.go+++ b/internal/cli/actions/actions_navigate.go@@ -99,15 +99,15 @@ func Navigate(client *http.Client, base, token string, url string, cmd *cobra.Coresult, usedFallback := postNavigate(client, base, token, req, false)resultTabID := tabIDFromNavigateResult(result)reportFallbackNewTab(cmd, usedFallback, req.tabID, resultTabID)+ stdout := navigateStdoutFor(cmd)if resultTabID != "" {- fmt.Println(resultTabID)+ if stdout.tabID {+ fmt.Println(resultTabID)+ } else {+ fmt.Fprintln(os.Stderr, resultTabID)+ }}-- // The landed URL is the cheap signal that the page is not the one asked for,- // but a second line would break `TAB=$(pinchtab nav URL)` — command- // substitution captures every line. So it is printed only for a human at a- // terminal, which is what --print-tab-id already promised.- if !tabIDOnly(cmd) {+ if stdout.landed {if landed := landedURL(result); landed != "" {output.Value(landed)}@@ -122,9 +122,23 @@ func Navigate(client *http.Client, base, token string, url string, cmd *cobra.Coreturn resultTabID}-// tabIDOnly reports whether stdout must carry nothing but the tab ID: either-// --print-tab-id was passed, or stdout is not a terminal, which is the shape a-// capture or a pipeline has.+type navigateStdout struct {+ tabID bool+ landed bool+}++func navigateStdoutFor(cmd *cobra.Command) navigateStdout {+ captured := tabIDOnly(cmd)+ return navigateStdout{tabID: !captured || !payloadRequested(cmd), landed: !captured}+}++func payloadRequested(cmd *cobra.Command) bool {+ snap, _ := cmd.Flags().GetBool("snap")+ snapDiff, _ := cmd.Flags().GetBool("snap-diff")+ text, _ := cmd.Flags().GetBool("text")+ return snap || snapDiff || text+}+func tabIDOnly(cmd *cobra.Command) bool {if only, _ := cmd.Flags().GetBool("print-tab-id"); only {return truediff --git a/internal/cli/actions/actions_navigate_test.go b/internal/cli/actions/actions_navigate_test.goindex 5a0f165d..8821a198 100644--- a/internal/cli/actions/actions_navigate_test.go+++ b/internal/cli/actions/actions_navigate_test.go@@ -22,6 +22,9 @@ func newNavigateCmd() *cobra.Command {cmd.Flags().Float64("timeout", 0, "")cmd.Flags().String("tab", "", "")cmd.Flags().Bool("print-tab-id", false, "")+ cmd.Flags().Bool("snap", false, "")+ cmd.Flags().Bool("snap-diff", false, "")+ cmd.Flags().Bool("text", false, "")return cmd}@@ -390,34 +393,54 @@ func TestNavigateReportsTheLandedURLAtATerminal(t *testing.T) {}}-// TAB=$(pinchtab nav URL) captures every line, so a second line would break it.-// Both the explicit flag and a non-terminal stdout must stay single-line.-func TestNavigatePrintsOnlyTheTabIDWhenCaptured(t *testing.T) {+const navigateSnapshotPayload = "# Example Domain | https://example.com/ | 1 nodes\ne1:link \"More information\""++func TestNavigateStdoutHasOneOwner(t *testing.T) {for _, tc := range []struct {- name string- terminal bool- flag bool+ name string+ terminal bool+ flag bool+ payload string+ wantStdout string+ tabIDOnStderr bool}{- {name: "stdout is not a character device", terminal: false},- {name: "print-tab-id at a terminal", terminal: true, flag: true},+ {name: "terminal", terminal: true, wantStdout: "ABC123\nhttps://example.com/\n"},+ {name: "terminal with snap", terminal: true, payload: "snap", wantStdout: "ABC123\nhttps://example.com/\n" + navigateSnapshotPayload + "\n"},+ {name: "pipe", wantStdout: "ABC123\n"},+ {name: "print-tab-id at a terminal", terminal: true, flag: true, wantStdout: "ABC123\n"},+ {name: "pipe with snap", payload: "snap", wantStdout: navigateSnapshotPayload + "\n", tabIDOnStderr: true},+ {name: "pipe with snap-diff", payload: "snap-diff", wantStdout: navigateSnapshotPayload + "\n", tabIDOnStderr: true},+ {name: "pipe with text", payload: "text", wantStdout: "Page text\n", tabIDOnStderr: true},+ {name: "print-tab-id at a terminal with snap", terminal: true, flag: true, payload: "snap", wantStdout: navigateSnapshotPayload + "\n", tabIDOnStderr: true},} {t.Run(tc.name, func(t *testing.T) {stdoutTerminal(t, tc.terminal)m := newMockServer()m.response = `{"tabId":"ABC123","url":"https://example.com/"}`+ m.responses["GET /snapshot"] = mockResponse{statusCode: 200, body: navigateSnapshotPayload}+ m.responses["GET /tabs/ABC123/text"] = mockResponse{statusCode: 200, body: `{"text":"Page text"}`}defer m.close()cmd := newNavigateCmd()if tc.flag {_ = cmd.Flags().Set("print-tab-id", "true")}- out := captureStdout(t, func() {- Navigate(m.server.Client(), m.base(), "", "https://example.com", cmd)+ if tc.payload != "" {+ _ = cmd.Flags().Set(tc.payload, "true")+ }+ var stdout string+ stderr := captureStderr(t, func() {+ stdout = captureStdout(t, func() {+ Navigate(m.server.Client(), m.base(), "", "https://example.com", cmd)+ })})- if got := strings.TrimSpace(out); got != "ABC123" {- t.Errorf("stdout = %q, want exactly the tab ID so $(pinchtab nav URL) stays usable", got)+ if stdout != tc.wantStdout {+ t.Errorf("stdout = %q, want %q", stdout, tc.wantStdout)+ }+ if got := strings.Contains(stderr, "ABC123\n"); got != tc.tabIDOnStderr {+ t.Errorf("tab id on stderr = %v, want %v (stderr %q)", got, tc.tabIDOnStderr, stderr)}})}@@ -563,9 +586,6 @@ func TestNavigateTextFetchesPageText(t *testing.T) {defer m.close()cmd := newNavigateCmd()- cmd.Flags().Bool("snap", false, "")- cmd.Flags().Bool("snap-diff", false, "")- cmd.Flags().Bool("text", false, "")_ = cmd.Flags().Set("text", "true")out := captureStdout(t, func() {