cursor icon indicating copy to clipboard operation
cursor copied to clipboard

ANSI escape codes should only be written if writer is a TTY

Open LennartC opened this issue 2 years ago • 3 comments

ANSI escape codes should only be written when the writer is a terminal.

This is important when the output is redirect to a file for example.

This would solve https://github.com/atomicgo/cursor/issues/24

LennartC avatar Nov 16 '23 16:11 LennartC

Codecov Report

Attention: 17 lines in your changes are missing coverage. Please review.

Comparison is base (3d3b320) 4.86% compared to head (1372939) 3.12%.

Files Patch % Lines
cursor_other.go 5.55% 15 Missing and 2 partials :warning:
Additional details and impacted files
@@           Coverage Diff            @@
##            main     #23      +/-   ##
========================================
- Coverage   4.86%   3.12%   -1.74%     
========================================
  Files          6       6              
  Lines        185     192       +7     
========================================
- Hits           9       6       -3     
- Misses       176     184       +8     
- Partials       0       2       +2     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Nov 16 '23 22:11 codecov[bot]

Hi, thanks for the PR! We should still have a way to force ANSI codes, even if it is a file. Sometimes the user might want to record the ANSI codes to debug a problem, or to validate tests. We should either have something like var Force bool or we could let the user handle the logic by providing a method to turn off ANSI output.

MarvinJWendt avatar Nov 16 '23 22:11 MarvinJWendt

Not entirely sure how to fix this then. If you want to let the user handle it, I see the following options:

  1. make the cursor's writer public, so the user can implement an IsTerminal himself and decide whether to call Hide, Show, ...
  2. keep the new IsTerminal method in cursor, and make it public.
  3. not care at all in this module, and then if the user wants to handle it, he'll have to explicitly set the writer of the cursor.
  4. Or, as you suggested, add a Force flag.

Let me know what you prefer and I'll update the PR ;-)

LennartC avatar Nov 17 '23 07:11 LennartC