Ticket #1364 (assigned enhancement)

Opened 16 years ago

Last modified 7 months ago

overly verbose xterm window title

Reported by: ossi Owned by:
Priority: major Milestone: Future Releases
Component: mc-core Version: master
Keywords: Cc: hkmaly@…, andrej.korshikov@…
Blocked By: Blocking:
Branch state: no branch Votes for changeset:

Description

ticket #192 makes my desktop's task bar pretty useless for distinguishing my mc instances: the actually visible part of every window's title is now identical.
also, this additional information is useless in most cases.
if you want to do that right, the display must be selective: show only info which differs from the defaults. the locality and ownership of the session can be determined by querying the utmp database (on the command line, that would be the tty and who commands).
the less magic and more flexible approach would be allowing the user to specify what to put into the title manually - a patch has been proposed in http://mail.gnome.org/archives/mc-devel/2007-August/msg00004.html.

Change History

comment:1 Changed 15 years ago by angel_il

  • severity set to no branch
  • Milestone changed from 4.7 to 4.7.0-pre2

comment:2 follow-up: ↓ 3 Changed 15 years ago by hkmaly

  • Cc hkmaly@… added

What about making the hostname in title configurable option? Detecting defaults might be relatively hard and source of possible errors ... of course, the mentioned more flexible approach is even better.

comment:3 in reply to: ↑ 2 Changed 15 years ago by angel_il

Replying to hkmaly:

What about making the hostname in title configurable option

it will be in pre2

comment:4 Changed 15 years ago by angel_il

  • Milestone changed from 4.7.0-pre2 to 4.7.0-pre3

comment:5 Changed 15 years ago by angel_il

  • Milestone changed from 4.7.0-pre3 to 4.7.0-pre4

comment:6 Changed 15 years ago by angel_il

  • Status changed from new to accepted
  • Owner set to angel_il

comment:7 Changed 15 years ago by angel_il

  • Milestone changed from 4.7.0-pre4 to 4.7

comment:8 Changed 15 years ago by angel_il

  • Type changed from defect to enhancement

comment:9 Changed 15 years ago by angel_il

  • Milestone changed from 4.7 to 4.7.1

comment:10 Changed 15 years ago by angel_il

  • Milestone changed from 4.7.1 to 4.7

comment:11 Changed 13 years ago by andrew_b

  • Branch state set to no branch
  • Milestone changed from 4.7 to Future Releases

comment:12 Changed 8 years ago by andrew_b

  • Owner angel_il deleted
  • Status changed from accepted to assigned

comment:13 Changed 7 months ago by Andrei_Korshikov

  • Cc andrej.korshikov@… added

comment:14 Changed 7 months ago by zaytsev

Patch from the mailing list:

[PATCH] Custom mc xterm title
From: yarodin <yarodin gmail com>
To: mc-devel gnome org
Subject: [PATCH] Custom mc xterm title
Date: Tue, 21 Aug 2007 19:28:08 +0600
This patch will added possibility to configure xterm title through MC_XTITLE 
environment variable (like a zsh/bash promt)

%%    - percent '%' character                                 
%$,%# - '#' for root, '$' for normal user                    
%w,%d - working directory with edit/view filename                                   
%W,%C - working directory (last component only) with edit/view filename
%p    - process id (pid)
%u,%n - username
%h,m  - hostname up to the first dot
%H,M  - hostname.domainname
%t,%y - tty connected to stdin without /dev/
%l    - tty connected to stdin without /dev/ and tty (if present)                             
%V    - mc version

--- edit/editwidget.c.orig	Fri May 27 20:19:18 2005
+++ edit/editwidget.c	Thu Aug 16 12:59:49 2007
@@ -28,6 +28,9 @@
 #include "../src/widget.h"	/* redraw_labels() */
 #include "../src/menu.h"	/* menubar_new() */
 #include "../src/key.h"		/* is_idle() */
+#include "../src/main.h"	/* xterm_filename */
+#include "../src/win.h"		/* xterm_flag */
+#include "../src/layout.h"	/* xterm_title */
 
 WEdit *wedit;
 struct WMenu *edit_menubar;
@@ -168,6 +171,11 @@ edit_file (const char *_file, int line)
     static int made_directory = 0;
     Dlg_head *edit_dlg;
     WButtonBar *edit_bar;
+    
+    if (xterm_flag && xterm_title) {
+	xterm_filename = _file;
+        update_xterm_title_path ();
+    }
 
     if (option_backup_ext_int != -1) {
 	option_backup_ext = g_malloc (sizeof (int) + 1);
--- src/main.c.orig	Sat Jul 23 22:52:02 2005
+++ src/main.c	Thu Aug 16 16:00:33 2007
@@ -279,6 +279,8 @@ char *mc_home = NULL;
 
 char cmd_buf[512];
 
+char *xterm_filename = NULL;
+
 static void
 reload_panelized (WPanel *panel)
 {
@@ -1600,22 +1602,159 @@ midnight_callback (struct Dlg_head *h, d
 
 #define xtoolkit_panel_setup()
 
-/* Show current directory in the xterm title */
+/* Show current directory in the xterm title 
+   Added possibility to configure xterm title through MC_XTITLE environment variable (like a zsh/bash promt).
+   %%    - percent '%' character                                 
+   %$,%# - '#' for root, '$' for normal user                    
+   %w,%d - working directory with edit/view filename                                   
+   %W,%C - working directory (last component only) with edit/view filename
+   %p    - process id (pid)
+   %u,%n - username
+   %h,m  - hostname up to the first dot
+   %H,M  - hostname.domainname
+   %t,%y - tty connected to stdin without /dev/
+   %l    - tty connected to stdin without /dev/ and tty (if present)                             
+   %V    - mc version */
+
 void
 update_xterm_title_path (void)
 {
+    int i = 0;
     unsigned char *p, *s;
+    unsigned char title [BUF_MEDIUM+1],hostname[MAXHOSTNAMELEN+1],domainname[MAXHOSTNAMELEN+1],nametty[BUF_SMALL+1];
 
     if (xterm_flag && xterm_title) {
-	p = s = g_strdup (strip_home_and_password (current_panel->cwd));
+        /* Use special environment variable to format title */
+        if ((p = getenv ("MC_XTITLE")) == NULL) {
+            p = "mc - %n %m:%w";
+        }
+ 
 	do {
+            if (*p != '%') {
+                title [i++] = *p;
+                continue;
+            }
+            if (!*++p)
+                break;
+ 
+            /* Substitute '%' special characters
+             * (meaning the same as for bash, but preceded by '%', not '\')
+             */
+            s = NULL;
+            switch (*p) {
+            case '%' : /* % - percent '%' character */
+                title [i++] = '%';
+                break;
+            case '#' :
+            case '$' : /* %$ or %# - '#' for root, '$' for normal user */
+                title [i++] = (geteuid()==0 ? '#' : '$');
+                break;
+            case 'd' :
+            case 'w' : /* %w or %d - working directory */
+		if (xterm_filename==NULL) {
+                    s = g_strdup (strip_home_and_password (current_panel->cwd));
+                } else {
+                    if (view_one_file == NULL) {
+                        if (g_ascii_strcasecmp (strip_home_and_password (current_panel->cwd),"/")==0) {
+	          	    s = g_strdup_printf("/%s",xterm_filename);
+                        } else {
+                            s = g_strdup_printf("%s/%s",strip_home_and_password (current_panel->cwd),xterm_filename);
+                        }
+                    } else {
+                        s = g_strdup_printf("%s",strip_home_and_password (xterm_filename));
+                    }
+                }
+                break;
+            case 'C' :
+            case 'W' : /* %W or %C- working directory (last component only) */
+                if (xterm_filename==NULL) {		    
+            	    s = g_strdup (g_path_get_basename ((strip_home_and_password (current_panel->cwd))));
+                } else {
+                    if (view_one_file == NULL) {                          
+                        s = g_strdup_printf ("%s/%s",x_basename (strip_home_and_password (current_panel->cwd)),xterm_filename);
+		    } else {
+			s = g_strdup_printf ("%s/%s",x_basename (g_path_get_dirname (strip_home_and_password (xterm_filename))),x_basename(strip_home_and_password(xterm_filename)));
+		    }
+                }
+                break;
+            case 'p' : /* %p - process id (pid) */
+                s = g_strdup_printf ("%d", getpid ());
+                break;
+            case 'n' :
+            case 'u' : /* %u or %n - username */
+                s = g_strdup (getpwuid (getuid ()) -> pw_name);
+                break;
+            case 'm' :
+	    case 'h' : /* %h or %m - hostname up to the first dot */
+		if (gethostname (hostname,MAXHOSTNAMELEN) == 0) {
+                    hostname [strcspn(hostname,".")] = '\0';                    
+                    s = g_strdup(hostname);
+		}
+	        break;
+            case 'M' :
+            case 'H' : /* %H or %M - hostname.domainname */
+                if (gethostname (hostname,MAXHOSTNAMELEN) == 0) {
+                    hostname [MAXHOSTNAMELEN] = '\0';                
+            	    if (getdomainname (domainname, MAXHOSTNAMELEN) == 0) {
+	        	domainname [MAXHOSTNAMELEN] = '\0';            	    
+            		if (strlen(g_strstrip(hostname))>0 && strlen(g_strstrip(domainname))==0) { 
+			    s = g_strdup(hostname);
+ 	    		} else if (strlen(g_strstrip(hostname))==0 && strlen(g_strstrip(domainname))>0) {
+                	    s = g_strdup (domainname);                    
+            		} else if (strlen(g_strstrip(hostname))>0 && strlen(g_strstrip(domainname))>0) {
+                            if (g_str_has_suffix (g_strstrip(hostname), ".") || g_str_has_prefix (g_strstrip(domainname), ".")) {	                    	
+				s = g_strconcat(hostname,domainname,NULL);
+			    } else {
+                                s = g_strconcat(hostname,".",domainname,NULL);
+			    }
+            		}
+		    }
+                }
+                break;
+            case 'y' :
+            case 't' : /* %t or %y - tty connected to stdin without /dev/ */                 
+                s = g_strdup (x_basename(ttyname (0)));
+                break;
+            case 'l' : /* %l - tty connected to stdin */
+                if (ttyname_r (0,nametty,BUF_SMALL) == 0) {
+                    nametty [BUF_SMALL] = '\0';
+            	    if (g_str_has_prefix (nametty, "/dev/tty")) {
+                        strncpy(nametty,nametty+8,BUF_SMALL-8);    
+			s = g_strdup(g_strstrip(nametty));		      	
+                    } else {
+			s = g_strdup(x_basename(nametty));
+                    }
+		}
+                break;
+            case 'V' : /* %V - mc version */
+                s = g_strdup (VERSION);
+                break;
+            }
+ 
+            /* Append substituted string */
+            if (s) {
+                strncpy (title+i, s, BUF_MEDIUM-i);
+                title [BUF_MEDIUM] = '\0';
+                i = strlen (title);
+                g_free (s);
+            }
+        } while (*++p && i<BUF_MEDIUM);
+         title [i] = '\0';
+ 
+        /* Replace non-printable characters with '?' */
+        s = title;
+        while (*s) {
+	
 	    if (!is_printable (*s))
 		*s = '?';
-	} while (*++s);
-	fprintf (stdout, "\33]0;mc - %s\7", p);
+            s++;
+        }
+ 
+        /* Use xterm escape sequence to set window title */
+        fprintf (stdout, "\33]0;%s\7", title);
 	fflush (stdout);
-	g_free (p);
     }
+    xterm_filename=NULL; 
 }
 
 /*
--- src/main.h.orig	Fri Jul  1 21:47:06 2005
+++ src/main.h	Thu Aug 16 13:05:33 2007
@@ -64,6 +64,7 @@ extern int alternate_plus_minus;
 extern int only_leading_plus_minus;
 extern int output_starts_shell;
 extern int midnight_shutdown;
+extern char *xterm_filename;
 extern char cmd_buf [512];
 extern const char *shell;
 
--- src/view.c.orig	Fri May 27 20:19:18 2005
+++ src/view.c	Tue Aug 14 12:10:25 2007
@@ -2654,6 +2654,11 @@ view (const char *_command, const char *
     WButtonBar *bar;
     Dlg_head *view_dlg;
 
+    if (xterm_flag && xterm_title) {
+        xterm_filename = _file;
+	update_xterm_title_path ();
+    }
+
     /* Create dialog and widgets, put them on the dialog */
     view_dlg =
 	create_dlg (0, 0, LINES, COLS, NULL, view_dialog_callback,

comment:15 Changed 7 months ago by zaytsev

Related ticket: #4538

comment:16 Changed 7 months ago by ossi

that patch is a mess ...
but the intended semantics make sense at first sight.
(i presumably thought the same 15 years ago. 😂)

Note: See TracTickets for help on using tickets.