JakeWharton / JakeWharton/pidcat

On Python3, color codes are printed instead of colors

Open
#182 2 comments 24 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
5k
Forks
517
PR merge metrics
No merged PRs in 30d

Description

I'm running on Bash 5.1.16 and have the `TERM` variables set appropriately:
```
$ env | grep TERM
COLORTERM=truecolor
TERM=xterm-256color
```

If I run pidcat with python3, I get output like this:
```
b'\x1b[37m ActivityManager\x1b[0m \x1b[30;102m I \x1b[0m Process com.samsung.android.app.galaxyfinder:appservice (pid 18912) has died: fg SVC (127,1420)'
b'\x1b[33matibilityChangeReporter\x1b[0m \x1b[30;104m D \x1b[0m Compat change id reported: 135634846; UID 10300; state: DISABLED'
b' \x1b[30;104m D \x1b[0m Compat change id reported: 143937733; UID 10300; state: ENABLED'
```
but if I run with python2, then I get colorized output as expected.

If I delete the `.encode(utf-8)` from the final print statement, then it works as expected on both Python2 and Python3, but I suspect this might cause problems on some systems if locale isn't `utf-8`.

```
$ git revert -n 6b6034ab67
Auto-merging pidcat.py
$ git reset
Unstaged changes after reset:
M pidcat.py
$ git diff
diff --git a/pidcat.py b/pidcat.py
index 6a23786..4a2b42f 100755
--- a/pidcat.py
+++ b/pidcat.py
@@ -359,4 +359,4 @@ while adb.poll() is None:
message = matcher.sub(replace, message)

linebuf += indent_wrap(message)
- print(linebuf.encode('utf-8'))
+ print(linebuf)
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by running pidcat with Python 3 under the reported TERM settings, then inspect the final print statement in pidcat.py around the shown diff. Done means Python 3 renders ANSI color output instead of a bytes representation while preserving the expected Python 2 behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.