Renaming Konsole session tabs programmatically - SOLVED

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



	I've fiddled with this for quite a while, and I finally have gotten it to
work!
	I wanted the names on the Konsole session tabs to match the last node of the
working directory; that makes it much easier to pick the right tab.  The
change is triggered by bash routine kcd (konsole cd) and the code that
actually changes the tab name is changeKonsoleSessionName.
	I've attached the pieces of code that do the job.  (changeKonsoleSessionName
is written in Rexx.  Sorry, but the string manipulation tools in bash are
just awful; but the code is probably clear enough, and could be rewritten by
an enterprising person. :-)
	I was thinking of adding something to change the text colour of the tab, but
there doesn't seem to be a DCOP method for doing that. :-(

Leslie
--
Platform: Linux
Distribution: openSUSE Leap 15.4 - x86_64
Desktop Environment: Trinity
Qt: 3.5.0
TDE: R14.1.0
tde-config: 1.0
#!/bin/bash
# Rename a Konsole session (tab) to match the (possibly new) path.
kcd ()
{ local Path SessionName ThisTerm Owner OwnerDCOP Instances Konsole Mainwindows Mainwindow

# Get the new path.
  if [ $# = 1 ]; then     # No path was provided
    Path="$PWD"           # Just change the session's name
  fi

  if [ "$1" = '.' ]; then # It's the same as the current path
    Path="$PWD"           # Just change the session's name

  else
    Path="$1"             # Use the supplied path
  fi

# Change to the supplied path.
  cd $Path

# Change the Konsole session name to match the last node of the path.
  rexx /usr/local/bin/changeKonsoleSessionName $PWD
}

  export -f kcd
#!/usr/bin/env rexx
/* Change the current Konsole session name to match the last node of the path.       */
/* This program is called by bash script kcd, which performs the cd operation first; */
/* rexx cannot permanently change the current directory to another.                  */
/* .-------------------------------------------------------------------------------. */
/* |                               Change Log                                      | */
/* |Version    Date      Description                                               | */
/* |------- ---------- ------------------------------------------------------------| */
/* |                                                                               | */
/* |1.0     2023-09-01 Creation                                                    | */
/* '-------------------------------------------------------------------------------' */
/* N.B. Root sessions seem to always set RC = 1 for every command, so this program   */
/* checks for messages to determine if a failure has occurred.                       */

  trace normal

/* Define collections for collecting command output. */
  bash.       = ''; bash.0    = 0
  message.    = ''; message.0 = 0
  DCOPsession = .Array ~ new

/* Route commands to bash and their outputs into program storage. */
  address 'bash' with output replace stem bash. error replace stem message.

/* Make sure that the official commands are being used. */
  dcop = '/opt/trinity/bin/dcop'
  tty  = '/usr/bin/tty'
  who  = '/usr/bin/who'

/* Process the input argument. */
  parse arg path
  path = strip(path)
  path = strip(path,trailing,'/')

/* Leave off /home/ if the path is to the home directory. */
  if left(path,6) = '/home/' then
    path = subStr(path,7)

/* Use only the last node of the path for the new session name. */
  if lastPos('/',path) > 0 then                           -- there is more than one.
    parse var path =(lastPos('/',Path,)+1) newName .

  else                                                    -- there is only one.
    parse var path                         newName

/* Get this terminal's device number. */
  call cmd tty
  parse var bash.1 '/dev/' thisTerminal

/* See who owns this terminal. */
  call cmd who

  loop B = 1 to bash.0 until terminal = thisTerminal
    parse var bash.B owner terminal .
  end

/* Get the session name of the owner's DCOP DCOPsession. */
/* (An array is used to make it easier to bypass the header line.) */
  address 'bash' dcop '--user' owner '--list-sessions' with output replace using (DCOPsession)
  DCOPsession ~ delete(1)                                 -- Discard the header.

/* There might be more than one session to talk to. */
  loop D = 1 to DCOPsession ~ items

/*  Get the name(s) of the konsole instance(s). */
    call cmd dcop '--user' owner '--session' DCOPsession[D] "'konsole*'"

/*  Save the output so that bash. may be reused. */
    loop B = 1 to bash.0
      K = B
      konsoleSession.K = bash.B
      konsoleSession.0 = K
    end

/*  Find the active Konsole session. */
    loop K = 1 to konsoleSession.0
      call cmd dcop '--user' owner '--session' DCOPsession[D] konsoleSession.K "'konsole-mainwindow#*'"
      mainWindow = bash.1
      call cmd dcop '--user' owner '--session' DCOPsession[D] konsoleSession.K mainWindow 'isActiveWindow'

      if bash.1 = 'false' then
        iterate K

/*    Find the ID of the current session (tab). */
      call cmd dcop '--user' owner '--session' DCOPsession[D] konsoleSession.K 'konsole currentSession'
      sessionID = bash.1

/*    Rename the session (tab). */
      call cmd dcop '--user' owner '--session' DCOPsession[D] konsoleSession.K sessionID 'renameSession' newName
      leave D
    end
  end
exit 0

cmd: procedure expose message. bash.
  trace normal
  parse arg commandString
  commandString

  if message.0 = 0 then
    return

    loop M = 1 to message.0
      say message.M
    end
exit 1
____________________________________________________
tde-users mailing list -- users@xxxxxxxxxxxxxxxxxx
To unsubscribe send an email to users-leave@xxxxxxxxxxxxxxxxxx
Web mail archive available at https://mail.trinitydesktop.org/mailman3/hyperkitty/list/users@xxxxxxxxxxxxxxxxxx

[Index of Archives]     [Trinity Devel]     [KDE]     [Linux Sound]     [ALSA Users]     [ALSA Devel]     [Linux Audio Users]     [Linux Media]     [Kernel]     [Gimp]     [Yosemite News]     [Linux Media]     [Trinity Desktop Environment]

  Powered by Linux