Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overly verbose xterm window title #1364

Open
mc-butler opened this issue Jun 21, 2009 · 16 comments
Open

overly verbose xterm window title #1364

mc-butler opened this issue Jun 21, 2009 · 16 comments
Labels
area: core Issues not related to a specific subsystem prio: medium Has the potential to affect progress

Comments

@mc-butler
Copy link

Important

This issue was migrated from Trac:

Origin https://midnight-commander.org/ticket/1364
Reporter ossi (@ossilator)
Mentions hkmaly@….com, andrej.korshikov@….com

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.

@mc-butler
Copy link
Author

Changed by angel_il (@ilia-maslakov) on Jul 20, 2009 at 12:33 UTC (comment 1)

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

@mc-butler
Copy link
Author

Changed by hkmaly (hkmaly@….com) on Jul 31, 2009 at 10:58 UTC (comment 2)

  • Cc set to hkmaly@….com

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.

@mc-butler
Copy link
Author

Changed by angel_il (@ilia-maslakov) on Jul 31, 2009 at 11:02 UTC (comment 2.3)

Replying to hkmaly:

What about making the hostname in title configurable option

it will be in pre2

@mc-butler
Copy link
Author

Changed by angel_il (@ilia-maslakov) on Aug 8, 2009 at 12:06 UTC (comment 4)

  • Milestone changed from 4.7.0-pre2 to 4.7.0-pre3

@mc-butler
Copy link
Author

Changed by angel_il (@ilia-maslakov) on Sep 22, 2009 at 11:28 UTC (comment 5)

  • Milestone changed from 4.7.0-pre3 to 4.7.0-pre4

@mc-butler
Copy link
Author

Changed by angel_il (@ilia-maslakov) on Oct 24, 2009 at 13:50 UTC (comment 6)

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

@mc-butler
Copy link
Author

Changed by angel_il (@ilia-maslakov) on Oct 26, 2009 at 12:19 UTC (comment 7)

  • Milestone changed from 4.7.0-pre4 to 4.7

@mc-butler
Copy link
Author

Changed by angel_il (@ilia-maslakov) on Nov 2, 2009 at 11:23 UTC (comment 8)

  • Type changed from defect to enhancement

@mc-butler
Copy link
Author

Changed by angel_il (@ilia-maslakov) on Dec 28, 2009 at 14:51 UTC (comment 9)

  • Milestone changed from 4.7 to 4.7.1

@mc-butler
Copy link
Author

Changed by angel_il (@ilia-maslakov) on Feb 24, 2010 at 12:45 UTC (comment 10)

  • Milestone changed from 4.7.1 to 4.7

@mc-butler
Copy link
Author

Changed by andrew_b (@aborodin) on Oct 29, 2011 at 16:53 UTC (comment 11)

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

@mc-butler
Copy link
Author

Changed by andrew_b (@aborodin) on Nov 29, 2016 at 7:23 UTC (comment 12)

  • Owner angel_il deleted
  • Status changed from accepted to assigned

@mc-butler
Copy link
Author

Changed by Andrei_Korshikov (andrej.korshikov@….com) on May 16, 2024 at 12:54 UTC (comment 13)

  • Cc changed from hkmaly@….com to hkmaly@….com, andrej.korshikov@….com

@mc-butler
Copy link
Author

Changed by zaytsev (@zyv) on May 16, 2024 at 14:54 UTC (comment 14)

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,

@mc-butler
Copy link
Author

Changed by zaytsev (@zyv) on May 16, 2024 at 14:56 UTC (comment 15)

Related ticket: #4538

@mc-butler
Copy link
Author

Changed by ossi (@ossilator) on May 16, 2024 at 15:19 UTC (comment 16)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: core Issues not related to a specific subsystem prio: medium Has the potential to affect progress
Development

No branches or pull requests

1 participant