Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
KDE's Konsole automatic tab renaming
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
penguinbrat
n00b
n00b


Joined: 06 Jan 2005
Posts: 19

PostPosted: Thu Jan 06, 2005 7:31 am    Post subject: KDE's Konsole automatic tab renaming Reply with quote

One of the things that really annoyed me about using konsole, was that the names of the tabs were simply the particular shell name that I was using (IE: shell) - gnome-terminal spoiled me in that the fancy PS1 prompt could modfiy it - unfortunately konsole couldn't do that - until I started playing around with dcop.... Here is what I have in my /etc/profile to automatically change the tab name to the particular directory that I'm in... I had to write a simple little python app to get the "~" in replace of /home/username, and one to rename the session to the particular file I'm editing in vi - they are below the script...

WARNING!!!

I've locked my self out of the system a few times because of the way I had things working in the /etc/profile - I think I've got all the bugs worked out (I haven't locked my self out again), but use at your own risk ;-)

--addition to /etc/profile---
Code:
DCOPUSER=`ps auwxf | grep "kdeinit Running" |grep -v grep | cut -d ' ' -f 1`

if [ `whoami` = 'root' ]
  then
    export KONSOLE_APPEND="--user $DCOPUSER"
    export KONSOLE_PREPEND="ROOT*"
  fi

function KonsolePrompt() {

  for X in `dcop $KONSOLE_APPEND konsole*` none
    do
      ACTIVE=`dcop $KONSOLE_APPEND $X konsole-mainwindow#1 isActiveWindow 2> /dev/null`

      if [ -n "`echo $ACTIVE | grep true`" ]
        then
          export KONSOLE_SESSION="`dcop $KONSOLE_APPEND $X konsole currentSession 2> /dev/null`"
          export KONSOLE_APP=$X
          case `whoami` in
            root) PROMPT_COMMAND='dcop $KONSOLE_APPEND $KONSOLE_APP $KONSOLE_SESSION renameSession "${KONSOLE_PREPEND}${PWD}"';;
            *)  PROMPT_COMMAND='dcop $KONSOLE_APPEND $KONSOLE_APP $KONSOLE_SESSION renameSession "`/usr/local/bin/pwd.py`"';;
          esac
          alias vi='/usr/local/bin/startVim.sh'
        fi
  done
}

KONSOLE_APP=""

while [ -z "$KONSOLE_APP" ]
  do
    [ -z "$DCOPUSER" ] && break
    KonsolePrompt
  done

--clip here--

---/usr/local/bin/pwd.py---
Code:
#!/usr/bin/python
import os


cwd=os.environ['PWD']
home=os.environ['HOME']

if cwd.count(home):
        print cwd.replace(os.environ['HOME'],"~")
else:
        print cwd

---clip here---

---/usr/local/bin/startVim.sh---
Code:
#!/bin/sh

dcop $KONSOLE_APPEND $KONSOLE_APP $KONSOLE_SESSION renameSession "${KONSOLE_PREPEND}Vim $1"

/usr/bin/vim $1

---clip here---

EDIT: Added code tags. Also, users should be aware that there are a couple of lines wrapped that shouldn't be. --pjp
Back to top
View user's profile Send private message
theDreamer
Tux's lil' helper
Tux's lil' helper


Joined: 20 Oct 2003
Posts: 118

PostPosted: Sat Jan 29, 2005 3:16 pm    Post subject: nice.. Reply with quote

nice, I was just looking for a way to do so..

few days ago i accidently changed it by modifying system variable COMMAND_PROMPT or PROMPT_COMMAND or something like it..

i tried to change the topic but accidently changed the tab name, wonder if it's a bug :)

Suggestion: Clean to code to require only a stand-alone file, it's much easier to understand and use.
_________________
Cheers,
Nir Dremer
www.dremer.org
Back to top
View user's profile Send private message
snakattak3
Guru
Guru


Joined: 11 Dec 2002
Posts: 468
Location: Seattle

PostPosted: Sat Jan 29, 2005 4:24 pm    Post subject: Reply with quote

You can also just put this in your bashrc.
Code:

#konsole Wrap
if [ "$KONSOLE_DCOP_SESSION" ]
then
    alias ssh='konsolewrap -s ssh'
    alias lftp='konsolewrap -s lftp'
    alias sftp='konsolewrap -s sftp'
    alias su='konsolewrap su'
    PROMPT_COMMAND='dcop $KONSOLE_DCOP_SESSION renameSession "`echo $PWD | sed s,^$HOME,~,`"'
fi

and put this in a file called "konsolewrap" somewhere in your path (mine is ~/bin)
Code:

#!/bin/sh
#This script is GPL, see http://www.gnu.org
if [ $1 == "-s" ]
then
    shift 1
    cmd=$1
    shift 1
else
    cmd=""
fi
dcop $KONSOLE_DCOP_SESSION renameSession "$*"
exec $cmd "$@"

That way you don't have to mess with your system settings. I can't find the original author of this, I think I got it off of kde-look.org. This works with kde-3.4 for me, and all previous versions.

[EDIT]
Found the author - here
[/EDIT]
_________________
Ban Reality TV!
Adopt an Unanswered Post
Back to top
View user's profile Send private message
Headrush
Watchman
Watchman


Joined: 06 Nov 2003
Posts: 5597
Location: Bizarro World

PostPosted: Sat Jan 29, 2005 4:52 pm    Post subject: Reply with quote

I'm using kde 3.4_beta1 and that functionality seems to be builtin.

Tab name reflects pwd of that tab.
Back to top
View user's profile Send private message
theDreamer
Tux's lil' helper
Tux's lil' helper


Joined: 20 Oct 2003
Posts: 118

PostPosted: Sat Jan 29, 2005 5:49 pm    Post subject: my solution Reply with quote

i wrote a simple bash script.
command line options are:
koncop new {session name} or list to see sessions list.
koncop rename {name} to change current session name.

Code:

#!/bin/sh

command_line()
{
   echo "koncop utility, command line options:"
   echo "new {list} / {session name}"
   echo "rename {new name}"
   exit 0
}

#checking for the existance of DCOP.
if [ ! "$KONSOLE_DCOP_SESSION" ]; then
   echo " dcop is disabled!"
   echo " (konsole not executed with --script or executed in remote host)"
   echo " aborting."
   exit 0
fi

if [ "$1" == "new" ] ; then
   if [ ! "$2" == "" ] ; then
      if [ "$2" == "list" ] ; then
         echo "These are the konsole type can be created:"
         echo `find $HOME/.kde/share/apps/konsole/ -name "*.desktop" -printf "\"%f\" "`
      else
         #passing $2-$4 for session one with spaces.
         dcop $KONSOLE_DCOP newSession $2 $3 $4
      fi
   else
      command_line
   fi
else if [ "$1" == "rename" ] ; then
   #check for valid command line parameter.
   if [ ! -z $2 ] ; then
      dcop $KONSOLE_DCOP_SESSION renameSession $2
   else
      command_line
   fi
fi
fi


Also, i have something similar to snakattak3:
Code:

#!/bin/sh

PREFIX=""
PARAM=""

if [ "$#" == "0" ] ; then
   echo "qc (Quick Connect) Command Line:"
   echo "qc <protocol> <ip> / qc <protocol> <prefix> <suffix> / qc <shortcut>"
   echo "Explanation:"
   echo "<protocol>:   can be either s/f/h - sftp/ftp/ssh."
   echo "<ip>:         simple ip."
   echo "<prefix>:      can be either 172 or 10."
   echo "<suffix>:      any number between 1-255, the last sectio in the ip address."
   echo "<shortcut>:   Hardcoded shortcuts to specific URLs / IPs."
   echo "examples:"
   echo "   qc s ftp.linux.com -> translated into sftp to ftp.linux.com."
   echo "   qc f ftp.linux.com -> translated into ftp to ftp.linux.com."
   echo "   qc s 10 128 -> translated to sftp to 10.0.0.128."
else   
   if [ "$1" == "s" ] ; then
      PREFIX="sftp://"
   else if [ "$1" == "f" ] ; then
      PREFIX="ftp://"
   else if [ "$1" == "h" ] ; then #required for tab name
      PREFIX="ssh://"
   fi
   fi
   fi

   # additional command line parameter.
   PARAM=$3

   #aliases for easy access
   if [ "$2" == "home" ] ; then
      HOST="blabla.me.com"
   else if [ "$2" == "dev" ] ; then
      HOST="1.2.3.4"

   #easy access to internal network subnets (see help above).
   else if [ "$2" == "10" ] ; then
      HOST="10.0.0.$3"
      PARAM=$4
   else if [ "$2" == "172" ] ; then
      HOST="172.16.1.$3"
      PARAM=$4
   else
      HOST=$2
   fi
   fi
   fi
   fi

   if [ "$1" == "h" ] ; then
      CMD="ssh $HOST $PARAM"
   else
      CMD="lftp $PREFIX$HOST $PARAM"
   fi

   ORIG_NAME=`dcop $KONSOLE_DCOP_SESSION sessionName`
   dcop $KONSOLE_DCOP_SESSION renameSession "$PREFIX$HOST"
   $CMD
   dcop $KONSOLE_DCOP_SESSION renameSession "$ORIG_NAME"
fi


with the additional aliases:
Code:

#quick ftp connection.
alias qftp="qc f"
#quick sftp connection.
alias qsftp="qc s"
#quick SSH connection"
alias qssh="qc h"


which makes it more intoitive to access ftp/sftp/ssh using simple aliases.
_________________
Cheers,
Nir Dremer
www.dremer.org
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum