Почему дисплей с 256 цветами не работает с консолью Linux, а с экраном?

1138
philipp

Я работаю над программой на Python с текстовым графическим интерфейсом на основе urwid. urwid - это библиотека для создания программ в стиле ncurses с python.

Операционная система - CentOS 7.

Все нормально работает в «нормальных» условиях (X-Server, окно терминала). Однако на консоли Linux без X-сервера urwid переключается в режим низкого цвета. (С неправильными цветовыми кодами на некоторых машинах я получаю раздражающе мигающий текст, чем)

Забавно, что при использовании консоли linux мне просто нужно запустить screen в качестве обходного пути. Внутри экрана все снова хорошо. Без какой-либо специальной конфигурации для экрана.

Я уже пытался сравнить много информации об окружающей среде между текстовой и экранной оболочками, но это не помогло. Например, локаль идентична, pythons sys.stdout.encoding одинакова, выполнение «: runtime syntax / colortest.vim» в vim выглядит правильно и красочно в обоих случаях.

1

1 ответ на вопрос

2
grawity

This is normal. It happens simply because the Linux kernel console does not support the 256-color mode. It wasn't written to use 256 colors, as in VGA text mode there's simply no way to do that. (In framebuffer mode it could, but the code still has the exact same constraints.) GNU Screen knows this and automatically translates from the 256-color palette to the nearest 16-color one.

Only very recent Linux versions (3.13 and later) started recognizing the 256-color escape codes, but even then it still maps them to the 16-color palette just like Screen would.

There are framebuffer-based terminal emulators like kmscon or fbterm which implement their own rendering, and draw everything via KMS. Use those if you want a capable terminal avoiding X11.


Blinking text happens because the 256-color codes are very easily confused the 16-color ones. For example, ESC [38;5;35m can be interpreted as the 256-color code, or three ANSI (16-color) codes.

  • The Linux console doesn't know what 38 means, so it just interprets 5 as "enable blink" and 35 as "foreground – color #5 (magenta)" in the usual way.
    (See for example this table, under "SGR".)

  • Meanwhile, your X11 terminals recognize the 38 as the magic "foreground – extra colors" code, so they interpret 5 as "use 256-color palette" and 35 as "use color #35 (cyan)".
    (There's a 24-bit RGB mode too, as ESC [38;2;<r>;<g>;<b>m.)

Похожие вопросы