-
Notifications
You must be signed in to change notification settings - Fork 7.3k
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
Need a way to detect output goes to a file instead of the console #3452
Comments
Do you really mean a file or just redirected (not a tty/console). IIRC, there is logic in the native command processor that computes whether the output is redirected or not. |
I think it depends, If The bottom line here is that PowerShell should make it easy (if it isn't already) to support these scenarios in custom formatting, or alternatively strip (some limited) ANSI escape sequences automatically. |
I've been struggling with this for a little while - I rewrote my At first I tried calling When calling native code though, it does replace the handles as you'd expect, so I wrote a small C++ program, which seems to be the only way to tell if a stream is redirected or it's part of a pipeline. Since calling a native program does what you'd expect, I can't imagine it'd be too hard to add something like |
I actually had an issue with Richard Turner of Windows Terminal team about this. The general idea is request-based mediated execution. I made the point that tools like troff are legacy and pre-date REST approach to API development where a client and server handshake on the medium the messages use. |
For those coming here looking for a workaround, I found this Stack Overflow answer helpful: https://superuser.com/a/1706459/560725 $FormatEnumerationLimit=-1
Your Command | Format-Table | Out-File -width 999999 -Encoding utf8 "C:\Temp\result.txt" e.g., you can even simplify this in some cases: Get-Content "SomeLogFile.txt" | Select-String "LogPattern" | Out-File -width 999999 -Encoding utf8 "C:\Temp\result.txt" As someone who doesn't use PowerShell on a daily basis, I really dislike this behavior as compared to how I would use grep and redirection via The general idea here is that this example is a play on the same theme of request-based mediated execution. The format applied should depend on the context.
|
See also: dotnet/msbuild#4299 (comment) |
Still desired. |
Your solution does not solve the format length issue. It does solve the problem as initially motivated, but not all use cases for knowing the output target is a file. |
I'm not sure i entirely understand the problem, do you have an example? Out-File, Out-String and redirected
this works. example 'A' * 100kb + 'b' | select-string b > foo.tmp ^ does not get truncated.. |
I already gave examples. |
This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically. Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original. |
If a custom format wants to use ANSI escape sequences, it should be possible to skip the escape sequences when the target is a file instead of a tty.
For example, my custom format for
MatchInfo
(the output ofSelect-String
) uses ANSI escape sequences, but if I redirect output, e.g.dir *.txt | Select-String foo > out.txt
, I probably don't want the escape sequences.We should have a simple way (api, variable, whatever) that makes it easy to control this behavior.
The text was updated successfully, but these errors were encountered: