Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How to Get XTerm Title

by NateTut (Deacon)
on Sep 02, 2009 at 16:14 UTC ( #792957=perlquestion: print w/ replies, xml ) Need Help??
NateTut has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl script that sets the title of the XTerm emulator we are using (Putty). This works just fine. However I would like to be polite and capture the original title and restore it when my script is finished.

I can find innumerable references on how to set the title of an XTerm window, but nothing on how to get the title. Does anyone have some insight?

Update:I have discovered the escape sequence ^[[21t; outputs the title but it doesn't go to either STDOUT or STDERR! How can I capture it then?

Comment on How to Get XTerm Title
Download Code
Re: How to Get XTerm Title
by parv (Priest) on Sep 02, 2009 at 16:42 UTC
    I don't know offhand right now, but vim resets the title after a session is done (i.e., should look somewhere in vim source code).
Re: How to Get XTerm Title
by lostjimmy (Chaplain) on Sep 02, 2009 at 16:44 UTC
    I found this posting. It's not really a perl solution, but it should work out of the box on any distribution.
      Thanks, that was very close, but xprop is looking for an x-window and we are not running x-windows, just dumb terminals that just happen to do XTerm emulation.
Re: How to Get XTerm Title
by afoken (Parson) on Sep 03, 2009 at 20:18 UTC
    Update:I have discovered the escape sequence ^[[21t; outputs the title but it doesn't go to either STDOUT or STDERR! How can I capture it then?

    Think a second about how your program talks to the terminal (emulator) and the user behind it. (Think about a three-wire RS232 connection -- TxD, RxD, GND -- to an old VT100 terminal, if that helps you.) STDOUT and STDERR go into the terminal (TxD wire on the computer), where the text written is postprocessed and usually displayed on screen. STDIN comes from the terminal (RxD wire), usually only when the user starts hacking on the keyboard.

    All terminal emulators behave the same, in this respect: There is one (virtual) wire into the terminal, and another (virtual) wire back to the computer. The Linux virtual consoles (also known as text mode) behave like pimped VT100s, and so do all other virtual consoles. xterm and its clones do the same, but instead of writing characters into the text mode graphics card, they ask the X server to draw some pixels into the frame buffer. PuTTY does the same for Windows.

    The big difference between xterm and a classic VT100 is that the xterm terminal emulator knows some new tricks that involve the mouse, so you can use the mouse in joe and mc, for example. PuTTY pretends to be an xterm clone because, well, it is an xterm clone. It is a terminal emulator with mouse functions running in graphics mode, no matter how you communicate with the server (telnet, ssh, rlogin, serial port).

    Now, what is the only way for a terminal to send information back to the computer? STDIN, of course! You write a special escape sequence to STDOUT and the terminal will answer via STDIN, just in between the data entered by the user. The terminal software usually takes care that its own answer is send in one piece, uninterrupted by keyboard data.

    Now, why do you see the terminal's answer on the terminal screen? Because usually, the operating system echos all terminal input back to the terminal output.

    See also the PuTTY FAQ A.7.12

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      That escape sequence is part of the XTerm control set. When I did echo ^[[21t; > STDOUT.txt 2> STDERR.txt I get nothing in either file, yet I can see the window title on the screen. Putty is returning it, but I can't seem to capture it.

      Update: the first character of that control string is an ESC and I entered it by doing a Cntrl-v first then the ESC key.

      Update: Corrected a typo
        When I did echo ^[[21t; > STDOUT.txt 2> STDERR.txt I get nothing in either file, yet I can see the window title on the screen. Putty is returning it, but I can't seem to capture it.

        It seems you did not read or at least did not understand my posting.

        PuTTY and any other terminal emulator returns answers to status queries on the same channel where it returns user input: STDIN. STandarD INput, not STDOUT, not STDERR. You see the answer on screen because the terminal handling code of your operating system works in echo mode and writes back everything coming in from the terminal back to the terminal. This happens in the kernel, in a layer below the file handle layer.

        Stupid example that lacks disabling echo and a reasonable reading routine:

        #!/usr/bin/perl -w use strict; $|=1; print "Press <Enter> now, as this is a very stupid demo!\n", "Note that echo is not turned off, so you will see the answer +here,\n", "even before perl has read it: ", "\x05"; # Ctrl-E -> tell Putty to identify itself my $id=<STDIN>; chomp $id; print "OK, your terminal answered '$id' to Ctrl-E.\n";

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: How to Get XTerm Title
by Anonymous Monk on Jul 15, 2014 at 15:36 UTC
    Really? afoken's second response is harsh to say the least. I have 20 years background in IT and though I understand what was written in the first response, it is in no way an answer to the question. Where's the solution? Where's the code? Nowhere it seems. Just some text that is bordering on an arrogant response I'm sorry to have to say.
      Good luck trolling a 5 year old thread.

      Really? afoken's second response is harsh to say the least. I have 20 years background in IT and though I understand what was written in the first response, it is in no way an answer to the question. Where's the solution? Where's the code? Nowhere it seems. Just some text that is bordering on an arrogant response I'm sorry to have to say.

      And still somehow NateTut thanked afoken for the help in solving this issue

      Maybe you should take a minute to reexamine what is written to discover the answer ... that some escape sequence outputs the title on STDIN

Re: How to Get XTerm Title
by Anonymous Monk on Jul 15, 2014 at 16:21 UTC
    I was hoping afoken would some day return. You never know...

      I was hoping afoken would some day return. You never know...

      Hmm, both NateTut and afoken were last here one day ago

      If you didn't understand something, its ok to ask for help nicely :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://792957]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-07-31 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (245 votes), past polls