Qix- / better-exceptions

Pretty and useful exceptions in Python, automatically.
MIT License
4.58k stars 203 forks source link

terminfo (tmux-256color, xterm-256color) not considered to be valid #117

Open blueyed opened 2 years ago

blueyed commented 2 years ago

Using /usr/lib/terminfo/t/tmux-256color from ncurses 6.2-2 (on Arch Linux) color support (SUPPORTS_COLOR) is not detected properly.

[38] > …/Vcs/better-exceptions/better_exceptions/color.py(106)<module>()->None

...
 99             if is_tty:
100                 f = get_terminfo_file()
101                 if f is not None:
102                     with f:
103                         # f is a valid terminfo; seek and read!
104                         magic_number = struct.unpack('<h', f.read(2))[0]
105
106  ->                     if magic_number == 0x11A:
107                             # the opened terminfo file is valid.
108                             offset = 2 + 10  # magic number + size section (the next thing we read fr
109                             offset += struct.unpack('<h', f.read(2))[0]  # skip over names section
110                             offset += struct.unpack('<h', f.read(2))[0]  # skip over bool section
111                             offset += offset % 2  # align to short boundary
112                             offset += 13 * 2  # maxColors is the 13th numeric value
113
114                             f.seek(offset)
115                             max_colors = struct.unpack('<h', f.read(2))[0]
116
117                             if max_colors >= 8:
118                                 SUPPORTS_COLOR = True
 return None
(Pdb++) f
<_io.BufferedReader name='/usr/lib/terminfo/t/tmux-256color'>
(Pdb++) magic_number
542
(Pdb++) 0x11A
282

This also happens with /usr/lib/terminfo/x/xterm-256color (magic_number also being 542).

Using better-exceptions 0.3.3-1-g70e282d.

Qix- commented 2 years ago

Hmm. Very strange. I wonder if terminfo updated its file format.

@eric-s-raymond Hey there, sorry to bug you (not sure if you'll see this at all anyway). Do you know why the magic number might be different between versions of terminfo? Did the file format change at all within the last few years?

@blueyed Would you be willing to attach the terminfo file on this issue so I can take a look? It shouldn't contain any identifying/private information. I don't have an Arch machine onhand to pull the latest ncurses and try to repro (and I'm pretty strapped for time the next few weeks), so an upload would be very helpful.

Thanks for the detailed report!

blueyed commented 2 years ago

@Qix- https://archlinux.org/packages/core/x86_64/ncurses/download (via https://archlinux.org/packages/core/x86_64/ncurses/) I have not checked, but it is likely the same as with the upstream/original files. But I have no idea about this magic number in general.

bremme commented 2 years ago

I can confirm this. I'm running Ubuntu 20.04 with the default gnome-terminal application. My TERM environmental variable is set to xterm-256color and I get no colored output.

If I force colors by setting: export FORCE_COLOR=1 I get colored output.

Let me know if I can do anything else to help resolve this issue?

bremme commented 2 years ago

For completeness, better-exceptions finds the following values for my system:

bremme commented 2 years ago

@Qix- I zipped my xterm-256color terminfo file since Github does not allow uploading the plain binairy file. Hope this helps.

xterm-256color.zip

agvxov commented 2 months ago

bumping out of interest, as it affects me too.