[hackers] [lchat] refactor printing of terminal title || Jan Klemkow

From: <git_AT_suckless.org>
Date: Sun, 9 Oct 2022 23:36:27 +0200 (CEST)

commit e821ed80afb2e4c922c000dbe59043064096516b
Author: Jan Klemkow <j.klemkow_AT_wemelug.de>
AuthorDate: Sun Oct 9 23:33:33 2022 +0200
Commit: Jan Klemkow <j.klemkow_AT_wemelug.de>
CommitDate: Sun Oct 9 23:33:33 2022 +0200

    refactor printing of terminal title
    
     - fix segmentation fault reported by NRK <nrk_AT_disroot.org>
     - remove redundant code
     - move code to util.c. So, filter programs can also use
       this feature.
     - while here, reorder util.h in the same order as util.c

diff --git a/lchat.c b/lchat.c
index d5d8faf..9725124 100644
--- a/lchat.c
+++ b/lchat.c
_AT_@ -37,6 +37,7 @@
 
 static struct termios origin_term;
 static struct winsize winsize;
+static char *TERM;
 
 static void
 sigwinch(int sig)
_AT_@ -48,13 +49,8 @@ sigwinch(int sig)
 static void
 exit_handler(void)
 {
- char *title = getenv("TERM");
-
         /* reset terminal's window name */
- if (strncmp(title, "screen", 6) == 0)
- printf("\033k%s\033\\", title);
- else
- printf("\033]0;%s\a", title);
+ set_title(TERM, TERM);
 
         if (tcsetattr(STDIN_FILENO, TCSANOW, &origin_term) == -1)
                 die("tcsetattr:");
_AT_@ -171,6 +167,9 @@ main(int argc, char *argv[])
         char *prompt = read_file_line(".prompt");
         char *title = read_file_line(".title");
 
+ if ((TERM = getenv("TERM")) == NULL)
+ TERM = "";
+
         if (sl == NULL)
                 die("Failed to initialize slackline");
 
_AT_@ -252,10 +251,7 @@ main(int argc, char *argv[])
                 if ((title = basename(path)) == NULL)
                         die("basename:");
         }
- if (strncmp(getenv("TERM"), "screen", 6) == 0)
- printf("\033k%s\033\\", title);
- else
- printf("\033]0;%s\a", title);
+ set_title(TERM, title);
 
         /* prepare terminal reset on exit */
         if (tcgetattr(fd, &origin_term) == -1)
diff --git a/util.c b/util.c
index fd36421..0516577 100644
--- a/util.c
+++ b/util.c
_AT_@ -62,3 +62,12 @@ bell_match(const char *str, const char *regex_file)
 
         return false;
 }
+
+void
+set_title(const char *term, char *title)
+{
+ if (strncmp(term, "screen", 6) == 0)
+ printf("\033k%s\033\\", title);
+ else
+ printf("\033]0;%s\a", title);
+}
diff --git a/util.h b/util.h
index 9241384..3cac358 100644
--- a/util.h
+++ b/util.h
_AT_@ -1,7 +1,8 @@
 #ifndef _UTIL_H_
 #define _UTIL_H_
 
-bool bell_match(const char *str, const char *regex_file);
 void die(const char *fmt, ...);
+bool bell_match(const char *str, const char *regex_file);
+void set_title(const char *term, const char *title);
 
 #endif
Received on Sun Oct 09 2022 - 23:36:27 CEST

This archive was generated by hypermail 2.3.0 : Sun Oct 09 2022 - 23:37:00 CEST