-
Notifications
You must be signed in to change notification settings - Fork 260
Fix hyperlink ANSI escape codes appearing in non-terminal output #6015
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
64dcda7
Initial plan
Copilot 451f294
Fix hyperlink functions to detect terminal state and return plain URL…
Copilot 921c17e
Use input.IsTerminal logic for hyperlink terminal detection and rever…
Copilot 595b9fb
Refactor: Move IsTerminal to internal/terminal package to eliminate c…
Copilot 4aedaf6
Simplify hyperlink output in non-terminal mode and remove input.IsTer…
Copilot 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ words: | |
| - protoimpl | ||
| - Retryable | ||
| - runcontext | ||
| - surveyterm | ||
| - unmarshals | ||
| - unmarshaling | ||
| - unsetting | ||
|
|
||
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,30 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package terminal | ||
|
|
||
| import ( | ||
| "os" | ||
| "strconv" | ||
|
|
||
| "github.com/azure/azure-dev/cli/azd/internal/tracing/resource" | ||
| "github.com/mattn/go-isatty" | ||
| ) | ||
|
|
||
| // IsTerminal returns true if the given file descriptors are attached to a terminal, | ||
| // taking into account of environment variables that force TTY behavior. | ||
| func IsTerminal(stdoutFd uintptr, stdinFd uintptr) bool { | ||
| // User override to force TTY behavior | ||
| if forceTty, err := strconv.ParseBool(os.Getenv("AZD_FORCE_TTY")); err == nil { | ||
| return forceTty | ||
| } | ||
|
|
||
| // By default, detect if we are running on CI and force no TTY mode if we are. | ||
| // If this is affecting you locally while debugging on a CI machine, | ||
| // use the override AZD_FORCE_TTY=true. | ||
| if resource.IsRunningOnCI() { | ||
| return false | ||
| } | ||
|
|
||
| return isatty.IsTerminal(stdoutFd) && isatty.IsTerminal(stdinFd) | ||
| } |
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,65 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package output | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestWithHyperlink(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| url string | ||
| text string | ||
| expectEscape bool | ||
| expectedPlain string | ||
| }{ | ||
| { | ||
| name: "URL and text are the same", | ||
| url: "https://example.com", | ||
| text: "https://example.com", | ||
| expectEscape: true, | ||
| expectedPlain: "https://example.com", | ||
| }, | ||
| { | ||
| name: "URL and text are different", | ||
| url: "https://example.com", | ||
| text: "Example Site", | ||
| expectEscape: true, | ||
| expectedPlain: "https://example.com", | ||
| }, | ||
| { | ||
| name: "Text is empty", | ||
| url: "https://example.com", | ||
| text: "", | ||
| expectEscape: true, | ||
| expectedPlain: "https://example.com", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Test non-terminal mode by checking the actual output | ||
| // Since we can't reliably set os.Stdout.Fd() to non-terminal in tests, | ||
| // we'll just verify the function doesn't panic and returns a string | ||
| result := WithHyperlink(tt.url, tt.text) | ||
| require.NotEmpty(t, result) | ||
|
|
||
| // When running in a non-TTY environment (like most CI systems), | ||
| // the result should be the plain text version | ||
| if !isTTY() { | ||
| require.Equal(t, tt.expectedPlain, result) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // isTTY checks if stdout is a TTY (for testing purposes) | ||
| func isTTY() bool { | ||
| fileInfo, _ := os.Stdout.Stat() | ||
| return (fileInfo.Mode() & os.ModeCharDevice) != 0 | ||
| } |
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.