refactor printing of terminal title - lchat - A line oriented chat front end fo… | |
git clone git://git.suckless.org/lchat | |
Log | |
Files | |
Refs | |
README | |
--- | |
commit e821ed80afb2e4c922c000dbe59043064096516b | |
parent 91579d79ea33b7f14db6b8187255971b467f2553 | |
Author: Jan Klemkow <[email protected]> | |
Date: Sun, 9 Oct 2022 23:33:33 +0200 | |
refactor printing of terminal title | |
- fix segmentation fault reported by NRK <[email protected]> | |
- 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 | |
Diffstat: | |
M lchat.c | 16 ++++++---------- | |
M util.c | 9 +++++++++ | |
M util.h | 3 ++- | |
3 files changed, 17 insertions(+), 11 deletions(-) | |
--- | |
diff --git a/lchat.c b/lchat.c | |
@@ -37,6 +37,7 @@ | |
static struct termios origin_term; | |
static struct winsize winsize; | |
+static char *TERM; | |
static void | |
sigwinch(int sig) | |
@@ -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:"); | |
@@ -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"); | |
@@ -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 | |
@@ -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 | |
@@ -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 |