Ubuntu – How to detect current gnome-terminal character encoding

command lineencodinggconfgnome-terminal

I am trying to detect the current gnome-terminal character encoding from the command line. I have tried using gconftool :

$ gconftool-2 --get /apps/gnome-terminal/profiles/Default/encoding
current
$ gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/encoding
en_US.UTF-8
$ gconftool-2 --get /apps/gnome-terminal/profiles/Default/encoding
en_US.UTF-8

But if I now go to the gnome-terminal menu and choose Terminal->Set Character Encoding->ISO-8859-10 and then run again

$ gconftool-2 --get /apps/gnome-terminal/profiles/Default/encoding
en_US.UTF-8

so the encoding in /apps/gnome-terminal/profiles/Default/encoding has not changed to ISO-8859-10 even though the terminal is using that encoding. So it seems gconftool cannot be used to determine the current encoding.

Best Answer

I recommend checking locale charmap's output (which reports the values set by $LANG, $LC_CTYPE, $LC_ALL). This one does not directly query the terminal, but it's essential for most of the applications that the locale is set up correctly, consistently with the terminal. If it reports anything else than the actual behavior, not only your app but pretty much every other app will misbehave in the terminal, and it's not your fault. Of course the user can change the encoding from the menu, but if they decide to shoot themselves in the foot, there's not much you can/should do against. It's not your task to detect a faulty system-wide setting. Also there's no way for you to notice if the user switches encoding while your app is running, so I don't think there's much point in verifying it at startup.

If you really really need to check the runtime behavior, you can emit escape sequences that query the cursor position (and report it back in a certain format as if it's been typed from the keyboard), then emit some bytes that e.g. form a single character in UTF-8 while multiple characters in any other encoding (disabling local echo in the mean time, so keypresses by the user don't advance the cursor), and query the cursor position again. It's perhaps too much trouble and really not worth the effort.

Related Question