JakeWharton / pidcat

Colored logcat script which only shows log entries for a specific application package.
Apache License 2.0
4.84k stars 500 forks source link

On Python3, color codes are printed instead of colors #182

Open bobpaul opened 2 years ago

bobpaul commented 2 years ago

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)
davidmontemayor commented 1 year ago

Yup, ran into this as well. I guess not a lot of people use this script anymore? Thanks for pointing out the fix!

cxzero commented 8 months ago

Got the same error, thanks for sharing the workaround and the fix, hopefully gets merged