Ubuntu: How to detect current gnome-terminal character encoding?



Question:

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.


Solution:1

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.


Solution:2

Here is a half-solution, addressing the problem of determining the encoding when /apps/gnome-terminal/profiles/Default/encoding is equal to the string current. Assuming that the string current means that gnome-terminal should use the current locale.

It is tempting to check the LANG variable to determine the encoding, but according to this question, that is not reliable. Instead the Perl module I18N::Langinfo should be used:

temp=$(gconftool-2 --get /apps/gnome-terminal/profiles/Default/encoding)    if [[ $temp == "current" ]] ; then      perl -MI18N::Langinfo=langinfo,CODESET -E 'say langinfo(CODESET())'  else      echo $temp  fi  

Note that this answer does not address the problem of determining the gnome-terminal encoding when the user manually changes the encoding from the gnome-terminal menu.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »